Example of HelloWorld model in RabbitMQ

        // Create MQ connection factory object 
        ConnectionFactory connectionFactory = new ConnectionFactory ();
         // Set connection RabbitMQ host 
        connectionFactory.setHost ("192.168.153.138" );
         // Set port number 
        connectionFactory.setPort (5672 );
         // Set which virtual connection to connect Host 
        connectionFactory.setVirtualHost ("/ ems" );
         // Set the username and password to access the virtual host 
        connectionFactory.setUsername ("ems" ); 
        connectionFactory.setPassword ( "123" ); 

        // Get the connection object 
        Connection connection = connectionFactory.newConnection ( );
        // Get the 
        channel in the connection Channel channel = connection.createChannel ();
         // Message queue corresponding to the channel binding
         // Parameter 1: Queue name is automatically created if the queue does not exist
         // Parameter 2: Used to define whether the queue should be persisted true persistence false not persistence
         // parameter 3: whether to monopolize the queue true monopoly false not exclusive
         // parameter 4: whether to automatically delete the queue after consumption true automatic deletion false does not automatically delete
         // parameter 5: additional parameter 
        channel.queueDeclare ( "hello", false , false , false , null );
         // Parameter 1: Switch
         // Parameter 2: Queue name
         // Parameter 3: Additional settings for message delivery
         // Parameter 4: Specific content of the message
        channel.basicPublish("", "hello", null, "hello rabbitmq".getBytes());

        channel.close();
        connection.close();

Consumer consumption

        // Create MQ connection factory object 
        ConnectionFactory connectionFactory = new ConnectionFactory ();
         // Set connection RabbitMQ host 
        connectionFactory.setHost ("192.168.153.138" );
         // Set port number 
        connectionFactory.setPort (5672 );
         // Set which virtual connection to connect Host 
        connectionFactory.setVirtualHost ("/ ems" );
         // Set the username and password to access the virtual host 
        connectionFactory.setUsername ("ems" ); 
        connectionFactory.setPassword ( "123" ); 

        // Get the connection object 
        Connection connection = connectionFactory.newConnection ( );
        // Get the 
        channel in the connection Channel channel = connection.createChannel ();
         // Message queue corresponding to the channel binding
         // Parameter 1: Queue name is automatically created if the queue does not exist
         // Parameter 2: Used to define whether the queue should be persisted true persistence false not persistence
         // parameter 3: whether to monopolize the queue true monopoly false not exclusive
         // parameter 4: whether to automatically delete the queue after consumption true automatic deletion false does not automatically delete
         // parameter 5: additional parameter 
        channel.queueDeclare ( "hello", false , false , false , null );
         // Consumer
         // Parameter 1: Which queue's message queue name to consume
         // Parameter 2: Start automatic message confirmation mechanism
         //参数3:消费时的回调接口
        channel.basicConsume("hello", true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("new String(body) = " + new String(body));
            }
        });
View Code

 

Guess you like

Origin www.cnblogs.com/teles/p/12727744.html