Spring-data-redis Configuring subscription/Publishing Features

Source: Internet
Author: User

Spring-data-redis Configuring subscription/Publishing Features


One, add maven dependency

<!--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--

Second, redis.properties

# #applicationContext-redis.xmlredis.ip=192.168.1.156redis.port=6379 redis.timeout=5000redis.maxidle= 10redis.minidle=1redis.maxtotal=30 redis.maxwaitmillis=5000 #testOnBorrow True Indicates whether the test is performed before the connection is removed from the pool, and if the test fails, Remove the connection from the pool and try to remove the other redis.testonborrow=true

Third, Applicationcontext-redis-pub-sub-demo.xml

<?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/beanshttp://www.springframework.org/ schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/ schema/jee/spring-jee-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/ aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/ Context/spring-context-4.0.xsdhttp://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  Publish   Read   examples </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}" &Nbsp;/><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 represents the channel for listening, is a regular match  --> <redis:listener ref= "Listener"  method= "Handlemessage" topic= "*"  /></redis: Listener-container>  </beans>


Iv. Receiving End Redismessagelistenerservice.java

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;/** *  subscription service class  *  *   @author  nibili 2015 May 14  *  */public class  redismessagelistenerservice {@SuppressWarnings ("resource") Public static void main (string[]  args)  {new classpathxmlapplicationcontext ("/applicationcontext-redis-pub-sub-demo.xml");   (true)  { //  here is a dead loop, the purpose of which is to let the process not exit, to receive the published message try {system.out.println ("Current time:   " + new date ()); Thread.Sleep (3000);}  catch  (interruptedexception e)  {e.printstacktrace ();}} Public void handlemessage (serializable message)  {//  do nothing, only output 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);} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN (tostringbuilder.reflectiontostring (message));}}}

V. Sending end

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;/** * Publish Message Test * * @author Nibili May 14, 2015 * * * * @RunWith (Springjunit4classrunner.class) @ContextConfiguration ("/applicationcontext-redis-demo.xml") public class redispubtest {@Resource (name = "Redistemplate") private redistemplate<string, string> template;/** * Post message * * @auth Nibili May 14, 2015 */@Testpublic Void Pub () {template.convertandsend ("Java", "Java I publish messages!");}}


Spring-data-redis Configuring subscription/Publishing features

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.