Example usage for com.rabbitmq.client Channel queueDeclare

List of usage examples for com.rabbitmq.client Channel queueDeclare

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel queueDeclare.

Prototype

Queue.DeclareOk queueDeclare() throws IOException;

Source Link

Document

Actively declare a server-named exclusive, autodelete, non-durable queue.

Usage

From source file:de.tuberlin.cit.livescale.messaging.endpoints.AMQPEndpointTest.java

License:Apache License

/**
 * Tests sending a "normal" non-{@link de.tuberlin.cit.livescale.messaging.RequestMessage}
 * /*from   ww w.  ja  v  a 2s  .  c o  m*/
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testAMQPSendNoResponse() throws IOException, URISyntaxException {
    ConnectionFactory fac = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Channel chan = mock(Channel.class);
    Queue.DeclareOk declareOK = mock(Queue.DeclareOk.class);

    // ConnectionFactory
    when(fac.newConnection()).thenReturn(con);
    // Connection
    when(con.createChannel()).thenReturn(chan);
    // Channel
    when(chan.queueDeclare()).thenReturn(declareOK);
    // DeclareOK result object
    String queueName = "testQueue";
    when(declareOK.getQueue()).thenReturn(queueName);

    AMQPEndpoint ep = new AMQPEndpoint();
    ep.setName("amqpTest");
    Whitebox.setInternalState(ep, "connectionFactory", fac);
    ep.configure(this.exampleConf);

    // configuredRoutingKey
    String routingKey = this.exampleConf.get(AMQPEndpoint.CONFIG_KEY_ROUTING_KEY);
    URI targetURI = new URI("amqp:///test");

    MessageCenter mc = new MessageCenter(false, this.emptyConf);
    mc.addEndpoint(ep);
    mc.startAllEndpoints();
    assertTrue(mc.hasEndpointToURI(targetURI));

    // kickin the jams
    mc.send(new TestMessage(), targetURI);
    // verify(chan, times(1)).basicPublish(eq(""), eq(routingKey), (BasicProperties) isNull(), (byte[]) any());
}

From source file:de.tuberlin.cit.livescale.messaging.endpoints.AMQPEndpointTest.java

License:Apache License

/**
 * Tests message arrival on a {@link AMQPEndpoint} and
 * correctness of the generated responseURI in the
 * {@link MessageManifest} //from  w  ww  .  j  a v  a2  s  .  co m
 * 
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testArrival() throws IOException, URISyntaxException {
    ConnectionFactory fac = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Channel chan = mock(Channel.class);
    Queue.DeclareOk declareOK = mock(Queue.DeclareOk.class);

    // ConnectionFactory
    when(fac.newConnection()).thenReturn(con);
    // Connection
    when(con.createChannel()).thenReturn(chan);
    // Channel
    when(chan.queueDeclare()).thenReturn(declareOK);
    // DeclareOK result object
    String queueName = "testQueue";
    when(declareOK.getQueue()).thenReturn(queueName);

    AMQPEndpoint ep = new AMQPEndpoint();
    String endpointName = "amqpTest";
    ep.setName(endpointName);
    Whitebox.setInternalState(ep, "connectionFactory", fac);
    ep.configure(this.exampleConf);
    // hookup a listener
    MessageEndpointListener listener = mock(MessageEndpointListener.class);
    ep.addMessageEndpointListener(listener);

    // kickin the jams 
    ep.start();
    // hook up the consumer / manually call the callback
    ArgumentCaptor<DefaultConsumer> consumer = ArgumentCaptor.forClass(DefaultConsumer.class);
    // should in total consume the own queue and the tasks queue
    // depends on config settings
    verify(chan, times(2)).basicConsume(anyString(), anyBoolean(), (Consumer) anyObject());
    verify(chan).basicConsume(eq(queueName), anyBoolean(), consumer.capture());
    String replyQueueName = "replyQueue";
    // faux envelope
    Envelope envelope = new Envelope(-1l, false, "daExchange", "daRoutingKey");
    // faux properties
    BasicProperties props = new BasicProperties.Builder().replyTo(replyQueueName)
            .appId(UUID.randomUUID().toString()).build();
    // faux message 
    TestRequestMessage message = new TestRequestMessage();
    // faux manifest
    MessageManifest mmIn = new MessageManifest(message, new URI("amqp:///targetQueue"));
    // call the callback function
    consumer.getValue().handleDelivery("leTag", envelope, props, MessageFactory.encode(mmIn));
    ArgumentCaptor<MessageManifest> mm = ArgumentCaptor.forClass(MessageManifest.class);
    verify(listener).handleMessageReceived(mm.capture());
    assertEquals("amqp", mm.getValue().getResponseURI().getScheme());
    assertEquals(endpointName, mm.getValue().getResponseURI().getAuthority());
    assertEquals("/" + replyQueueName, mm.getValue().getResponseURI().getPath());
}

From source file:edu.kit.dama.util.test.RabbitMQReceiver.java

License:Apache License

public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    final Connection connection = factory.newConnection();
    final Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "audit.*");

    //  channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    /*  final Consumer consumer = new DefaultConsumer(channel) {
    @Override//  w w  w.ja v  a  2  s .  c om
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    String message = new String(body, "UTF-8");
            
    System.out.println(" [x] Received '" + message + "'");
    try {
      doWork(message);
    } finally {
      System.out.println(" [x] Done");
      channel.basicAck(envelope.getDeliveryTag(), false);
    }
    }
    };
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
    }*/
    /*   QueueingConsumer consumer = new QueueingConsumer(channel);
            
           /*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(" [x] Received '" + message + "'");
    }
           };*/
    /*channel.basicConsume(queueName, true, consumer);
           QueueingConsumer.Delivery delivery = consumer.nextDelivery(10000);
           if (delivery != null) {
    byte[] message = delivery.getBody();
    System.out.println("MESS " + new String(message));
           }*/
    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(" [x] Received '" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);

    // System.exit(0);
}

From source file:es.devcircus.rabbitmq_examples.rabbitmq_publish_subscribe.ReceiveLog.java

License:Open Source License

/**
 *
 * @param argv/*from   www .ja v a2 s. c o  m*/
 * @throws Exception
 */
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");

    System.out.println(" [*] Waiting for messages. 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());

        System.out.println(" [x] Received '" + message + "'");
    }
}

From source file:es.devcircus.rabbitmq_examples.rabbitmq_routing.ReceiveLogsDirect.java

License:Open Source License

/**
 *
 * @param argv// w  w w  . j a  v a2 s.  c o  m
 * @throws Exception
 */
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    //        factory.setHost("localhost");
    factory.setHost("192.168.0.202");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "direct");
    String queueName = channel.queueDeclare().getQueue();

    if (argv.length < 1) {
        System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
        System.exit(1);
    }

    for (String severity : argv) {
        channel.queueBind(queueName, EXCHANGE_NAME, severity);
    }

    System.out.println(" [*] Waiting for messages. 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] Received '" + routingKey + "':'" + message + "'");
    }
}

From source file:es.devcircus.rabbitmq_examples.rabbitmq_topics.ReceiveLogsTopic.java

License:Open Source License

/**
 * /*from  w w w . j  a  va 2 s . c  om*/
 * @param argv 
 */
public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        String queueName = channel.queueDeclare().getQueue();

        if (argv.length < 1) {
            System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
            System.exit(1);
        }

        for (String bindingKey : argv) {
            channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
        }

        System.out.println(" [*] Waiting for messages. 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] Received '" + routingKey + "':'" + message + "'");
        }
    } catch (IOException | InterruptedException | ShutdownSignalException | ConsumerCancelledException e) {
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:eu.betaas.rabbitmq.subscriber.SubscriberService.java

License:Apache License

public void startService() {
    try {/*w  ww .  j  av a2s. co m*/
        log.info("#Starting queue #");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection;
        log.info("#Starting #");
        connection = factory.newConnection();
        log.info("#Starting connection#");
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(ename, mode);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, ename, bussubkey);
        log.info("#Starting connection on queue #");
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);

        log.info("#Running #");
        run();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ShutdownSignalException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ConsumerCancelledException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:genqa.ExportRabbitMQVerifier.java

License:Open Source License

public void run() throws IOException, InterruptedException {
    final Connection connection = m_connFactory.newConnection();
    final Channel channel = connection.createChannel();

    try {//from   w  w  w  .ja v  a 2s  . co m
        channel.exchangeDeclare(m_exchangeName, "topic", true);
        String dataQueue = channel.queueDeclare().getQueue();
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE_FOO.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2_FOO.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE_FOO.#");
        String doneQueue = channel.queueDeclare().getQueue();
        channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE.#");
        channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE_FOO.#");

        // Setup callback for data stream
        channel.basicConsume(dataQueue, false, createConsumer(channel));

        // Setup callback for the done message
        QueueingConsumer doneConsumer = new QueueingConsumer(channel);
        channel.basicConsume(doneQueue, true, doneConsumer);

        // Wait until the done message arrives, then verify count
        final QueueingConsumer.Delivery doneMsg = doneConsumer.nextDelivery();
        final long expectedRows = Long.parseLong(ExportOnServerVerifier.RoughCSVTokenizer
                .tokenize(new String(doneMsg.getBody(), Charsets.UTF_8))[6]);

        while (expectedRows > m_verifiedRows) {
            Thread.sleep(1000);
            System.err.println("Expected " + expectedRows + " " + m_verifiedRows);
        }
    } finally {
        tearDown(channel);
        channel.close();
        connection.close();
    }
}

From source file:io.bootique.rabbitmq.client.channel.ChannelFactory.java

License:Apache License

/**
 * TODO: Comment what this method should do (and actually do)
 * Create channel and bind queue to exchange.
 *//*from   ww  w. j  a va 2s. c  o  m*/
public Channel openChannel(Connection connection, String exchangeName, String queueName, String routingKey) {
    try {
        Channel channel = connection.createChannel();
        exchangeDeclare(channel, exchangeName);

        if (queueName == null) {
            queueName = channel.queueDeclare().getQueue();
        } else {
            queueDeclare(channel, queueName);
        }

        channel.queueBind(queueName, exchangeName, routingKey);
        return channel;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:it.jugtorino.one.msvc.way.rabbit.geoip.reference.client.RpcClient.java

License:Open Source License

public RpcClient(Channel channel, String queue) throws IOException {
    this.channel = channel;
    this.queue = queue;
    this.callbackQueue = channel.queueDeclare().getQueue();
    this.consumer = new QueueingConsumer(channel);
    channel.basicConsume(callbackQueue, true, consumer);
    this.deliveryListeners = new ConcurrentHashMap<>();
}