Import Package:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>
Production-side code: interface ellipsis
Package Jredis;
Import java.io.Serializable;
Import org.springframework.data.redis.core.RedisTemplate;
public class Redisdaoimpl implements redisdao{
private redistemplate<string, object> redistemplate = null;
Public Redisdaoimpl () {
}
@Override public
void SendMessage (String channel, Serializable message) {
Redistemplate.convertandsend (channel, message);
Public Redistemplate Getredistemplate () {return
redistemplate;
}
public void Setredistemplate (Redistemplate redistemplate) {
this.redistemplate = redistemplate;
}
}
Client code: for monitoring Redis
Package Jredis;
Import com.sun.tools.javac.util.List;
Import com.sun.xml.internal.xsom.impl.scd.Iterators;
Import Org.apache.commons.lang.builder.ToStringBuilder;
Import java.io.Serializable;
Import Java.util.Arrays;
public class Messagedelegatelistenerimpl implements Messagedelegatelistener {
@Override public
Void Handlemessage (Serializable message) {
//do nothing, output only
if (message = = null) {
System.out.println ("null");
} else if (Message.getclass (). IsArray ()) {
System.out.println (arrays.tostring ((object[)) message);
if (message instanceof list<?>) {
System.out.println (message);
} else if (message instanceof iterators.map<? ,?>) {
System.out.println (message);
} else {
System.out.println ( tostringbuilder.reflectiontostring (message));
SYSTEM.OUT.PRINTLN (message);}}
Start consumer end, consumer side test class:
package Jredis;
Import Org.junit.Before;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Java.util.Date;
public class Testredisconsumer {private Messagedelegatelistenerimpl messagedelegatelistener=null; @Before public void SetUp () throws Exception {ApplicationContext ApplicationContext = new Classpathxmlapplica
Tioncontext ("Spring-consumer-test.xml");
Messagedelegatelistener = (Messagedelegatelistenerimpl) applicationcontext.getbean ("MessageDelegateListener");
public static void Main (string[] args) {new Classpathxmlapplicationcontext ("Spring-consumer-test.xml");
SYSTEM.OUT.PRINTLN ("Consumer 1");
while (true) {//Here is a dead loop, the purpose is to keep the process from quitting, to receive the published message try {thread.sleep (3000);
catch (Interruptedexception e) {e.printstacktrace (); }
}
}
}
Production-Side test class:
Package Jredis;
Import Service.weibo.impl.TencentOauthV1BackgroundServiceImpl;
Import Service.weibo.impl.TencentOauthV1ForegroundServiceImpl;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testredisproduce {
private redisdaoimpl redisdao=null;
@Before public
void SetUp () throws Exception {
ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("Spring-service-test.xml");
Redisdao = (Redisdaoimpl) applicationcontext.getbean ("Redisdao");
}
@Test public
void Testpublishmessage () throws Exception {
String msg = "Hello, redis!";
Redisdao.sendmessage ("Java", msg); Publish string Message
integer[] values = new integer[]{21341,123123,12323};
Redisdao.sendmessage ("Java", values); Publish an array message
}
}
Production-side XML configuration:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:redis= "Http://www.springframework.org/schema/redis" xmlns:p= "http://www.springframework.org/schema/p" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/contex T/spring-context-3.0.xsd Http://www.springframework.org/schema/redis http://www.springframework.org/schema/ Redis/spring-redis-1.0.xsd "> <bean id=" redisconnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "P:hostname=" 10.25.172.174 "p:port=" 6379 " P:usepool= "true" > </bean> <!--redis template deFinition--> <bean id= "redistemplate" class= "Org.springframework.data.redis.core.RedisTemplate" P:conn ectionfactory-ref= "Redisconnectionfactory"/> <bean id= "Redisdao" class=. Redisdaoimpl "> <property name=" redistemplate "ref=" redistemplate "/> </bean> <bean id=" Listener "class=" Jredis. Messagedelegatelistenerimpl "/> <!--the default connectionfactory--> <bean id=" Jdkserializer "class= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <redis:listener-container > <!--the ' can be skipped as the ' default method name ' Handlemessage '--> < Redis:listener ref= "Listener" serializer= "Jdkserializer" method= "Handlemessage" topic= "java"/> <!--the name of the distribution channel--&
Gt </redis:listener-container> </beans>
Consumer-side XML configuration:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/co ntext http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id=" Jedisconnectionfa Ctory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "p:hostname=" 10.25.172.174 " P:port= "6379" p:usepool= "true" > </bean> <!--redis template definition--> <bean id= "Redis Template "class=" Org.springframework.data.redis.core.RedisTemplate "p:connectionfactory-ref=" jedisconnectionfact Ory "/> <bean id=" reDisdao "class=" Jredis. Redisdaoimpl "> <property name=" redistemplate "ref=" redistemplate "/> </bean> <bean id=" Serialization "class=" Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer/> <bean ID = "Messagedelegatelistener" class= "Jredis". Messagedelegatelistenerimpl "/> <bean id=" MessageListener "class=" Org.springframework.data.redis.listener.adapter.MessageListenerAdapter "> <property name=" Delegate "ref=" Mes Sagedelegatelistener "/> <property name= Serializer" ref= "serialization"/> </bean> <be An id= "Rediscontainer" class= "Org.springframework.data.redis.listener.RedisMessageListenerContainer" > <prop
Erty name= "ConnectionFactory" ref= "jedisconnectionfactory"/> <property name= "Messagelisteners" >
<!--map of listeners and their associated topics (channels Or/and patterns)--> <map> ,Entry key-ref= "MessageListener" > <bean class= "Org.springframework.data.redis.listener.ChannelTo" Pic > <constructor-arg value= "java"/> <!--where the consumer side is configured to subscribe to a channel that can be multiple. This example subscribe to Java this channel--> </bean> </entry> </map> & Lt;/property> </bean> </beans>
Disadvantages:
Because Spring-redis is client-based, if you deploy a client in a distributed deployment, several instances of deploying a few instances receive the same message. In most cases we do not need this. We only need one instance of one to process the message. In this case, does Redis offer any treatment? I have not found, welcome everyone to provide the program.