Server code:
1 package COM. example. workqueue; 2 3 Import Java. io. ioexception; 4 5 import COM. rabbitmq. client. channel; 6 Import COM. rabbitmq. client. connection; 7 Import COM. rabbitmq. client. connectionfactory; 8 Import COM. rabbitmq. client. messageproperties; 9 10 public class send {11 12 public static void main (string [] ARGs) throws ioexception {13 14 // queue name 15 string queuename = "task_queue"; 16 17 connectionfactor Y factory = new connectionfactory (); 18 19 // Remote Server IP address. If you test it locally, you can change it to localhost20 factory. sethost ("121.40.151.120"); 21 22 // the user name and password must be set to 23 factory. setusername ("rabbitmqname"); 24 factory. setpassword ("rabbitmqpwd"); 25 26 connection conn = factory. newconnection (); 27 channel = Conn. createchannel (); 28 29 Boolean durable = true; 30 31/** 32 * parameter description: 33 * queue: queue name 34 * durable: whether the queue data can be persisted, true: yes, FAL SE: No. That is to say, after the service is restarted, whether the queue data still exclusive: whether it is a dedicated connection of a queue 36 * autodelete: whether to automatically delete 37 * arguments when the queue is no longer used, that is, when there is no consumer, for example, the queue survival time is 38 */39 channels. queuedeclare (queuename, durable, false, false, null); 40 41 string [] STRs = new string [] {"first message. "}; 42 string message = getmessage (STRs); 43 44/** 45 * parameter description: 46 * exchange: the default exchange type is" ", which is of the direct type, 47 * any messages sent to the default exchange will be routed to the queue corresponding to the routingkey name. If no queue exists Information will be discarded. 48 * routingkey: Specify the queue for receiving messages 49 * props: other attributes, such as message routing header information, persistent information 50 * Body: Message content 51 */52 channel. basicpublish ("", queuename, messageproperties. persistent_text_plain, message. getbytes (); 53 54 system. out. println ("[" + message + "]"); 55 56 // finally, we close the channel and connection to release resources. 57 channel. close (); 58 Conn. close (); 59} 60 61 Private Static string getmessage (string [] strings) {62 if (strings. length <1) {63 return "Hello world! "; 64} 65 return joinstrings (strings," "); 66} 67 68 private static string joinstrings (string [] strings, string delimiter) {69 int length = strings. length; 70 If (length = 0) {71 return ""; 72} 73 stringbuilder words = new stringbuilder (strings [0]); 74 for (INT I = 1; I <length; I ++) {75 words. append (delimiter ). append (strings [I]); 76} 77 return words. tostring (); 78} 79 80}
Client code:
1 package COM. example. workqueue; 2 3 Import Java. io. ioexception; 4 5 import COM. rabbitmq. client. channel; 6 Import COM. rabbitmq. client. connection; 7 Import COM. rabbitmq. client. connectionfactory; 8 Import COM. rabbitmq. client. consumercancelledexception; 9 Import COM. rabbitmq. client. queueingconsumer; 10 Import COM. rabbitmq. client. shutdownsignalexception; 11 12 public class Recv {13 14 public static void Main (string [] ARGs) throws ioexception, shutdownsignalexception, 15 consumercancelledexception, interruptedexception {16 17 // queue name 18 string queuename = "task_queue "; 19 20 connectionfactory factory = new connectionfactory (); 21 22 factory. sethost ("121.40.151.120"); 23 factory. setusername ("rabbitmqname"); 24 factory. setpassword ("rabbitmqpwd"); 25 26 connection = factory. newconnection (); 27 Channel channel = connection. createchannel (); 28 29 // indicates that at the same time, do not send more than one rev message (only one message can be sent ), that is to say, do not distribute a new message to rev until it is processed and the notification flag (acknowledged) 30 channel of the previous message is returned. basicqos (1); 31 32 // consistent with the server 33 channel. queuedeclare (queuename, true, false, false, null); 34 35 system. out. println ("crtl + C"); 36 37 // queueingconsumer: Used to cache messages pushed to us by the server. 38 queueingconsumer consumer = new queueingconsumer (Channel); 39 40 Boolean autoack = false; 41/** 42 * parameter description: 43 * queue: queue name 44 * autoack: automatic response, true: Once a message is consumed by the consumer, the server will know that the message has been delivered, removing the message from the queue; 45 * false: the consumer must display the call Channel. the basicack () method notifies the server. If no call is displayed, the message enters the 46 * unacknowledged status. After the current consumer is disconnected, the message changes to the ready status and enters the queue again. 47 * callback: The specific consumer class is 48 */49 channel. basicconsume (queuename, autoack, consumer); 50 51 while (true) {52 queueingconsumer. delivery delivery = consumer. nextdelivery (); 53 string message = new string (delivery. getbody (); 54 system. out. println ("[" + message + "]"); 55 dowork (Message); 56 system. out. println ("R [done]"); 57 58/*** 59 * indicates that the call notifies the server that the message has been consumed and acknowledged60 * true: notifies all untracked with the same tag, false: Only one 61 */62 channel is notified. basicack (delivery. getenvelope (). getdeliverytag (), false); 63} 64} 65 66 Private Static void dowork (string message) throws interruptedexception {67 for (char CH: Message. tochararray () {68 if (CH = '. ') {69 thread. sleep (1000); 70} 71} 72} 73 74}
Rabbitmq Series II: work queue