Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis Pub/Sub with Spring Boot

Demo project for the tutorial Hands-On Redis Pub/Sub with Spring Boot for Fast and Lightweight Messaging.

Stack

  • Java 21
  • Spring Boot 3.5.x
  • Spring Data Redis (Lettuce)
  • Docker Compose

Project Structure

src/main/java/com/boottechnologies/ci/redispubsub/
├── RedisPubSubApplication.java
├── config/
│   ├── RedisChannels.java          # Channel name constants
│   └── RedisConfig.java            # Container, listeners, ObjectMapper
├── controller/
│   └── EventController.java        # REST endpoints
├── dto/
│   ├── OrderRequest.java
│   ├── PublishResponse.java
│   └── StatusUpdateRequest.java
├── exception/
│   └── GlobalExceptionHandler.java
├── model/
│   └── EventMessage.java           # Envelope for all published events
├── publisher/
│   └── EventPublisher.java         # Publishes events via StringRedisTemplate
└── subscriber/
    ├── AuditEventSubscriber.java   # Pattern subscriber (orders.events.*)
    └── OrderEventSubscriber.java   # Channel subscriber (orders.events.created / .status)

Getting Started

1. Start Redis

docker compose up -d

2. Run the Application

./mvnw spring-boot:run

3. Publish an Order Created Event

curl -s -X POST http://localhost:8080/api/orders/created \
  -H "Content-Type: application/json" \
  -d '{
    "orderId": "ORD-001",
    "customerId": "CUST-42",
    "amount": 149.99,
    "notes": "Express shipping requested"
  }' | jq

4. Update Order Status

curl -s -X POST http://localhost:8080/api/orders/status \
  -H "Content-Type: application/json" \
  -d '{
    "orderId": "ORD-001",
    "status": "SHIPPED",
    "reason": "Express courier dispatched"
  }' | jq

Monitoring Redis Pub/Sub

docker exec -it redis-pubsub redis-cli

# List active channels
PUBSUB CHANNELS

# Subscriber count per channel
PUBSUB NUMSUB orders.events.created orders.events.status

# Count active pattern subscriptions
PUBSUB NUMPAT

# Subscribe and watch messages manually
SUBSCRIBE orders.events.created

Running Tests

Tests use Testcontainers — Docker must be running.

./mvnw test

Releases

Packages

Contributors

Languages