Redis publish subscription and custom command combination

Source: Internet
Author: User

Start the subscription and publish Client

On the subscription Client

Redis 127.0.0.1: 6379> psubscribe share

Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "share"

3) (integer) 1

Indicates that the client subscribes to the share channel

1 indicates that the number of connected subscription channels in the client is 1.

Publish the clientTo publish a message for this channel.

Redis 127.0.0.1: 6379> Publish share "share"
(Integer) 1

1 indicates that one connection receives the message.

Subscription client display:

1) "pmessage" // Message Type
2) "share" // The Name Of The channel I subscribed
3) "share" // The Name Of The channel I receive
4) "share" // message content

PS: The JAVA Implementation subscription code is attached:

Public static void main (string [] ARGs) {string cmd = "subscribe share \ r \ n"; socketchannel client = NULL; try {socketaddress address = new inetsocketaddress ("localhost ", 6379); client = socketchannel. open (Address); client. configureblocking (false); // set it to asynchronous bytebuffer buffer = bytebuffer. allocate (1, 100); buffer. put (CMD. getbytes (); buffer. clear (); client. write (buffer); system. out. println ("send data:" + new string (buffer. array (); While (true) {buffer. flip (); int I = client. read (buffer); if (I> 0) {byte [] B = buffer. array (); system. out. println ("received data:" + new string (B); break ;}} catch (exception e) {try {client. close () ;}catch (ioexception E1) {e1.printstacktrace () ;}e. printstacktrace ();}}

2:Redis also supports custom command combinations. Through multi and exec, several commands can be combined for execution.

redis 127.0.0.1:6379> SET counter 0OKredis 127.0.0.1:6379> MULTIOKredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> EXEC1) (integer) 12) (integer) 23) (integer) 3redis 127.0.0.1:6379> GET counter"3"

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.