Get ready:
1. Download the RABBITMQ and build the environment (as in Python: http://www.cnblogs.com/g177w/p/8176797.html)
2. Download the supported jar packages (http://repo1.maven.org/maven2/com/rabbitmq/amqp-client)
Producer Side (Productor.java):
1 Package rabbitmqtest;2 3 4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 7 ImportCom.rabbitmq.client.AMQP.Queue;8 ImportCom.rabbitmq.client.Channel;9 Importcom.rabbitmq.client.Connection;Ten Importcom.rabbitmq.client.ConnectionFactory; One APublicclassProductor { -public static String queue_name ="STEVEN"; - Public static void Main (string[] args) { the//initializing the socket link -ConnectionFactory factory =new ConnectionFactory (); -//Specify a link address -Factory.sethost ("localhost"); + Try{ -//establishing the socket connection for the program and RABBITMQ +Connection Connection =factory.newconnection (); A//Creating Pipelines atChannel Channel =Connection.createchannel (); -//declaring queues - Channel.queuedeclare (queue_name,false,false,false,null); -//let the current thread sleep 8s to verify the RABBITMQ message polling -Thread.CurrentThread (). Sleep (8000); -String message ="Hello,world"; in//Send Message -Channel.basicpublish ("", queue_name, NULL, message.getbytes ()); toSystem.out.println ("[x] send the message"+message); + channel.close (); - connection.close (); the }catch (Exception e) { *System.out.println ("Program error:"+e); $ }Panax Notoginseng - } the}
View Code
Consumer side (Consummer.java):
1 Packagerabbitmqtest;2 Importcom.rabbitmq.client.Connection;3 ImportCom.rabbitmq.client.Channel;4 Importcom.rabbitmq.client.ConnectionFactory;5 ImportCom.rabbitmq.client.QueueingConsumer;6 ImportCom.rabbitmq.client.QueueingConsumer.Delivery;7 /**8 * This procedure in order to practice the simple operation of RABBITMQ9 * Ten * @authorSTEVEN One * A */ - Public classConsumer { - Public Static voidMain (string[] args) { the Try { - //To create a connection factory object -ConnectionFactory factory =Newconnectionfactory (); - //sets the parameters of the factory object to connect to the RABBITMQ +Factory.sethost ("localhost"); - //establishing a socket connection to the RABBITMQ +Connection Connection =factory.newconnection (); A //Creating Pipelines atChannel Channel =Connection.createchannel (); - //declaring queues -Channel.queuedeclare (Productor.queue_name,false,false,false,NULL); -SYSTEM.OUT.PRINTLN ("[*] waiting for messages. To exit Press CTRL + C "); - //Create a queue consumption object -Queueingconsumer consumer =NewQueueingconsumer (channel); in //Setting Parameters -Channel.basicconsume (Productor.queue_name,true, consumer); to //create a Receive object to receive messages from the server +Delivery Delivery =NULL; - //cyclic reception, which is equivalent to opening a listening the while(true) { *Delivery =consumer.nextdelivery (); $String message =NewString (Delivery.getbody ());Panax NotoginsengSystem.out.println ("[X] Received '" + Message + "'"); - } the}Catch(Exception e) { + System.out.println (e); A } the } +}
View Code
Java link RABBITMQ required jar package link address