Spring-data-redis配置 訂閱/發佈 功能

Spring-data-redis配置 訂閱/發佈 功能java


1、添加maven依賴redis

<!-- redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.0.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
<scope>provided</scope>
</dependency>
<!-- redis -->

2、redis.propertiesspring

##applicationContext-redis.xml
redis.ip=192.168.1.156
redis.port=6379 
redis.timeout=5000
redis.maxIdle=10
redis.minIdle=1
redis.maxTotal=30 
redis.maxWaitMillis=5000  
#testOnBorrow true 指明是否在從池中取出鏈接前進行檢驗,若是檢驗失敗,則從池中去除鏈接並嘗試取出另外一個
redis.testOnBorrow=true

3、applicationContext-redis-pub-sub-demo.xmlapache

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:redis="http://www.springframework.org/schema/redis"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						http://www.springframework.org/schema/jee
						http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-4.0.xsd
						http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-4.0.xsd
                        http://www.springframework.org/schema/redis
        http://www.springframework.org/schema/redis/spring-redis-1.0.xsd"
	default-lazy-init="true">
<description>redis 發佈 定閱 示例</description>
<context:property-placeholder location="classpath*:redis.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="timeout" value="${redis.timeout}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
 
 
 <bean id="listener" class="cn.com.easy.redis.RedisMessageListenerService" />

	<redis:listener-container connection-factory="jedisConnectionFactory">
		<!-- the method attribute can be skipped as the default method name is 
			"handleMessage" -->
			<!-- topic表明監聽的通道,是一個正規匹配 -->
		<redis:listener ref="listener" method="handleMessage"
			topic="*" />
	</redis:listener-container>
 
 
</beans>


4、接收端  RedisMessageListenerService.javaapp

package cn.com.easy.redis;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 訂閱服務類
 * 
 * @author nibili 2015年5月14日
 * 
 */
public class RedisMessageListenerService {
@SuppressWarnings("resource")
public static void main(String[] args) {
new ClassPathXmlApplicationContext("/applicationContext-redis-pub-sub-demo.xml");
while (true) { // 這裏是一個死循環,目的就是讓進程不退出,用於接收發布的消息
try {
System.out.println("current time: " + new Date());
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void handleMessage(Serializable message) {
// 什麼都不作,只輸出
if (message == null) {
System.out.println("null");
} else if (message.getClass().isArray()) {
System.out.println(Arrays.toString((Object[]) message));
} else if (message instanceof List<?>) {
System.out.println(message);
} else if (message instanceof Map<?, ?>) {
System.out.println(message);
} else {
System.out.println(ToStringBuilder.reflectionToString(message));
}
}
}

5、發送端maven

package cn.com.easy.redis;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 發佈消息測試
 * 
 * @author nibili 2015年5月14日
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-redis-demo.xml")
public class RedisPubTest {
@Resource(name = "redisTemplate")
private RedisTemplate<String, String> template;
/**
 * 發佈消息
 * 
 * @auth nibili 2015年5月14日
 */
@Test
public void pub() {
template.convertAndSend("java", "java我發佈的消息!");
}
}
相關文章
相關標籤/搜索