Using spring Data Redis operations Redis (ii)

Source: Internet
Author: User
Tags sub command

The previous article covers most of the topics for the spring Date Redis operations Redis, which describes the use of the subscription and publishing features of Redis in spring applications.

1. Redis's pub/sub command

The subscription and publishing Service for Redis is like 6 commands, with a brief description of each command below.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5C/2E/wKioL1Ub6AeygSswAAIwoSKArE4025.jpg "title=" Pubsub.png "width=" 580 "height=" 337 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:580PX;HEIGHT:337PX; "alt=" Wkiol1ub6aeygsswaaiwoskare4025.jpg "/>

Publish: Sends a message to the specified channel (channel)

Subscribe: Subscribe to the specified channel, you can subscribe to multiple

Psubscribe: Subscribe to a channel that specifies pattern (pattern, mode matching with channel name)

Unsubscribe: Cancel subscription to channel, cancel multiple subscriptions at once

Punsubscribe: Canceling a subscription with the specified pattern

PubSub: is an introspective command that looks at the status of a subscription and the publishing system, which consists of several sub-commands in different formats (see also:

HTTP://REDIS.IO/COMMANDS/PUBSUB)

In the SDR (Spring Data Redis) channel corresponds to the topic class, the top class is an interface with channel and pattern two implementation classes, each of which is a channel with the specified name and pattern matching. About subscription information is defined by the subscription interface.


2.Redis Message Listener Container Declaration and message Listener Registration

There are two ways to implement the declaration of a message listener container in SDR, one is to define a bean through a Redis namespace.

Here are mainly related to redismessagelistenercontainer,messagelisteneradapter,messagelistener several classes.

2.1 How to configure using the Redis namespace

<bean id= "Mdplistener" class= "Secondriver.spring.redis.MyMessageListener"/><bean id= "Mdelegatelistener" class= "Secondriver.spring.redis.DefaultMessageDelegate"/><redis:listener-container connection-factory= " Jedisconnectionfactory "><redis:listener ref=" Mdplistener "topic=" spring* "/><redis:listener ref=" Mdelegatelistener "method=" Handlemessage "topic=" CCTV5 cctv6 NBTV hello* "/></redis:listener-container>

Description

The definition of topic can be a specific channel name or pattern, and multiple channels (themes) are separated by a space.

This defines two listner,mymessagelistener implementations of the Messagealistener interface, Defaultmessagedelegate implements the Messagedelegate interface.

Mymessagelistener:

Package Secondriver.spring.redis;import Org.springframework.data.redis.connection.message;import Org.springframework.data.redis.connection.messagelistener;public class Mymessagelistener Implements MessageListener {@Overridepublic void onMessage (Message message, byte[] pattern) {System.out.println ("Channel:" + New String (Message.getchannel ()) + ", message:" + New String (Message.getbody ()));}}

Messagedelegate Interface:

Package Secondriver.spring.redis;import Java.io.serializable;import Java.util.map;public interface MessageDelegate { public void Handlemessage (String message);p ublic void Handlemessage (map<?,? > Message);p ublic void Handlemessage ( byte[] message);p ublic void Handlemessage (Serializable message);//Pass the Channel/pattern as wellpublic void handlemess Age (Serializable message, String Channel);}


Defaultmessagedelegate Type:

package secondriver.spring.redis;import java.io.serializable;import java.util.map;public  class defaultmessagedelegate implements messagedelegate {@Overridepublic  void  Handlemessage (string message)  {system.out.println ("Handlemessage (string message):"  +  message);} @Overridepublic  void handlemessage (map<?, ?> message)  {system.out.println (" Handlemessage (map<?, ?> message): " + message);} @Overridepublic  void handlemessage (byte[] message)  {system.out.println ("Handlemessage" (byte [] message): "+ new string (Message));} @Overridepublic  void handlemessage (serializable message)  {system.out.println (" Handlemessage (serializable message): "+ message.tostring ());} @Overridepublic  void handlemessage (Serializable message, string channel)  { System.out.println ("Handlemessage (Serializable&nbsP;message, string channel): "+ message.tostring ()  + ",  channel: " +  channel);}}

This way of defining message snooping is not dependent on Redis, which is designed as a message-driven POJOs (MDPs), Messagelisteneradapter implements the MessageListener interface, It will delegate the message to the object Listener (target Listener) Defalutmessagedelegate method, the appropriate conversion of the message parameter, and then invoke the method through reflection.



2.2 Defining the way the bean is configured


<!-- bean configuration --><bean id= "MessageListener" class= " Org.springframework.data.redis.listener.adapter.MessageListenerAdapter "><constructor-arg><bean  class= "Secondriver.spring.redis.MyMessageListener"  /></constructor-arg></bean>< Bean id= "Rediscontainer" class= "Org.springframework.data.redis.listener.RedisMessageListenerContainer" > <property name= "ConnectionFactory"  ref= "Jedisconnectionfactory"  /><property name= "Messagelisteners" ><map><entry key-ref= "MessageListener" ><list><bean class= " Org.springframework.data.redis.listener.ChannelTopic "><constructor-arg value=" SPRINGTV " /> </bean><bean class= "Org.springframework.data.redis.listener.PatternTopic" ><constructor-arg  value= "hello*"  /></bean><bean class= " Org.springframework.data.redis.listener.PatternTopic "><constructor-arg vAlue= "tv*"  /></bean></list></entry></map></property></bean> 


3. Publishing and receiving of analog messages


Simple test redismessagelistener@ignore@testpublic void test10 () throws Interruptedexception { Redismessagelistenercontainer rmlc;//Ctx.getbean (redismessagelistenercontainer.class); RMLC = ( Redismessagelistenercontainer) Ctx.getbean ("Rediscontainer"), while (true) {if (rmlc.isrunning ()) { System.out.println ("Redismessagelistenercontainer is running..");} Thread.Sleep (5000);}}

Since this is a test that keeps the program running through a dead loop and then publishes a message to the specified channel of the Redis service, the message for the subscribed channel is received by the client connection and the OnMessage method in the Mymessagelistener object is called.

is the simulation process:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5C/34/wKiom1Ub8BSDgB1fAAKgcAjznCU947.jpg "title=" Pubsubmessage.png "alt=" Wkiom1ub8bsdgb1faakgcajzncu947.jpg "/>

Four messages were released, Spring,springtv,hello, Nono four channels, according to the topic name in the configuration that defines the bean, only Springtv,hello matches the pattern, and the two messages are also received.

In addition to this simulation, the actual application development is through the jedisconnection pub/sub-related approach to the Redis service to publish messages or Redistemplate Convertandsend method.

4. Finally

SDR is in the development phase of the project, more features read the source code, step-to-step mining.

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1627478

Using spring Data Redis operations Redis (ii)

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.