pom:
org.springframework.boot spring-boot-starter-data-redis
代碼:
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.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@Configuration
public class RedisSubListenerConfig {
@Bean
MessageListenerAdapter messageListener() {
//abstract methods overwrite
return new MessageListenerAdapter((MessageListener) (message, pattern) -> {
System.out.println("Message received: " + message.toString());
});
}
@Bean
RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container
= new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(messageListener(), topic());
return container;
}
@Bean
ChannelTopic topic() {
return new ChannelTopic("messageQueue");
}
}
測試類:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
/**
* @date 2019-05-25 10:59
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisTest {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Test
public void test(){
stringRedisTemplate.convertAndSend("messageQueue","hello world");
}
}
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.hztianpu.com/yun/74628.html
摘要:的配置后在其他低版本的中也有使用這種配置的,具體根據版本而定。等注解是的相關知識,后面的文章將詳細講述。 ??在我們的實際開發(fā)的過程中,無論多復雜的業(yè)務邏輯到達持久層都回歸到了增刪改查的基本操作,可能會存在關聯多張表的復雜sql,但是對于單表的增刪改查也是不可避免的,大多數開發(fā)人員對于這個簡單而繁瑣的操作都比較煩惱。 ??為了解決這種大量枯燥的簡單數據庫操作,大致的解決該問題的有三種方...
摘要:從最開始的到后來的,到目前的隨著框架的不斷更新換代,也為我們廣大的程序猿提供了更多的方便,一起搭建一個從控制層到持久層的項目可能需要一兩天的時間,但是采用的方式,我們可能只需要分鐘就能輕松完成一個項目的搭建,下面我們介紹一下整合的方法一新建 ??從最開始的SSH(Struts+Spring+Hibernate),到后來的SMM(SpringMVC+Spring+MyBatis),到目前...
摘要:與整合默認使用的是,相較于,是一個可伸縮的,線程安全的客戶端。在處理高并發(fā)方面有更多的優(yōu)勢。使用依賴主要需要的依賴為配置配置使用與整合可以在不更改現有代碼邏輯的基礎上,通過增加注解的方式,實現緩存。 springboot2.0 與redis整合默認使用的是Lettuce,相較于jedis,lettuce 是一個可伸縮的,線程安全的redis客戶端。在處理高并發(fā)方面有更多的優(yōu)勢。 Red...
閱讀 856·2021-09-28 09:35
閱讀 2657·2019-08-29 11:25
閱讀 2213·2019-08-23 18:36
閱讀 1927·2019-08-23 16:31
閱讀 2129·2019-08-23 14:50
閱讀 3202·2019-08-23 13:55
閱讀 3360·2019-08-23 12:49
閱讀 2173·2019-08-23 11:46