The main structure is: The Python process publishes messages, and the Java process subscribes to messages.
Dependent environment:
Python:pip Install Redis
Java:jedis
1. Python side:
pubsub.py
Import Redisclass PubSub (object): def __init__ (self, host, port, db): self.__conn = Redis. Redis (host, port, DB) def publish (self, Channel, msg): self.__conn.publish (channel, msg) return True def subscribe (self, channel): pub = Self.__conn.pubsub () pub.subscribe (channel) Pub.parse_response () return Pub
sub.py
From PubSub Import pubsubobj = PubSub (' localhost ', 6379, 1) redis_sub = obj.subscribe (' cord ') while True: msg = REDIS_SU B.parse_response () msg = Msg[2].decode () print (msg)
2. Java side
Redispub.java
Import redis.clients.jedis.jedis;import java.util.date;public class redispub { private static Jedis Jedis = new Jedis ("localhost", 6379); Private static final String channel = "cord"; public static void main (string[] args) {String message = new Date (). toString (); Jedis.publish (channel, message) ; }}
Reference Links:
Http://www.cnblogs.com/melonjiang/p/5342383.html
Implementation of Python and Java interprocess communication based on Redis (subscription release)