pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5</groupId> <artifactId>rabbitmq</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.6.5</version> </dependency> </dependencies> </project>
Creating tools connection:
// no need to make a single embodiment of multiplexing VirtualHost public class {MQConnectionUtils // create a new connection public static Connection newConnection () throws IOException, a TimeoutException { // create the connection factory the ConnectionFactory Factory = new new the ConnectionFactory (); // link address factory.setHost ( "192.168.91.6" ); // user name factory.setUsername ( "ADMIN" ); // user password factory.setPassword ( "ADMIN" ); // AMQP port number factory.setPort (5672 ); / /连接virtualhost factory.setVirtualHost("/admin_toov5"); Connection connection = factory.newConnection(); return connection; } }
Producer categories:
public class Producer { // queue name Private static Final String UEUE_NAME = "test_queue" ; public static void main (String [] args) throws IOException, a TimeoutException { // create a new connection Connection Connection = MQConnectionUtils.newConnection (); // Create Channel Channel Channel = connection.createChannel (); // create a queue channel.queueDeclare (UEUE_NAME, to false , to false , to false , null ); // Create Message String MSG = "toov5_message" ; System.out.println ( "producer delivery message" + MSG); // producer sends message channel.basicPublish ( "", UEUE_NAME, null , Msg.getBytes ()) ; // close the passage and connection channel.close (); Connection.close (); } }
Operating results, look at the queue:
Simulation Get message:
Consumer Producer with substantially similar:
public class Consumer { // queue name Private static Final String queue_name = "test_queue" ; public static void main (String [] args) throws IOException, a TimeoutException { System.out.println ( "Consumer Start ....... ... " ); // create a new connection connection connection = MQConnectionUtils.newConnection (); // create a Channel Channel Channel = connection.createChannel (); // consumer association queue channel.queueDeclare (qUEUE_NAME,to false , to false , to false , null ); DefaultConsumer defaultConsumerr = new new DefaultConsumer (Channel) { // listening message acquiring @Override public void handleDelivery (String consumerTag, Envelope Envelope, BasicProperties Properties, byte [] body) throws IOException { String MSG = new new String (body, "UTF-8" ); System.out.println ( "consumers get news producer:" + msg); } }; // hand default mode setting automatic answer mode true: automatic-answer mode channel.basicConsume (queue_name, to true , defaultConsumerr); // // close the passage and connected // channel.close (); // Connection.close ( ); } }
After the results: