崛起於Springboot2.X + redis消息隊列(52)

《SpringBoot2.X心法總綱》redis

      我以爲redis消息隊列不太好,雖然有消息隊列的功能,也能作延遲,可是不建議使用redis作消息隊列。spring

一、pom文件

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

二、application.properties

spring.redis.database=15
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=10000ms

三、實體類

/**
 * @Author:MuJiuTian
 * @Description: 全局topicname的定義
 * @Date: Created in 上午10:54 2019/9/26
 */
public class TopicName {

    public static String topic_name_test = "loving";
}

四、Service層(生產者)

@Service
public class PublishService {

    @Autowired
    StringRedisTemplate redisTemplate;

    public void sendMessage(String channel, Object message){
        redisTemplate.convertAndSend(channel, message);
    }
}

五、Controller層

/**
 * @Author:MuJiuTian
 * @Description: 測試redis消息隊列
 * @Date: Created in 下午1:49 2019/9/10
 */
@RestController
@RequestMapping(value = "/mq")
public class RedisMqController {

    @Autowired
    PublishService publishService;

    @GetMapping(value = "/sendMessage")
    public String sendMessage(){
        publishService.sendMessage(TopicName.topic_name_test,"i love you aoxin");
        return "SUCCESS";
    }
}

六、配置類

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;


@Configuration
@AutoConfigureAfter({Receiver.class})
public class SubscriberConfig {

    /**
     * 綁定消息監聽者和接收監聽的方法
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(Receiver receiver){
        //若是使用此方法,那麼Receiver則  implements MessageListener
        return new MessageListenerAdapter(receiver);
        
        // 若是使用此方法,那麼Receiver則 不用implements MessageListener,能夠自定義方法處理消息,下面的第二個參數爲方法名
        //return new MessageListenerAdapter(receiver,"receiveMessage");
    }

    /**
     * 建立消息監聽容器
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(TopicName.topic_name_test));
        return redisMessageListenerContainer;
    }
}

七、消費者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @Author:MuJiuTian
 * @Description: 消息接受而後處理
 * @Date: Created in 下午1:57 2019/9/10
 */
@Component
public class Receiver implements MessageListener{


    @Autowired
    StringRedisTemplate redisTemplate;
    
   /* public void receiveMessage(String message) {
        System.out.println(message);
    }*/

    @Override
    public void onMessage(Message message, byte[] bytes) {
        RedisSerializer<String> valueSerializer = redisTemplate.getStringSerializer();
        String deserialize = valueSerializer.deserialize(message.getBody());
        System.out.println(deserialize);
    }
}

八、測試

相關文章
相關標籤/搜索