Search in sources :

Example 51 with QueueingConsumer

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

the class MemoryAlarms method overlappingAlarmsFlowControl.

@Test
public void overlappingAlarmsFlowControl() throws IOException, InterruptedException {
    QueueingConsumer c = new QueueingConsumer(channel);
    String consumerTag = channel.basicConsume(Q, true, c);
    setResourceAlarm("memory");
    basicPublishVolatile(Q);
    assertNull(c.nextDelivery(100));
    setResourceAlarm("disk");
    assertNull(c.nextDelivery(100));
    clearResourceAlarm("memory");
    assertNull(c.nextDelivery(100));
    clearResourceAlarm("disk");
    assertNotNull(c.nextDelivery(3100));
    channel.basicCancel(consumerTag);
    basicPublishVolatile(Q);
    assertNotNull(basicGet(Q));
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Example 52 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project tech by ffyyhh995511.

the class Recv method recv.

public static void recv() throws java.io.IOException, java.lang.InterruptedException, TimeoutException {
    System.out.print("----------main method is begin----------");
    ConnectionFactory factory = new ConnectionFactory();
    System.out.println("----------factory successful!----------");
    factory.setHost("127.0.0.1");
    // factory.setUsername("mq");
    // factory.setPassword("mq");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    System.out.println("----------connected successful!----------");
    // 指定队列持久化
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    // 指定该线程同时只接收一条消息
    channel.basicQos(1);
    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 53 with QueueingConsumer

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

the class ClientReceive1 method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 * @throws IOException              the io exception
 * @throws InterruptedException     the interrupted exception
 * @throws TimeoutException         the timeout exception
 * @throws KeyManagementException   the key management exception
 * @throws NoSuchAlgorithmException the no such algorithm exception
 * @throws URISyntaxException       the uri syntax exception
 */
public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException, TimeoutException, KeyManagementException, NoSuchAlgorithmException, URISyntaxException {
    ConnectionFactory factory = new ConnectionFactory();
    // factory.setHost("localhost");
    // factory.setVirtualHost("my_mq");
    // factory.setUsername("zhxia");
    // factory.setPassword("123456");
    // 获取url
    factory.setUri("amqp://guest:guest@172.26.129.3:5672");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(queue_name, durable, false, false, null);
    System.out.println("Wait for message");
    // 消息分发处理
    channel.basicQos(1);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue_name, autoAck, consumer);
    while (true) {
        Thread.sleep(500);
        QueueingConsumer.Delivery deliver = consumer.nextDelivery();
        String message = new String(deliver.getBody());
        System.out.println("Message received:" + message);
        channel.basicAck(deliver.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 54 with QueueingConsumer

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

the class Recv 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("localhost");
    // factory.setHost("127.0.0.1");
    // 获取url
    factory.setUri("amqp://guest:guest@172.26.129.3:5672");
    // 打开连接和创建频道,与发送端一样
    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);
        List aa = new ArrayList();
        // nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        aa.add(message);
        System.out.println("你好吗!" + " [x] Received '" + message + "'" + aa);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 55 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project helix by apache.

the class ConsumerThread method run.

@Override
public void run() {
    Connection connection = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(_mqServer);
        connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        String queueName = channel.queueDeclare().getQueue();
        String bindingKey = _partition;
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
        System.out.println(" [*] " + _consumerId + " Waiting for messages on " + bindingKey + ". To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            String routingKey = delivery.getEnvelope().getRoutingKey();
            System.out.println(" [x] " + _consumerId + " Received '" + routingKey + "':'" + message + "'");
        }
    } catch (InterruptedException e) {
        System.err.println(" [-] " + _consumerId + " on " + _partition + " is interrupted ...");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException) IOException(java.io.IOException)

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