溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用redis做消息隊列有用嗎

發(fā)布時間:2021-06-23 10:23:31 來源:億速云 閱讀:188 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“用redis做消息隊列有用嗎”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“用redis做消息隊列有用嗎”吧!

      我覺得redis消息隊列不太好,雖然有消息隊列的功能,也能做延遲,但是不建議使用redis做消息隊列。

1、pom文件
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、application.properties
spring.redis.database=15
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=10000ms
3、實體類
/**
 * @Author:MuJiuTian
 * @Description: 全局topicname的定義
 * @Date: Created in 上午10:54 2019/9/26
 */
public class TopicName {

    public static String topic_name_test = "loving";
}
4、Service層(生產(chǎn)者)
@Service
public class PublishService {

    @Autowired
    StringRedisTemplate redisTemplate;

    public void sendMessage(String channel, Object message){
        redisTemplate.convertAndSend(channel, message);
    }
}
5、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";
    }
}
6、配置類
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 {

    /**
     * 綁定消息監(jiān)聽者和接收監(jiān)聽的方法
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(Receiver receiver){
        //如果使用此方法,那么Receiver則  implements MessageListener
        return new MessageListenerAdapter(receiver);
        
        // 如果使用此方法,那么Receiver則 不用implements MessageListener,可以自定義方法處理消息,下面的第二個參數(shù)為方法名
        //return new MessageListenerAdapter(receiver,"receiveMessage");
    }

    /**
     * 創(chuàng)建消息監(jiān)聽容器
     */
    @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;
    }
}
7、消費者
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);
    }
}
8、測試

用redis做消息隊列有用嗎

用redis做消息隊列有用嗎

到此,相信大家對“用redis做消息隊列有用嗎”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI