The working principle of

RabbitMQ- RabbitMQ- the RabbitMQ website describes the framework in detail. For those of you new to RabbitMQ, check it out. Once you understand how this works, look directly at the following code.

producers

For background reasons, there is no need for Exchange. Those of you who want to see the whole process go to this article. Be careful to run in child threads

ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST); / / IP address factory. SetPort (PORT); // port factory.setusername ("test");
        factory.setPassword("test"); // create a newConnection Connection = factory.newconnection (); // Create a Channel Channel = connection.createchannel (); // Declare a queue channel.queueDeclare(QUEUE_NAME,false.false.false, null); // Channel. BasicPublish ("", QUEUE_NAME, null, message.getBytes("UTF-8")); // Close the channel and connect channel.close(); connection.close();Copy the code

consumers

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);
        factory.setUsername("test");
        factory.setPassword("test"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // Declare a queue channel.queueDeclare(QUEUE_NAME,false.false.false, null);
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("RecvLogsTopic1 [x] Received '" + envelope.getRoutingKey() + "' : '" + message + "'"); }}; channel.basicConsume(QUEUE_NAME,true, consumer);
Copy the code