使用Spring Redis發布/訂閱

繼續發現功能強大的Redis功能集,值得一提的是對發布/訂閱消息的開箱即用支持。

發布/訂閱消息傳遞是許多軟件體系結構的重要組成部分。 某些軟件系統要求消息傳遞解決方案提供高性能,可伸縮性,隊列持久性和持久性,故障轉移支持,事務以及許多其他令人敬畏的功能,在Java世界中,大多數情況下總是導致使用JMS實現之一提供者。 在我以前的項目中,我一直積極使用Apache ActiveMQ (現在轉向Apache ActiveMQ Apollo )。 盡管這是一個很好的實現,但有時我只需要簡單的排隊支持,而Apache ActiveMQ看上去就顯得過于復雜了。

備擇方案? 請歡迎Redis pub / sub! 如果您已經在使用Redis作為鍵/值存儲,那么很少的其他配置行將立即將發布/訂閱消息傳遞到您的應用程序。

Spring Data Redis項目很好地抽象了Redis pub / sub API,并為使用Spring功能與JMS集成的每個人提供了如此熟悉的模型。

與往常一樣,讓我們??從POM配置文件開始。 這是相當小的,簡單的,包括必要的春天的依賴, 春天Redis的數據和Jedis ,偉大的Java客戶端Redis的 。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelversion>4.0.0</modelversion><groupid>com.example.spring</groupid><artifactid>redis</artifactid><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceencoding>UTF-8</project.build.sourceencoding><spring.version>3.1.1.RELEASE</spring.version></properties><dependencies><dependency><groupid>org.springframework.data</groupid><artifactid>spring-data-redis</artifactid><version>1.0.1.RELEASE</version></dependency><dependency><groupid>cglib</groupid><artifactid>cglib-nodep</artifactid><version>2.2</version></dependency><dependency><groupid>log4j</groupid><artifactid>log4j</artifactid><version>1.2.16</version></dependency><dependency><groupid>redis.clients</groupid><artifactid>jedis</artifactid><version>2.0.0</version><type>jar</type></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-core</artifactid><version>${spring.version}</version></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-context</artifactid><version>${spring.version}</version></dependency></dependencies><build><plugins><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><version>2.3.2</version><configuration><source>1.6<target>1.6</target></configuration></plugin></plugins></build>
</project>

繼續配置Spring上下文,讓我們了解為了使發布者發布一些消息并讓消費者使用它們而需要的內容。 了解有關JMS的相應Spring抽象將對此有很大幫助。

  • 我們需要連接工廠-> JedisConnectionFactory
  • 我們需要一個模板供發布者發布消息-> RedisTemplate
  • 我們需要一個消息監聽器供使用者使用消息-> RedisMessageListenerContainer

使用Spring Java配置,讓我們描述一下上下文:

package com.example.redis.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;import com.example.redis.IRedisPublisher;
import com.example.redis.impl.RedisMessageListener;
import com.example.redis.impl.RedisPublisherImpl;@Configuration
@EnableScheduling
public class AppConfig {@BeanJedisConnectionFactory jedisConnectionFactory() {return new JedisConnectionFactory();}@BeanRedisTemplate< String, Object > redisTemplate() {final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();template.setConnectionFactory( jedisConnectionFactory() );template.setKeySerializer( new StringRedisSerializer() );template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );return template;}@BeanMessageListenerAdapter messageListener() {return new MessageListenerAdapter( new RedisMessageListener() );}@BeanRedisMessageListenerContainer redisContainer() {final RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory( jedisConnectionFactory() );container.addMessageListener( messageListener(), topic() );return container;}@BeanIRedisPublisher redisPublisher() {return new RedisPublisherImpl( redisTemplate(), topic() );}@BeanChannelTopic topic() {return new ChannelTopic( 'pubsub:queue' );}
}

非常簡單明了。 @EnableScheduling批注不是必需的,并且僅對于我們的發布者實現是必需的:發布者將每100毫秒發布一次字符串消息。

package com.example.redis.impl;import java.util.concurrent.atomic.AtomicLong;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.scheduling.annotation.Scheduled;import com.example.redis.IRedisPublisher;public class RedisPublisherImpl implements IRedisPublisher {private final RedisTemplate< String, Object > template;private final ChannelTopic topic; private final AtomicLong counter = new AtomicLong( 0 );public RedisPublisherImpl( final RedisTemplate< String, Object > template, final ChannelTopic topic ) {this.template = template;this.topic = topic;}@Scheduled( fixedDelay = 100 )public void publish() {template.convertAndSend( topic.getTopic(), 'Message ' + counter.incrementAndGet() + ', ' + Thread.currentThread().getName() );}
}

最后是我們的消息偵聽器實現(僅在控制臺上打印消息)。

package com.example.redis.impl;import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;public class RedisMessageListener implements MessageListener {@Overridepublic void onMessage( final Message message, final byte[] pattern ) {System.out.println( 'Message received: ' + message.toString() );}
}

太棒了,只有兩個小類,一個配置用于將事物連接在一起,并且我們的應用程序中具有完整的發布/訂閱消息支持! 讓我們以獨立方式運行應用程序…

package com.example.redis;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.example.redis.config.AppConfig;public class RedisPubSubStarter {public static void main(String[] args) {new AnnotationConfigApplicationContext( AppConfig.class );}
}

…并在控制臺中查看以下輸出:

...
Message received: Message 1, pool-1-thread-1
Message received: Message 2, pool-1-thread-1
Message received: Message 3, pool-1-thread-1
Message received: Message 4, pool-1-thread-1
Message received: Message 5, pool-1-thread-1
Message received: Message 6, pool-1-thread-1
Message received: Message 7, pool-1-thread-1
Message received: Message 8, pool-1-thread-1
Message received: Message 9, pool-1-thread-1
Message received: Message 10, pool-1-thread-1
Message received: Message 11, pool-1-thread-1
Message received: Message 12, pool-1-thread-1
Message received: Message 13, pool-1-thread-1
Message received: Message 14, pool-1-thread-1
Message received: Message 15, pool-1-thread-1
Message received: Message 16, pool-1-thread-1
...

大! Redis pub / sub可以做很多事情, Redis官方網站上提供了出色的文檔 。

參考:我們的JCG合作伙伴 Andrey Redko在Andriy Redko {devmind}博客上使用Spring從Redis發布/ 訂閱 。


翻譯自: https://www.javacodegeeks.com/2012/10/redis-pubsub-using-spring.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371223.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371223.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371223.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python在律師上作中的實例_python-基礎面試題

深拷貝1.對象A拷貝&#xff0c;生成對象B&#xff0c;且我們修改對象B(對象A)中的數據或方法&#xff0c;對象A(對象B)不會受影響&#xff0c;這就是深拷貝2.對于可變與不可變類型對于不可變類型&#xff0c;深拷貝會和淺拷貝一樣&#xff0c;拷貝的是引用&#xff0c;不會創建…

2017 校招華為上機題

1. 給定一個字符串&#xff0c;把字符串內的字母轉換成該字母的下一個字母&#xff0c; a 換成b&#xff0c;z 換成a&#xff0c;Z 換成A&#xff0c;如aBf 轉換成bCg&#xff0c;字符串內的其他字符不改變&#xff0c;給定函數&#xff0c;編寫函數void Stringchang&#xff0…

JSON –拯救杰克遜

有時您必須使用JavaScript從服務器中獲取一些數據&#xff0c; JSON是完成此任務的不錯選擇。 讓我們玩一下JPA揭秘&#xff08;第1集&#xff09;-OneToMany和ManyToOne映射中的“雇主-雇員-福利”示例。 我們將在基于Spring Framework的Web應用程序中使用它。 我們的第一個…

maven 使用記錄之修改 maven默認jdk版本

maven package執行的時候會遇到jdk版本不對的問題 &#xff1a;原因是 maven所指定的jdk版本與項目使用的jdk版本不一致1.項目屬性的 java compiler可以設置2.直接修改 maven 的 settings.xml 一勞永逸settiings.xml <profiles>標簽內加入<profile> <id>j…

java默認值_Java中八種基本數據類型的默認值

通過一段代碼來測試一下 8種基本數據類型的默認值package dierge;public class Ceshi {int a;double b;boolean c;char d;float f;byte e;long h;short j;public static void main(String args[]){Ceshi anew Ceshi();System.out.println("整型的默認值是&#xff1a;&quo…

HDU - 1024 Max Sum Plus Plus 最大m段子段和+滾動數組優化

給定n個數字&#xff0c;求其中m段的最大值&#xff08;段與段之間不用連續&#xff0c;但是一段中要連續&#xff09; 例如&#xff1a;2 5 1 -2 2 3 -1五個數字中選2個&#xff0c;選擇1和2 3這兩段。 dp[i][j]從前j個數字中選擇i段&#xff0c;然后根據第j個數字是否獨立成一…

JavaFX教程–基礎

JavaFX似乎正在RIA領域獲得發展。 有了正確的工具和開發支持&#xff0c;它肯定會在下一個最佳技術“物”上付出巨大的代價。 我沒有在這里寫任何JavaFX評論&#xff0c;因為有很多技術評論可能對它進行了廣泛的評論&#xff0c;但是&#xff0c;我將編寫一個簡單的教程&#x…

java script this_JavaScript this 關鍵字

JavaScript this 關鍵字面向對象語言中 this 表示當前對象的一個引用。但在 JavaScript 中 this 不是固定不變的&#xff0c;它會隨著執行環境的改變而改變。在方法中&#xff0c;this 表示該方法所屬的對象。如果單獨使用&#xff0c;this 表示全局對象。在函數中&#xff0c;…

trim函數的作用 $.trim(str)

去掉字符序列左邊和右邊的空格轉載于:https://www.cnblogs.com/dandeliongogo/p/6610890.html

php數據庫備份腳本

// 備份數據庫 $host "localhost"; $user "root"; //數據庫賬號 $password ""; //數據庫密碼 $dbname "mysql"; //數據庫名稱 // 這里的賬號、密碼、名稱都是從頁面傳過來的 if (!mysql_connect($host, $user, $password)) // 連接…

java swing 案例詳解_《Java Swing圖形界面開發與案例詳解》PDF_IT教程網

資源名稱&#xff1a;《Java Swing圖形界面開發與案例詳解》PDF內容簡介&#xff1a;《Java Swing圖形界面開發與案例詳解》全書共20章&#xff0c;其中第1&#xff5e;2章主要介紹有關Swing的基礎知識&#xff0c;包括Swing的基本概述、如何使用IDE開發Swing程序&#xff1b;第…

水晶球錯覺

我注意到人們有時會避免進行徹底的測試。 對于某些人來說&#xff0c;這聽起來像是偽造的&#xff0c;但是請聽我說……我確實理解為什么會這樣。 測試會產生被困的感覺&#xff0c;每引入一個新的測試&#xff0c;負擔就會加重。 建立穩定&#xff0c;無干擾且質量保證的測試套…

Python—day3

1、字符串在C里邊就是字符數組 Python里邊一切事物都是對象&#xff0c;對象則是類創建的 2、set集合 set是一個無序且不能重復的元素集合 #!/usr/bin/env python# encoding: utf-8#set對象不能有重復s1 set()s1.add(alex)print(s1)s1.add(alex)print(s1)s1.add(shidong)print…

iOS - The file “XXX.app” couldn’t be opened because you don’t have permission to view it.

當引入第三方的框架的時候 容易產生以下問題&#xff1a; The file “XXX.app” couldn’t be opened because you don’t have permission to view it. 如圖&#xff1a; 造成的原因&#xff1a; info文件中的字段Executable file 與 build settings欄中的Packaging中的Produc…

Google Guava v07范例

我們在TouK舉辦了一個名為“每周技術研討會”的活動&#xff0c;即每個星期五的16:00&#xff0c;每個愿意參加的人都有一個演講。 我們展示了我們在家學習和學習的東西&#xff0c;但是我們也設有一個公告板&#xff0c;上面有人們想聽的話題。 上周MaciejPrchniak談論了Cloju…

推薦一些經過實踐檢驗的學習方法

作者做了多年的Java培訓教師&#xff0c;也接觸過不少初學者&#xff0c;根據多年的教學互動經驗&#xff0c;總結了一些能少走彎路的學習方法&#xff0c;供大家參考。 第一&#xff0c;是要多學多練&#xff0c;這似乎是廢話&#xff0c;但真正能非常上心學習的人還真是少數&…

使JFrame透明

首先創建一個帶有滑塊的框架&#xff0c;該滑塊將用于設置透明度量。 import javax.swing.JFrame; import javax.swing.JSlider;public class TransparentFrame extends JFrame {public TransparentFrame() {setTitle(Transparent Frame);setSize(400,400);setDefaultCloseOper…

第一次作業之成員介紹

Lab205的新鮮血液 很理所當然的&#xff0c;實驗室的4枚“小鮮肉”在現代軟工的課程上組成了一個team&#xff0c;作為一個負責的team長&#xff0c;我當然要放上組員們的自述啦&#xff01;&#xff08;為什么不是他述&#xff0c;╭(╯^╰)╮&#xff0c;誰讓我是個傲嬌的組長…

java自定義分頁標簽_自定義分頁標簽--仿javaeye分頁效果

效果如圖&#xff1a;1、JSP規范1.1版本后增加了自定義標簽庫。實現自定義標簽的步驟(1)開發自定義標簽處理類。(2)建立*.tld文件。(3)在web.xml中增加自定義標簽的定義。(4)在jsp中使用自定義標簽。2、自定義標簽類(1)繼承javax.servlet.jsp.tagext.TagSupport(2)標簽類屬性&a…

Java隱藏代碼

不久前&#xff0c;我遇到了字符串中不可見字符的問題。 因為它們是不可見的&#xff0c;所以它們確實會引起混亂。 String a "Hello\u200e";String b "Hello\u200f";System.out.println(\ a " and " b " are length " a.length…