RabbitMQ Work queues

消息生产者代码

package com.gch.rabbit.queues;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.util.concurrent.TimeoutException;

/**
 * 消息生产者
 */
public class Task {

    private static final String TASK_QUEUE_NAME = "queue";

    public static void main(String[] argv) throws java.io.IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();//创建工厂
        factory.setHost("localhost");//设置rabbitMQ地址
        Connection connection = factory.newConnection();//用工厂创建连接
        Channel channel = connection.createChannel();//用连接创建频道

        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);//在频道中申明一个队列
        //分发消息
        for(int i = 0 ; i < 5; i++){
            String message = "Hello World! " + i;
            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            System.out.println("发送消息" + message);
        }
        channel.close();
        connection.close();
    }
}
 1号队列代码

package com.gch.rabbit.queues;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;

public class Worker1 {

    private static final String TASK_QUEUE_NAME = "queue1";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
        System.out.println("1号队列正在等待消息...");

        // 每次从队列中获取数量
        channel.basicQos(1);

        final 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("1号队列接收到消息:" + message);
                try {
                    doWork(message);
                } finally {
                    System.out.println("1号队列处理完任务");
                    // 消息处理完成确认
                    //ACK机制
                    channel.basicAck(envelope.getDeliveryTag(), false);//设置   队列确认完成
                }
            }
        };
        // 消息消费完成确认
        channel.basicConsume(TASK_QUEUE_NAME, false, consumer);//频道反馈确认队列消息完成
    }

    private static void doWork(String task) {
        try {
            System.out.println("1号队列正在处理:" + task);
            Thread.sleep(1000); // 暂停1秒钟
        } catch (InterruptedException _ignored) {
            Thread.currentThread().interrupt();
        }
    }

}

2号队列代码

package com.gch.rabbit.queues;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;

public class Worker2 {

    private static final String TASK_QUEUE_NAME = "queue2";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
        System.out.println("2号队列正在等待消息...");
        // 每次从队列中获取数量
        channel.basicQos(1);

        final 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("2号队列接收到消息:" + message);
                try {
                    doWork(message);
                } finally {
                    System.out.println("2号队列处理完任务");
                    // 消息处理完成确认
                    //ACK机制
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
        };
        // 消息消费完成确认
        channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
    }

    private static void doWork(String task) {
        try {
            System.out.println("2号队列正在处理:" + task);
            Thread.sleep(1000); // 暂停1秒钟
        } catch (InterruptedException _ignored) {
            Thread.currentThread().interrupt();
        }
    }

}

你可能感兴趣的:(rabbitMQ,java)