同时从RabbitMQ读取

时间:2017-07-31 14:52:20

标签: concurrency rabbitmq threadpoolexecutor

基本上我试图从RabbitMQ读取。 我需要同时进行。因此我使用的是ThreadPoolExecutor。 我已经写了相同的下面的类。我正在使用intelliJ IDEA。

1)RabbitMQConsumer.java

package consume.rabbitmq;


import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMQConsumer implements Runnable {

private String name;

public RabbitMQConsumer(String name)
{
    this.name = name;
}

public String getName() {
    return name;
}

public void run() {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("admin");
    factory.setPassword("password");
    factory.setVirtualHost("abc-pqr.dessqa.com");
    factory.setHost("abc-pqr.dessqa.com");
    factory.setPort(5672);
    Connection conn = null;
    Channel channel = null;
    String exchangeName = "amq.direct";
    String queueName = "cust-670";
    boolean durable = true;
    boolean noAck = false;
    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean runInfinite = true;

    try {
        conn = factory.newConnection();
        channel = conn.createChannel();
        channel.exchangeDeclare(exchangeName, "direct", durable);
        channel.queueDeclare(queueName, durable,false,false,null);
        channel.basicConsume(queueName, noAck, consumer);
        Long duration = (long) (Math.random() * 10);
        System.out.println("Consuming the queue during : " + name);
        TimeUnit.SECONDS.sleep(duration);

        while (runInfinite) {
            QueueingConsumer.Delivery delivery;
            try {
                delivery = consumer.nextDelivery();
            } catch (InterruptedException ie) {
                continue;
            }
            System.out.println("Message received"
                    + new String(delivery.getBody()));
            try {
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(),     false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        channel.close();
        conn.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }

 }

}

2)BasicThreadPoolExecutor

package consume.rabbitmq;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class BasicThreadPoolExecutor {

public static void main(String[] args){

    ThreadPoolExecutor executor = (ThreadPoolExecutor)   Executors.newCachedThreadPool();
    for (int i = 0; i <= 5; i++)
    {
        RabbitMQConsumer rabbitMQConsumer = new  RabbitMQConsumer("rabbitMQConsumer " + i);
        System.out.println("A new rabbitMQConsumer has been added : " +  rabbitMQConsumer.getName());
        executor.execute(rabbitMQConsumer);
    }
    executor.shutdown();

}

}

代码工作正常,我可以从Rabbitmq读取。 但我不确定我的方法是否正确。我们可以优化它吗?

此致

0 个答案:

没有答案