Search in sources :

Example 6 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.

the class CompetingReceiver method receive.

public String receive() {
    if (channel == null) {
        initialize();
    }
    String message = null;
    try {
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        message = new String(delivery.getBody());
        LOGGER.info("Message received: " + message);
        return message;
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ShutdownSignalException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return message;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 7 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project pancm_project by xuwujing.

the class Worker method main.

// private static final String TASK_QUEUE_NAME = "tsk.hybris.productbrand.tsk";
/**
 * The entry point of application.
 *
 * @param argv the input arguments
 * @throws Exception the exception
 */
public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    // factory.setHost("localhost");
    factory.setUri("amqp://guest:guest@172.26.129.3:5672");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    // queue的持久化需要在声明时指定durable=True
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    // 保证在接收端一个消息没有处理完时不会接收另一个消息
    channel.basicQos(1);
    // channel.basicQos(0, 1, false); //这样RabbitMQ就会使得每个Consumer在同一个时间点最多处理一个Message。换句话说,在接收到该Consumer的ack前,他它不会将新的Message分发给它。
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer)

Example 8 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project pancm_project by xuwujing.

the class RabbitConsumer method main.

/**
 * The entry point of application.
 *
 * @param argv the input arguments
 * @throws Exception the exception
 */
public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 打开连接和创建频道,与发送端一样
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    // 声明队列,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    // 创建队列消费者
    QueueingConsumer consumer = new QueueingConsumer(channel);
    // 指定消费队列
    channel.basicConsume(QUEUE_NAME, true, consumer);
    while (true) {
        // 消费者程序运行开着 如果生产者新增了数据会自动获取
        Thread.sleep(500);
        // nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("'[x] Received '" + message);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer)

Example 9 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project storm-amqp-spout by rapportive-oss.

the class AMQPSpout method setupAMQP.

private void setupAMQP() throws IOException {
    final int prefetchCount = this.prefetchCount;
    final ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(amqpHost);
    connectionFactory.setPort(amqpPort);
    connectionFactory.setUsername(amqpUsername);
    connectionFactory.setPassword(amqpPassword);
    connectionFactory.setVirtualHost(amqpVhost);
    this.amqpConnection = connectionFactory.newConnection();
    this.amqpChannel = amqpConnection.createChannel();
    log.info("Setting basic.qos prefetch-count to " + prefetchCount);
    amqpChannel.basicQos(prefetchCount);
    final Queue.DeclareOk queue = queueDeclaration.declare(amqpChannel);
    final String queueName = queue.getQueue();
    log.info("Consuming queue " + queueName);
    this.amqpConsumer = new QueueingConsumer(amqpChannel);
    this.amqpConsumerTag = amqpChannel.basicConsume(queueName, false, /* no auto-ack */
    amqpConsumer);
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Queue(com.rabbitmq.client.AMQP.Queue)

Example 10 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.

the class MemoryAlarms method flowControl.

@Test
public void flowControl() throws IOException, InterruptedException {
    basicPublishVolatile(Q);
    setResourceAlarm("memory");
    // non-publish actions only after an alarm should be fine
    assertNotNull(basicGet(Q));
    QueueingConsumer c = new QueueingConsumer(channel);
    String consumerTag = channel.basicConsume(Q, true, c);
    // publishes after an alarm should not go through
    basicPublishVolatile(Q);
    // the publish is async, so this is racy. This also tests we don't die
    // by heartbeat (3x heartbeat interval + epsilon)
    assertNull(c.nextDelivery(3100));
    // once the alarm has cleared the publishes should go through
    clearResourceAlarm("memory");
    assertNotNull(c.nextDelivery(3100));
    // everything should be back to normal
    channel.basicCancel(consumerTag);
    basicPublishVolatile(Q);
    assertNotNull(basicGet(Q));
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Aggregations

QueueingConsumer (com.rabbitmq.client.QueueingConsumer)55 Test (org.junit.Test)30 IOException (java.io.IOException)13 ConsumerCancelledException (com.rabbitmq.client.ConsumerCancelledException)12 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)12 Channel (com.rabbitmq.client.Channel)11 Connection (com.rabbitmq.client.Connection)9 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)9 Delivery (com.rabbitmq.client.QueueingConsumer.Delivery)9 AMQP (com.rabbitmq.client.AMQP)2 ArrayList (java.util.ArrayList)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Queue (com.rabbitmq.client.AMQP.Queue)1 Envelope (com.rabbitmq.client.Envelope)1 GetResponse (com.rabbitmq.client.GetResponse)1 List (java.util.List)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RabbitMQMessage (org.apache.axis2.transport.rabbitmq.RabbitMQMessage)1