成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

最簡單的springboot2整合redis 10行代碼 完成發(fā)布和訂閱

songze / 2203人閱讀

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

相關文章

  • SpringBoot2.0之三 優(yōu)雅整合Spring Data JPA

    摘要:的配置后在其他低版本的中也有使用這種配置的,具體根據版本而定。等注解是的相關知識,后面的文章將詳細講述。 ??在我們的實際開發(fā)的過程中,無論多復雜的業(yè)務邏輯到達持久層都回歸到了增刪改查的基本操作,可能會存在關聯多張表的復雜sql,但是對于單表的增刪改查也是不可避免的,大多數開發(fā)人員對于這個簡單而繁瑣的操作都比較煩惱。 ??為了解決這種大量枯燥的簡單數據庫操作,大致的解決該問題的有三種方...

    ningwang 評論0 收藏0
  • SpringBoot2.0之四 簡單整合MyBatis

    摘要:從最開始的到后來的,到目前的隨著框架的不斷更新換代,也為我們廣大的程序猿提供了更多的方便,一起搭建一個從控制層到持久層的項目可能需要一兩天的時間,但是采用的方式,我們可能只需要分鐘就能輕松完成一個項目的搭建,下面我們介紹一下整合的方法一新建 ??從最開始的SSH(Struts+Spring+Hibernate),到后來的SMM(SpringMVC+Spring+MyBatis),到目前...

    Sanchi 評論0 收藏0
  • springboot 整合redis

    摘要:與整合默認使用的是,相較于,是一個可伸縮的,線程安全的客戶端。在處理高并發(fā)方面有更多的優(yōu)勢。使用依賴主要需要的依賴為配置配置使用與整合可以在不更改現有代碼邏輯的基礎上,通過增加注解的方式,實現緩存。 springboot2.0 與redis整合默認使用的是Lettuce,相較于jedis,lettuce 是一個可伸縮的,線程安全的redis客戶端。在處理高并發(fā)方面有更多的優(yōu)勢。 Red...

    elarity 評論0 收藏0

發(fā)表評論

0條評論

songze

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<