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"