Spring Data Redis with PubSub Messaging

Posted By Vishal Kumar | 31-Jul-2018

In Redis, publishers are not customized to send their messages to particular subscribers. Or maybe, published messages are portrayed into channels, without information of what (assuming any) subscribers there might be. 

Correspondingly, subscribers express enthusiasm for at least one points and just get messages that are of enthusiasm, without information of what (assuming any) publishers there are. 

This decoupling of publishers and subscribers can take into consideration more prominent adaptability and a more powerful system topology.

Redis Configuration

We should begin by including the setup which is required for the message lines. 

@Bean
MessageListenerAdapter messageListener() { 

    return new MessageListenerAdapter(new RedisMessageSubscriber());
}

RedisMessageListenerContainer is a class gave by Spring Data Redis which gives nonconcurrent conduct to Redis message audience members.

@Bean
RedisMessageListenerContainer redisContainer() {

    RedisMessageListenerContainer container = new RedisMessageListenerContainer(); 

    container.setConnectionFactory(jedisConnectionFactory()); 
    container.addMessageListener(messageListener(), topic()); 

    return container; 
}

We will likewise make a bean utilizing a custom-fabricated MessagePublisher interface and a RedisMessagePublisher usage. Thusly, we can have a non-specific message-distributing API, and have the Redis usage take a redisTemplate and theme as constructor contentions:

@Bean
MessagePublisher redisPublisher() { 

    return new RedisMessagePublisher(redisTemplate(), topic());
}

At last, we'll set up a subject to which the distributer will send messages, and the supporter will get them:

@Bean
ChannelTopic topic() {

    return new ChannelTopic("messageQueue");
}

Message Publishing

MessagePublisher is not provided by spring data redis. we can create a custom interface who can use redis template for execution:

public interface MessagePublisher {

    void publish(String message);
}

Our following stage is to give an execution of the MessagePublisher interface, including message distributing points of interest and utilizing the capacities in redisTemplate. 

The format contains an exceptionally rich arrangement of capacities for extensive variety of activities – out of which convertAndSend is equipped for making an impression on a line through a subject:

public class RedisMessagePublisher implements MessagePublisher {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ChannelTopic topic;

    public RedisMessagePublisher() {
    }

    public RedisMessagePublisher(

      RedisTemplate<String, Object> redisTemplate, ChannelTopic topic) {

      this.redisTemplate = redisTemplate;
      this.topic = topic;

    }

    public void publish(String message) {

        redisTemplate.convertAndSend(topic.getTopic(), message);
    }
}

As should be obvious, the distributer usage is direct. It utilizes the convertAndSend() strategy for the redisTemplate to organize and distribute the offered message to the designed point. 

A point actualizes distribute and buy in semantics: when a message is distributed, it goes to every one of the endorsers who are enrolled to tune in on that theme.

Message Subscribing

RedisMessageSubscriber actualizes the Spring Data Redis-gave MessageListener interface:

@Service
public class RedisMessageSubscriber implements MessageListener {

    public static List<String> messageList = new ArrayList<String>();

    public void onMessage(Message message, byte[] pattern) {

        messageList.add(message.toString());
        System.out.println("Message received: " + message.toString());

    }
}

Note that there is a second parameter called design, which we have not utilized as a part of this illustration. The Spring Data Redis documentation expresses that this parameter speaks to the, "design coordinating the channel (if indicated)", however that it can be invalid.

Receiving and Sending Messages

Presently we'll assemble everything. How about we make a message and afterward distribute it utilizing the RedisMessagePublisher:

String message = "Message " + UUID.randomUUID();

redisMessagePublisher.publish(message);

When we call publish(message), the substance is sent to Redis, where it is directed to the message line theme characterized in our distributer. At that point it is appropriated to the endorsers of that subject. 

You may as of now have seen that RedisMessageSubscriber is an audience, which registers itself to the line for recovery of messages. 

On the entry of the message, the endorser's onMessage() technique characterized activated. 

In our case, we can confirm that we've gotten messages that have been distributed by checking the messageList in our RedisMessageSubscriber:

RedisMessageSubscriber.messageList.get(0).contains(message)

Conclusion

In this article, we inspected a bar/sub message line usage utilizing Spring Data Redis. 

The execution of the above illustration can be found in a GitHub project.

Request for Proposal

Recaptcha is required.

Sending message..