發布/訂閱消息傳遞是許多軟件體系結構的重要組成部分。 某些軟件系統要求消息傳遞解決方案提供高性能,可伸縮性,隊列持久性和持久性,故障轉移支持,事務以及許多其他令人敬畏的功能,在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