Redis Message Queuing

Source: Internet
Author: User

In general, there are two scenarios for Message Queuing, a Publisher subscriber pattern, and a producer consumer model. Message Queuing with both Redis scenarios is possible.

producer Consumer Model : Producer production messages put in the queue, multiple consumers listening to the queue at the same time, who first grabbed the message who will take the message from the queue, that is, for each message can only be owned by a maximum of one consumer;

Publisher subscriber Mode : Publisher production messages are placed in the queue, and consumers of multiple listening queues receive the same message, that is, under normal circumstances, each consumer should receive the same message;

The corresponding usage scenarios include: A system holds data to the queue, and B system takes data in the queue;

1, Redis queue mode: Can realize the B system is not online state, a system to the queue of data, when the B system on line, you can manually fetch the data in the queue;

2, Redis subscription mode: Can implement a system to the queue of data, when the B system subscribed to this queue and online, you can automatically listen to the queue to fetch data, but not before the online data is not available;

First, Producer/consumer Mode

This method is implemented with the help of the Redis list structure. Producer calls Redis's Lpush to plug in a message into a specific key, consumer calls Brpop to constantly listen to the key.

Producer:

producer Code2 String key = "Demo:mq:test";  3 String msg = "Hello World";  4 Redisdao.lpush (key, msg);     

Consumer:

1 // Consumer code 2 String key = "Demo:mq:test"; 3 While (true) { 4 // block invoke < Span style= "color: #000000;" >5 list<string> msgs = Redisdao.brpop (block_timeout, ListKey); < Span style= "color: #000000;" >< Span style= "color: #000000;" >6 if (msgs = null ") Span style= "color: #0000ff;" >continue < Span style= "color: #000000;" >< Span style= "color: #000000;" >7 String jobmsg = msgs.get (1 ); < Span style= "color: #000000;" >< Span style= "color: #000000;" >8 Processmsg (jobmsg); < Span style= "color: #000000;" >< Span style= "color: #000000;" >< Span style= "color: #000000;" >9}

When there are multiple consumers, it dispatches the message in the order in which the brpop is called, not randomly. Block_timeout is not recommended for infinity (some Redis drivers also directly do not support inifinity), we are currently set to 30 (units in seconds) in good condition.

Second,pub/sub Mode

Redis supports pub/sub instructions starting from version 2.0.0. For details, see: Http://redis.io/topics/pubsub

Realization idea: Publisher calls Redis's Publish method to send a message to a specific channel, subscriber subscribe to the channel at initialization, and receives the message as soon as it is available.

A relatively simple demo can be found in: http://shift-alt-ctrl.iteye.com/blog/1867454

1. Publication and subscription of REDIS messages

The Redis Subscribe command allows the client to subscribe to any number of channels, and whenever new information is sent to the subscribed channel, the information is sent to all clients subscribed to the specified channel. As an example, it shows the channel CHANNE11 and the three clients subscribing to the channel: Client2, CLIENT5, and client1 relationships:

When a new message is sent to channel Channe11 via the Publish command, the message is sent to the three clients subscribed to it:

Subscribe to the Bar channel. format: Subscribe name1 name2

A successful subscription response, corresponding to the subscription type, subscription channel, number of subscriptions.

127.0.0.1:6379> SUBSCRIBE barreading messages ... (Press Ctrl-C to quit)"subscribe""bar"1      

Restart a Publisher client to send a message. format: Publish ChannelName Message

#对有一个订阅者的频道发送信息
127.0.0.1:6379>1
#对没有订阅者的频道发送信息
127.0.0.1:6379> Publish bar "Can any body hear me?"
(integer) 0

Subscribe to the client reply, corresponding to the message type, channel, message.

"message""bar""val        "

For details, see: http://www.cnblogs.com/mushroom/p/4470006.html

Http://redis.readthedocs.org/en/2.4/pub_sub.html

2 , key space notification (Keyspace notifications)

before subscribing to a channel, set the set notify-keyspace-events Kea parameter before running the Subscribe name1 name2 subscription channel.

At this point, a message is sent to the subscriber when a specified key is updated

See also: http://redis.io/topics/notifications

http://blog.csdn.net/chaijunkun/article/details/27361453

Redis Message Queuing

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.