Sitemap
Ravi Jaisawal

Ravi Jaisawal

Lead Software Engineer with 12+ years of experience architecting and delivering scalable, cloud-native distributed systems and enterprise platforms.

Implementing a Publish-Subscribe (Pub/Sub) pattern in Java with Spring Boot can be done in multiple ways, depending on your needs:

2 min readJun 13, 2025

--

The Pub/Sub (Publisher/Subscriber) model is a messaging pattern used in software architecture to facilitate asynchronous communication between different components or systems. In this model, publishers produce messages that are then consumed by subscribers.

Common Approaches in Spring Boot:

  1. In-memory Pub/Sub using Spring Events
  2. Distributed Pub/Sub using Apache Kafka (recommended for production)
  3. Other options: RabbitMQ, Redis Pub/Sub, Google Cloud Pub/Sub, etc.

Spring Boot + Kafka (Production-grade)

  1. Create a Publisher Service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class EventPublisher {

private final KafkaTemplate<String, String> kafkaTemplate;

@Autowired
public EventPublisher(KafkaTemplate<String, String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}

public void publish(String topic, String message) {
kafkaTemplate.send(topic, message);
}
}

2. Create a subscriber/consumer service

Get Ravi Jaisawal’s stories in your inbox

Join Medium for free to get updates from this writer.

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class EventSubscriber {

@KafkaListener(topics = "demo-topic", groupId = "test-group")
public void handleMessage(String message) {
System.out.println("Received Message: " + message);
}
}

Redis Pub/Sub Spring Boot

  1. Create Publisher
@Service
public class RedisPublisher {

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private ChannelTopic topic;

public void publish(String message) {
redisTemplate.convertAndSend(topic.getTopic(), message);
System.out.println("Published: " + message);
}
}

2. Create Subscriber

@Service
public class RedisSubscriber implements MessageListener {

@Override
public void onMessage(Message message, byte[] pattern) {
String msg = new String(message.getBody());
System.out.println("Received Message: " + msg);
}
}

In-Memory Spring Events (for internal async)

1.Add Listener

@Component
public class NotificationListener {

@EventListener
public void onEvent(String message) {
System.out.println("Handled in-memory event: " + message);
}
}

2. Publish Event

@Autowired
private ApplicationEventPublisher eventPublisher;

public void publishMessage(String msg) {
eventPublisher.publishEvent(msg);
}

--

--

Ravi Jaisawal
Ravi Jaisawal

Written by Ravi Jaisawal

Lead Software Engineer with 12+ years of experience architecting and delivering scalable, cloud-native distributed systems and enterprise platforms.