Welcome! In this unit, we will delve into implementing Pub/Sub for notifications within our Redis-based backend system project. You've already learned how to manage user data, handle transactions, and use streams for event logging. Now, we'll add another powerful feature to our project: real-time notifications using Redis Pub/Sub (publish/subscribe). This will enable our system to send and receive messages instantaneously.
In this unit, we'll focus on creating a simple real-time notification system using Redis Pub/Sub. Specifically, we'll cover:
- Publishing Messages: How to send notifications.
- Subscribing to Channels: How to receive and handle notifications.
To receive notifications, we set up a subscriber that listens to a specific channel. In our implementation, this is handled by the JedisPubSub
class, which processes incoming messages:
Java1// Create a pubsub instance to unsubscribe after receiving a set number of messages 2JedisPubSub pubSub = new JedisPubSub() { 3 private final AtomicInteger messageCount = new AtomicInteger(0); 4 private final int maxMessages = 2; 5 6 @Override 7 public void onMessage(String channel, String message) { 8 System.out.println("Received message on " + channel + ": " + message); 9 10 // Unsubscribe after maxMessages have been received 11 if (messageCount.incrementAndGet() >= maxMessages) { 12 System.out.println("Max messages reached. Unsubscribing..."); 13 unsubscribe(); 14 } 15 } 16};
This setup listens for messages on a channel and unsubscribes after receiving a certain number of messages (maxMessages
). The subscriber runs in a separate thread:
Java1Thread subscriberThread = new Thread(() -> { 2 try { 3 subscriberJedis.subscribe(pubSub, "chat_room"); 4 } catch (Exception e) { 5 e.printStackTrace(); 6 } finally { 7 subscriberJedis.close(); 8 } 9}); 10subscriberThread.start();
This thread subscribes to the "chat_room"
channel and processes messages in real time.
To send notifications, we use the publishMessage
method, which sends messages to the specified channel:
Java1// Function to publish messages to a channel 2public static void publishMessage(Jedis jedis, String channel, Notification message) { 3 String jsonMessage = gson.toJson(message); 4 jedis.publish(channel, jsonMessage); 5 System.out.println("Message published by " + message.getUser() + ": " + message.getText()); 6}
This method converts the Notification
object to JSON format and publishes it to the channel.
In the Main
class, we publish messages like this:
Java1commandJedis.publish("chat_room", "Hello from the main thread!"); 2commandJedis.publish("chat_room", "Another message");
Messages are published to the "chat_room"
channel, where the subscriber listens and processes them.
Here’s the full flow in action:
- Start the subscriber in a separate thread to listen for messages on the
"chat_room"
channel. - Publish messages to the same channel.
- Unsubscribe automatically after processing a set number of messages or manually after a timeout.
The output for this implementation will look like:
1Received message on chat_room: Hello from the main thread! 2Received message on chat_room: Another message 3Max messages reached. Unsubscribing... 4Unsubscribing due to timeout... 5Main thread finished.
In this code, the JedisPubSub
instance processes incoming messages in real time, and the publishMessage
method allows you to send notifications. This combination forms the core of our Redis-based real-time notification system.
Exciting, isn’t it? Now, it's time to put this into practice. Let's implement the complete code to build our real-time notification system.
Happy coding!