Jedis PubSub from Different Classes

743 views Asked by At

I was wondering if it was possible to have a publisher in jedis pushing messages onto a channel, and in a completely separate class, have a subscriber listening for messages on the same channel.

For example, for one class, have

jedis.publish(CHANNEL_NAME, entry.toString());

and on the other class, listen via

jedis.subscribe(subscriber, CHANNEL_NAME);

where subscriber is an inner class extending JedisPubSub.

Thanks for your help!

1

There are 1 answers

0
jstnchng On BEST ANSWER

After some research, I found that this is possible! The code stays the same, so long as you specify the CHANNEL_NAME in both to be the same (can maybe read from a properties file?) then there should be no problem. N.B. your subscriber has to be subscribed to the channel & waiting for messages before you publish anything to the channel.