1.Maven引用
< 依存性> < のgroupId > org.springframework.boot </ のgroupId > < たartifactId >ばねブートスタータウェブ</ たartifactId > </ 依存> < 依存性> < のgroupId > org.springframework.boot </ のgroupId > < たartifactId >スプリングブートスタータデータRedisの</ たartifactId > </ 依存>
2.redis属性の設定
spring.redis.database = 0 spring.redis.host = 127.0。0.1 spring.redis.port = 6379 spring.redis.password = ****** はserver.port = 5555
3.設定傍受関連オブジェクト
3.1オブジェクトに答えます
RedisReceiverは、通常のクラスであるか、または通常のクラス、受信したメッセージを受信しただけ、無チャンネル名として書き込まのMessageListenerを継承することができます
パッケージcom.example.redistest.config; インポートorg.springframework.data.redis.connection.Message; インポートorg.springframework.data.redis.connection.MessageListener; インポートorg.springframework.stereotype.Component; @Component パブリック クラスRedisReceiver { 公共 ボイドreceiveMessage(文字列メッセージ){ // TODOは、本明細書に受信メッセージチャネルの後に行われる工程である のSystem.out.println(メッセージ); } }
継承MessageListenerのは、メッセージ本体とチャンネル名を取得することができます。
パッケージcom.example.redistest.config。 輸入org.springframework.data.redis.connection.Message。 輸入org.springframework.data.redis.connection.MessageListener。 輸入org.springframework.stereotype.Component。 @Component パブリック クラス RedisReceiverを実装するMessageListener { @Override 公共 ボイドのonMessage(メッセージ・メッセージ、バイト[]パターン){ System.out.printlnは(新しい文字列(message.getBody()))。 System.out.println(新しい文字列(message.getChannel())); } }
3.2モニタアダプタの設定、メッセージ・リスナー・コンテナ
container.addMessageListener(listenerAdapter、新しいPatternTopic( "チャンネル:テスト" ));
メッセージリスナーがコンテナメッセージを高めるためにリッスンし、最初のパラメータが2番目のパラメータがチャネルをリッスンして、アダプタを聞いています。
パッケージcom.example.redistest.config。 輸入org.springframework.cache.annotation.EnableCaching。 輸入org.springframework.context.annotation.Bean。 輸入org.springframework.context.annotation.Configuration。 輸入org.springframework.data.redis.connection.MessageListener。 輸入org.springframework.data.redis.connection.RedisConnectionFactory; 輸入org.springframework.data.redis.core.StringRedisTemplate。 輸入org.springframework.data.redis.listener.PatternTopic; 輸入org.springframework.data.redis.listener.RedisMessageListenerContainer。 インポートorg.springframework.data.redis.listener.adapter.MessageListenerAdapter; @Configuration @EnableCaching パブリック クラスRedisCacheConfig { @Bean RedisMessageListenerContainer容器(RedisConnectionFactoryはconnectionFactory、 MessageListenerAdapter listenerAdapter){ RedisMessageListenerContainerコンテナ = 新しいRedisMessageListenerContainer()。 container.setConnectionFactory(はconnectionFactory)。 // 可以添加多个のMessageListener、配置不同的交换机 container.addMessageListener(listenerAdapter、新しい PatternTopic( "チャンネル:テスト" )); 戻り値のコンテナ。 } @Bean MessageListenerAdapter listenerAdapter(RedisReceiverレシーバ){ するSystem.out.println( "消息适配器1" )。 返す 新しい MessageListenerAdapter(受信機、 "のonMessage" ); } @Bean StringRedisTemplateテンプレート(RedisConnectionFactoryはconnectionFactory){ 戻り 新しいStringRedisTemplate(はconnectionFactory)を、 } }
メッセージング3.3
パッケージcom.example.redistest.controller。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.data.redis.core.RedisTemplate。 輸入org.springframework.data.redis.core.StringRedisTemplate。 輸入org.springframework.data.redis.core.ValueOperations。 輸入org.springframework.stereotype.Controller。 輸入org.springframework.web.bind.annotation.PathVariable; 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入java.util.Date; @RequestMapping( "/ Redisの" ) @Controller パブリック クラスRedisController { @Autowired StringRedisTemplateテンプレート。 / ** *发布消息 * * @paramのID * @return * / @RequestMapping( "/のsendMessage / {ID}" ) パブリック文字列のsendMessage(@PathVariable文字列ID){ ため(int型 i = 1; iは<= 5。私は++ ){ template.convertAndSend( :、String.Format の( "我是消息{%のD}号:%TT" "テストチャネル"、I、新しい日付()))を、 } リターン「」。 } }
テスト
郵便配達访问ます。http:// localhostを:5555 / Redisの/のsendMessage / 1
印刷されたメッセージを受信した後