This project demonstrates the implementation of Redis Streams within a Java Spring Boot application. It showcases a scheduled publisher automatically sending messages and a consumer processing messages from a Redis Stream using a consumer group.
- Scheduled Publisher: Automatically publishes a message containing a UUID, counter, and timestamp to the stream
my-streamevery 5 seconds. (SeeDataPublisher.java) - Stream Consumer: Reads and logs messages from
my-streamusing the consumer groupmy-groupand consumer nameconsumer-1. Acknowledgment is handled automatically by the container. (SeeDataConsumer.javaandRedisConfig.java) - Consumer Group Management: Automatically attempts to create the
my-groupconsumer group on startup if it doesn't exist. - Spring Boot Integration: Leverages Spring Data Redis (
StringRedisTemplate,StreamMessageListenerContainer) for seamless integration with Redis. - Gradle Build: Uses Gradle for dependency management and building the project.
- Docker Support: Includes
docker-compose.ymlto easily start a Redis instance and aDockerfileto containerize the application.
- Java Development Kit (JDK) 21 or later (as per
Dockerfile) - Gradle 8.x or later (or use the included Gradle wrapper
./gradlew) - Docker and Docker Compose (for running Redis easily)
The easiest way to run Redis is using the provided Docker Compose file:
docker-compose up -d redisThis command will start a Redis container in the background, listening on port 6379.
You can run the Spring Boot application directly using Gradle:
- Clone the repository:
git clone <your-repository-url> cd redis-streams-demo
- Run the application:
./gradlew bootRun
The application will start, connect to the Redis instance (ensure it's running at localhost:6379 as per application.properties), create the consumer group my-group on the stream my-stream if needed, and begin publishing/consuming messages. Check the application logs to see the published message IDs and the consumed message details.
- Start Redis (if not already running):
docker-compose up -d redis
- Build the application JAR:
./gradlew bootJar
- Build the Docker image:
docker build -t redis-streams-demo-app . - Run the application container:
Note: You might need to adjust the network name (
# Make sure the app container can reach the redis container # This command connects it to the same network created by docker-compose docker run --rm --name redis-streams-app --network redis-streams-demo_redis-net -p 8080:8080 redis-streams-demo-app
redis-streams-demo_redis-net) based on your project directory name. You may also need to configure the application inside the container to connect toredis(the service name indocker-compose.yml) instead oflocalhost. This can be done via environment variables when running the container:docker run --rm --name redis-streams-app --network redis-streams-demo_redis-net -p 8080:8080 -e SPRING_DATA_REDIS_HOST=redis redis-streams-demo-app
- Redis Connection: Configure Redis host and port in
src/main/resources/application.properties. Default islocalhost:6379. - Stream Details: The stream key (
my-stream), consumer group (my-group), and consumer name (consumer-1) are configured insrc/main/resources/application.properties. - Consumer Setup: The
StreamMessageListenerContainerand subscription logic are defined insrc/main/java/com/example/demo/config/RedisConfig.java. - Publisher Schedule: The publishing interval (5000ms) is set via
@Scheduledinsrc/main/java/com/example/demo/publisher/DataPublisher.java.
redis-streams-demo/
├── build/ # Build output
├── gradle/ # Gradle wrapper files
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/ # Main application code
│ │ │ ├── config/ # RedisConfig.java (Stream listener setup)
│ │ │ ├── consumer/ # DataConsumer.java (Message handler)
│ │ │ ├── model/ # DummyData.java (Data model)
│ │ │ └── publisher/ # DataPublisher.java (Scheduled publisher)
│ │ └── resources/ # application.properties, static/, templates/
│ └── test/ # Test code
├── .gitattributes
├── .gitignore
├── build.gradle # Gradle build script
├── docker-compose.yml # Docker Compose configuration for Redis ONLY
├── Dockerfile # Dockerfile for the Spring Boot application
├── gradlew # Gradle wrapper script (Linux/macOS)
├── gradlew.bat # Gradle wrapper script (Windows)
├── HELP.md
├── README.md # This file
└── settings.gradle