Spring Data Redis with PubSub Messaging
Posted By : Vishal Kumar | 08-Feb-2021
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
@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
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
A point actualizes distribute and buy in semantics: when a message is distributed, it goes to every one of the endorsers who
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
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
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vishal Kumar
Vishal Kumar is Master in Computers Application. He has good technical skills in Java and always motivated to learn new things.