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.
On the publish client, 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 VoidMain (String [] args ){
- String cmd ="Subscribe share \ r \ n";
- SocketChannel client =Null;
- Try{
- SocketAddress address =NewInetSocketAddress ("Localhost",6379);
- Client = SocketChannel. open (address );
- Client. configureBlocking (False);// Set to asynchronous
- ByteBuffer buffer = ByteBuffer. allocate (100);
- Buffer. put (cmd. getBytes ());
- Buffer. clear ();
- Client. write (buffer );
- System. out. println ("Send data :"+NewString (buffer. array ()));
- While(True){
- Buffer. flip ();
- IntI = client. read (buffer );
- If(I>0){
- Byte[] B = buffer. array ();
- System. out. println ("Receive data :"+NewString (B ));
- Break;
- }
- }
- }Catch(Exception e ){
- Try{
- Client. close ();
- }Catch(IOException e1 ){
- E1.printStackTrace ();
- }
- E. printStackTrace ();
- }
- }