Git Product home page Git Product logo

gs-messaging-redis's Introduction

This guide walks you through the process of using Spring Data Redis to publish and subscribe to messages sent with Redis.

What You Will Build

You will build an application that uses StringRedisTemplate to publish a string message and has a POJO subscribe for the message by using MessageListenerAdapter.

Note
It may sound strange to be using Spring Data Redis as the means to publish messages, but, as you will discover, Redis provides not only a NoSQL data store but a messaging system as well.

Setting up the Redis server

Before you can build a messaging application, you need to set up the server that will handle receiving and sending messages. https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/docker_compose_support.adoc

If you choose to run the Redis server yourself instead of using Spring Boot Docker Compose support, you have a few options: - Download the server and manually run it - Install with Homebrew, if you use a Mac - Manually run the compose.yaml file with docker compose up

If you go with any of these alternate approaches, you should remove the spring-boot-docker-compose dependency from the Maven or Gradle build file. You also need to add configuration to an application.properties file, as described in greater detail in the [_preparing_to_build_the_application] section. As mentioned earlier, this guide assumes that you use Docker Compose support in Spring Boot, so additional changes to application.properties are not required at this point.

Starting with Spring Initializr

You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

To manually initialize the project:

  1. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. Click Dependencies and select Spring Data Redis and Docker Compose Support.

  4. Click Generate.

  5. Download the resulting ZIP file, which is an archive of an application that is configured with your choices.

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

Create a Redis Message Receiver

In any messaging-based application, there are message publishers and messaging receivers. To create the message receiver, implement a receiver with a method to respond to messages, as the following example (from src/main/java/com/example/messagingredis/Receiver.java) shows:

link:complete/src/main/java/com/example/messagingredis/Receiver.java[role=include]

The Receiver is a POJO that defines a method for receiving messages. When you register the Receiver as a message listener, you can name the message-handling method whatever you want.

Note
For demonstration purposes, the receiver is counting the messages received. That way, it can signal when it has received a message.

Register the Listener and Send a Message

Spring Data Redis provides all the components you need to send and receive messages with Redis. Specifically, you need to configure:

  • A connection factory

  • A message listener container

  • A Redis template

You will use the Redis template to send messages, and you will register the Receiver with the message listener container so that it will receive messages. The connection factory drives both the template and the message listener container, letting them connect to the Redis server.

This example uses Spring Boot’s default RedisConnectionFactory, an instance of JedisConnectionFactory that is based on the Jedis Redis library. The connection factory is injected into both the message listener container and the Redis template, as the following example (from src/main/java/com/example/messagingredis/MessagingRedisApplication.java) shows:

link:complete/src/main/java/com/example/messagingredis/MessagingRedisApplication.java[role=include]

The bean defined in the listenerAdapter method is registered as a message listener in the message listener container defined in container and will listen for messages on the chat topic. Because the Receiver class is a POJO, it needs to be wrapped in a message listener adapter that implements the MessageListener interface (which is required by addMessageListener()). The message listener adapter is also configured to call the receiveMessage() method on Receiver when a message arrives.

The connection factory and message listener container beans are all you need to listen for messages. To send a message, you also need a Redis template. Here, it is a bean configured as a StringRedisTemplate, an implementation of RedisTemplate that is focused on the common use of Redis, where both keys and values are String instances.

The main() method kicks off everything by creating a Spring application context. The application context then starts the message listener container, and the message listener container bean starts listening for messages. The main() method then retrieves the StringRedisTemplate bean from the application context and uses it to send a Hello from Redis! message on the chat topic. Finally, it closes the Spring application context, and the application ends.

Run the Application

You can run the main method through your IDE. Note that, if you have cloned the project from the solution repository, your IDE may look in the wrong place for the compose.yaml file. You can configure your IDE to look in the correct place or you could use the command line to run the application. The ./gradlew bootRun and ./mvnw spring-boot:run commands launch the application and automatically find the compose.yaml file.

You should see the output:

yyyy-mm-ddT07:08:48.646-04:00  INFO 18338 --- [main] c.e.m.MessagingRedisApplication: Sending message...
yyyy-mm-ddT07:08:48.663-04:00  INFO 18338 --- [container-1] com.example.messagingredis.Receiver      : Received <Hello from Redis!>

Preparing to Build the Application

To run the code without Spring Boot Docker Compose support, you need a version of Redis running locally. To do this, you can use Docker Compose, but you must first make two changes to the compose.yaml file. First, modify the ports entry in compose.yaml to be '6379:6379'. Second, add a container_name.

The compose.yaml should now be:

services:
  redis:
    container_name: 'guide-redis'
    image: 'redis:latest'
    ports:
      - '6379:6379'

You can now run docker compose up to start the Redis server. Now you should have an external Redis server that is ready to accept requests. You can rerun the application and see the same output using your external Redis server.

Note
No configuration is required in the application.properties file because the default values match the Redis server configuration in compose.yaml. Specifically, the properties spring.data.redis.host and spring.data.redis.port default to localhost and 6379 respectively. More information about connecting to Redis can be found in the Spring Boot documentation.

Summary

Congratulations! You have just developed a publish-and-subscribe application with Spring and Redis.

gs-messaging-redis's People

Contributors

bclozel avatar btalbott avatar buzzardo avatar cbeams avatar christophstrobl avatar gregturn avatar habuma avatar robertmcnees avatar royclarkson avatar sdeleuze avatar spring-operator avatar thomsound avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gs-messaging-redis's Issues

Missing dependencies

The gs-messaging-redis guide has missing dependencies.

 java.lang.ClassNotFoundException: redis.clients.util.Pool

When I add the missing dependency

    <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.1</version>
        </dependency>

I get another dependency error, that I did not investigate further:

java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool$Config

container thread : The name of the thread has been accumulating counting.

I use this example in our project. However, some problems occurred during usage, CPU consumption suddenly increased to 100%, memory consumption suddenly increased. It is found that the thread created by RedisMessageListenerContainer has been increasing and has not been released. This is very deadly. I want to know if I'm writing something wrong, or if I have a way to avoid it and optimize it.

container-302230 : like this has backlog thousands or tens of thousands.

TPS 1000/s

`
@configuration
public class SubscriberConfig {
/**
* 创建连接工厂
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter realDataListener){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(realDataListener,new PatternTopic(RedisTopicUtil.TOPIC_REAL_DATA));
return container;
}

/**
 * 绑定消息监听者和接收监听的方法
 * @param receiver
 * @return
 */
@Bean
public MessageListenerAdapter realDataListener(RedisTopicReceiver redisTopicReceiver){
    return new MessageListenerAdapter(redisTopicReceiver,"realDataListener");
}


/**
 * 注册订阅者
 * @param latch
 * @return
 */
@Bean
public RedisTopicReceiver redisTopicReceiver(){
	return new RedisTopicReceiver();
}

}

public class RedisTopicReceiver {

private static final Logger LOGGER = LoggerFactory.getLogger(RedisTopicReceiver.class);



@Autowired
public RedisTopicReceiver() {
}
/**
 * 
 * <p>Title: realDataListener</p>  
 * <p>Description:读取接收到的实时数据 </p>  
 * @param message
 */
public void realDataListener(String message) {
    LOGGER.info("Received realDataListener :<" + message + ">");
  
}

}
`

Remove reference to Redis license

With the Redis license changes, this statement is no longer accurate Redis is an open source, BSD-licensed, key-value data store that also comes with a messaging system. I think we should just be able to remove this sentence.

@RedisListener

Does spring boot provide something like @JmsListener,@RabbitListener to support redis when use as messaging

Application hangs for me due to race condition

When I run the code as is, it never exits main, because the latch.await() is never satisfied. Using redis-cli monitor command, I see that the publish occurs before the subscription.

1581214604.709415 [0 127.0.0.1:61178] "PUBLISH" "chat" "Hello from Redis!" 1581214604.733783 [0 127.0.0.1:61179] "PSUBSCRIBE" "chat"
I confirmed that the container is alive and running before the send. I did not try to find a way to be notified when the subscribe is actually made. A sleep before the send was enough to change the behavior on my machine, but that's certainly not a true fix.

Some info about my setup:

MBP A1398 running OS X 10.12.6
Java 1.8.0_131
Spring Boot 2.2.4
Eclipse 2019-12 (I am running the app via Run As...)
Redis 3.2.0 (installed with brew)

Can I use Spring Data Redis for updating Spring Boot application running on multiple pods in EKS cluster ?

Hi, I am new to Spring Data Redis. Actually, I am working on a Spring boot application, where my requirement is something like the following:

On update of certain DB tables through an API, I would like to publish and "REFRESH" event to REDIS. I have 3 pods in the AWS EKS cluster and in each of the POD, my Spring Boot application is running. So, I would like each of these PODs will receive the event and refresh beans associated with the table.

I have been able to achieve this by running a scheduler at a fixed rate inside the application instance running on each POD, which is working fine.

But I would like to do this in an event-triggered way. So in this context, I found Spring Data Redis. But I am not sure, it would suffice my purpose.

Could anyone please help here with some pointers? Thanks.

Upgrade Spring Boot to the latest version

Update the guide to use the most recent Spring Boot version.

Files that require changes are:

initial/build.gradle
initial/pom.xml
complete/build.gradle
complete/pom.xml

Add guide on configuring port and hostname

I am not familiar with spring framework. I tried to use setHostname/setPort or RedisStandaloneConfiguration to configure host/port, but it always reported error ...
I think as guide for spring, it will be beneficial adding example on configuring host/port instead of using the default config. Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.