Redis provides us with the Publish/subscribe (Publish/subscribe) feature. We can subscribe a channel (a subscription), and Redis notifies us when someone publish a message on the channel so we can get a message from someone else.
As a Redis client for Java, Jedis provides an interface for Publish/subscribe. This article describes how to use Jedis to implement Publish/subscribe for Redis.
Define a
Subscriber
Class
Jedis defines an abstract class, in which the JedisPubSub
callback method of the publish/subsribe is defined. By inheriting the JedisPubSub
classes and re-implementing the callback methods, we can customize our own processing logic when the Publish/subsribe event occurs.
In the following example, we define the Subscriber
class, which inherits the class and JedisPubSub
re-implements the callback method in it.
Import Redis.clients.jedis.jedispubsub;public Class Subscriber extends Jedispubsub {public subscriber () { } Public void OnMessage (String channel, String message) { System.out.println (String.Format ("Receive Redis Published message, channel%s, message%s ", channel, Message)); } public void Onsubscribe (String channel, int subscribedchannels) { System.out.println (String.Format ("Subscribe Redis channel success, channel%s, Subscribedchannels%d ", Channel, Subscribedchannels)); public void Onunsubscribe (String channel, int subscribedchannels) { System.out.println (String.Format (" Unsubscribe Redis channel, channel%s, Subscribedchannels%d ", Channel, Subscribedchannels));} }
Defining the Subthread Thread class
Since the operation of the Jedis subscribe
is blocked, we have another thread to perform the subscribe operation.
import redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;public class Subthread extends Thread {private final jedispool jedispool; Private Final Subscriber subscriber = new Subscriber (); Private final String channel = "MyChannel"; Public Subthread (Jedispool Jedispool) {super ("Subthread"); This.jedispool = Jedispool; } @Override public void Run () {System.out.println (String.Format ("Subscribe Redis, channel%s, thread'll be Blocked ", channel)); Jedis Jedis = null; try {Jedis = Jedispool.getresource (); Jedis.subscribe (subscriber, channel); } catch (Exception e) {System.out.println (String.Format ("Subsrcibe Channel error,%s", e)); } finally {if (Jedis! = null) {jedis.close (); } } }}
In the above code, we JedisPool
get an instance from Jedis
and use the Jedis
operation of this instance subscribe
.
Jedis
's subscribe
statement is as follows:
public void Subscribe (final jedispubsub jedispubsub, final String ... channels)
The first parameter accepts an JedisPubSub
object, and the second parameter specifies which channel to subscribe to. In the example above, we pass the object we define to the Subscriber
subscribe
method.
When the Publish/subscribe event occurs, our method is automatically called Subscriber
.
Defining the Publisher Class
Publisher
Class accepts input from the user and publishes the input to the channel. When the user enters "quit", the input ends.
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;public class Publisher { private final Jedispool Jedispool; Public Publisher (Jedispool jedispool) { this.jedispool = Jedispool; } public void Start () { BufferedReader reader = new BufferedReader (new InputStreamReader (system.in)); Jedis Jedis = Jedispool.getresource (); while (true) { String line = null; try {line = Reader.readline (); if (! " Quit ". equals" { jedis.publish ("MyChannel", line), } else {break ; } } catch ( IOException e) { e.printstacktrace ();}}}}
Defining the entry Code
Here is our program entry code.
import redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;public class Pubsubdemo {public static void main (string[] args) {// Replace with your Reids address and port String redisip = "127.0.0.1"; int reidsport = 6379; Jedispool Jedispool = new Jedispool (new Jedispoolconfig (), Redisip, Reidsport); System.out.println (String.Format ("Redis pool is starting, Redis IP%s, redis port%d", Redisip, Reidsport)); Subthread subthread = new Subthread (Jedispool); Subthread.start (); Publisher publisher = new Publisher (Jedispool); Publisher.start (); }}
In the above code, we first generated a JedisPool
Redis connection pool, which is thread-safe because it is Jedis
not thread-safe JedisPool
. Our program is used in both the main thread and the subscription thread (subthread) Jedis
, so in the program we useJedisPool。
Since Jedis
the subcribe operation is blocked, we have another thread to perform the subcribe operation.
By invoking the Publisher::start()
method, the user's input is accepted and publish to the specified channel.
The application of Redis subscription and publication in Java