springboot2.0 integration redis publish and subscribe

1.Maven quote

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

2.redis attribute configuration

spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=******
server.port=5555

 

3. Set interception related objects

3.1 Answering objects

RedisReceiver may be an ordinary class or inherit the MessageListener, written as a normal class, only when the received message is received, no channel name
Package com.example.redistest.config; 

Import org.springframework.data.redis.connection.Message;
 Import org.springframework.data.redis.connection.MessageListener;
 Import org.springframework.stereotype.Component; 

@Component 
public  class RedisReceiver { 

    public  void receiveMessage (String message) {
         // the TODO herein is a process performed after the received message channels 
        System.out.println (message); 
    } 
}

Inheritance MessageListener, will be able to get the message body and channel name.

package com.example.redistest.config;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

@Component
public class RedisReceiver implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println(new String(message.getBody()));
        System.out.println(new String(message.getChannel()));
    }
}

3.2 monitor adapter configuration, the message listener container

container.addMessageListener(listenerAdapter, new PatternTopic("channel:test"));

Message listener listens to increase container message, the first parameter is listening adapter, the second parameter is listening channel.

package com.example.redistest.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@EnableCaching
public class RedisCacheConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 可以添加多个 messageListener,配置不同的交换机
        container.addMessageListener(listenerAdapter, new PatternTopic("channel:test"));
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(RedisReceiver receiver) {
        System.out.println("消息适配器1");
        return new MessageListenerAdapter(receiver, "onMessage");
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }

}

Messaging 3.3

package com.example.redistest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@RequestMapping("/redis")
@Controller
public class RedisController {

    @Autowired
    StringRedisTemplate template;

    /**
     * 发布消息
     *
     * @param id
     * @return
     */
    @RequestMapping("/sendMessage/{id}")
    public String sendMessage(@PathVariable String id) {
        for(int i = 1; i <= 5; i++) {
            template.convertAndSend("channel:test", String.format("我是消息{%d}号: %tT", i, new Date()));
        }
        return "";
    }

}

test

postman访问http://localhost:5555/redis/sendMessage/1

 

After receiving a message printed

 

Guess you like

Origin www.cnblogs.com/powerwu/p/11505481.html