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:uk.org.openeyes.oink.hl7v2.test.Hl7ITSupport.java

License:Open Source License

protected static String setupRabbitQueue(Channel channel, String exchange, String routingKey)
        throws IOException {
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, exchange, routingKey);
    return queueName;
}

From source file:uk.org.openeyes.oink.proxy.test.ITProxyRoute.java

License:Open Source License

@Test
public void testPatientSearchWithNull() throws Exception {

    // Build Oink request
    String resourcePath = "/Patient";
    String method = "GET";
    OINKRequestMessage request = new OINKRequestMessage();
    request.setResourcePath(resourcePath);
    request.setMethod(HttpMethod.valueOf(method));

    // Send Oink request over rabbit
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    String replyQueueName = channel.queueDeclare().getQueue();
    channel.queueBind(replyQueueName, testProperties.getProperty("rabbit.defaultExchange"), replyQueueName);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(replyQueueName, true, consumer);

    BasicProperties props = new BasicProperties().builder().replyTo(replyQueueName).build();

    byte[] requestBody = camelCtx.getTypeConverter().convertTo(byte[].class, request);

    channel.basicPublish(testProperties.getProperty("rabbit.defaultExchange"),
            testProperties.getProperty("rabbit.routingKey"), props, requestBody);

    // Wait//from   w  ww  .  j a v a2  s.  c o m
    Thread.sleep(1000);

    // Assert response
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(5000);
    assertNotNull(delivery);
    byte[] responseBody = delivery.getBody();

    OinkMessageConverter conv = new OinkMessageConverter();
    OINKResponseMessage message = conv.responseMessageFromByteArray(responseBody);

}

From source file:uk.org.openeyes.oink.proxy.test.ITProxyRoute.java

License:Open Source License

@Ignore
@Test/*  w w  w. j  a v  a 2  s . c  om*/
public void testGetRequestGetsProxied() throws Exception {

    // Mock endpoint server
    InputStream is = ITProxyRoute.class.getResourceAsStream("/example-messages/fhir/patient.json");
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer);
    String jsonPatient = writer.toString();
    byte[] expectedResponseBody = jsonPatient.getBytes();
    server.setResponse(200, expectedResponseBody, "application/json+fhir");
    server.start();

    // Build Oink request
    String resourcePath = "/Patient/1123";
    String parameters = "foo=bar&foo2=bar2";
    String method = "GET";
    OINKRequestMessage request = new OINKRequestMessage();
    request.setResourcePath(resourcePath);
    request.setParameters(parameters);
    request.setMethod(HttpMethod.valueOf(method));

    // Send Oink request over rabbit
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    String replyQueueName = channel.queueDeclare().getQueue();
    channel.queueBind(replyQueueName, testProperties.getProperty("rabbit.defaultExchange"), replyQueueName);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(replyQueueName, true, consumer);

    BasicProperties props = new BasicProperties().builder().replyTo(replyQueueName).build();

    byte[] requestBody = camelCtx.getTypeConverter().convertTo(byte[].class, request);

    channel.basicPublish(testProperties.getProperty("rabbit.defaultExchange"),
            testProperties.getProperty("rabbit.routingKey"), props, requestBody);

    // Wait
    Thread.sleep(1000);

    // Assert mocked server receives request intact
    assertEquals(method, server.getRequestMethod());
    assertEquals(parameters, server.getRequestParams());
    assertEquals("/oink" + resourcePath, server.getRequestPath());

    // Assert response
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(5000);
    assertNotNull(delivery);
    byte[] responseBody = delivery.getBody();

    OINKResponseMessage response = camelCtx.getTypeConverter().convertTo(OINKResponseMessage.class,
            responseBody);

    String responsePatientJson = camelCtx.getTypeConverter().convertTo(String.class,
            response.getBody().getResource());

    assertEquals(200, response.getStatus());
    assertEquals(jsonPatient, responsePatientJson);
    server.stop();

}

From source file:uk.org.openeyes.oink.proxy.test.ITProxyRoute.java

License:Open Source License

@Ignore
@Test/*from  ww w .  j  a  v a 2  s  .c om*/
public void testPostRequestGetsProxied() throws Exception {

    // Mock endpoint server
    server.setResponse(201, null, null);
    server.start();

    // Build Oink request
    OINKRequestMessage request = new OINKRequestMessage();
    request.setResourcePath("/Patient");
    request.setMethod(HttpMethod.POST);
    FhirBody body = buildFhirBodyFromResource("/example-messages/fhir/patient.json");
    request.setBody(body);

    // Send Oink request over rabbit
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    String replyQueueName = channel.queueDeclare().getQueue();
    channel.queueBind(replyQueueName, testProperties.getProperty("rabbit.defaultExchange"), replyQueueName);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(replyQueueName, true, consumer);

    BasicProperties props = new BasicProperties().builder().replyTo(replyQueueName).build();

    byte[] requestBody = camelCtx.getTypeConverter().convertTo(byte[].class, request);

    channel.basicPublish(testProperties.getProperty("rabbit.defaultExchange"),
            testProperties.getProperty("rabbit.routingKey"), props, requestBody);

    // Wait
    Thread.sleep(1000);

    // Assert mocked server receives request intact
    String requestBodyReceivedByServer = server.getRequestBody();
    StringWriter writer = new StringWriter();
    IOUtils.copy(ITProxyRoute.class.getResourceAsStream("/example-messages/fhir/patient.json"), writer);
    String expectedBodyReceivedByServer = writer.toString();
    assertEquals(expectedBodyReceivedByServer, requestBodyReceivedByServer);

    // Assert response
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(10000);
    assertNotNull(delivery);
    byte[] responseBody = delivery.getBody();

    OINKResponseMessage response = camelCtx.getTypeConverter().convertTo(OINKResponseMessage.class,
            responseBody);

    assertEquals(201, response.getStatus());
    server.stop();
}

From source file:uk.org.openeyes.oink.proxy.test.support.RabbitClient.java

License:Open Source License

public byte[] sendAndRecieve(byte[] message, String routingKey, String exchange) throws Exception {
    log.debug("Sending message to direct exchange:" + exchange + " with routing key:" + routingKey);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchange, "direct", true, false, null);
    String replyQueueName = channel.queueDeclare().getQueue();
    log.debug("Reply queue name is " + replyQueueName);
    channel.queueBind(replyQueueName, exchange, replyQueueName);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(replyQueueName, true, consumer);
    String corrId = java.util.UUID.randomUUID().toString();
    BasicProperties props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build();

    channel.basicPublish(exchange, routingKey, props, message);
    log.debug("Waiting for delivery");
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(20000);
    connection.close();/*from  w w w. j a  va  2 s .com*/
    if (delivery == null || !delivery.getProperties().getCorrelationId().equals(corrId)) {
        return null;
    } else {
        byte[] response = delivery.getBody();
        return response;

    }
}

From source file:uk.org.openeyes.oink.proxy.test.support.RabbitServer.java

License:Open Source License

@Override
public void run() {
    try {/*from  www  .  jav  a 2  s  .  com*/
        log.info("RabbitServer started");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(exchange, "direct", true, true, null);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, exchange, routingKey);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);

        while (!stop) {
            // Wait for RabbitQueue delivery
            QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);
            if (delivery != null) {
                log.info("Message received");
                receivedMessage = delivery.getBody();
                stop = true;
            }
        }

        if (receivedMessage == null) {
            log.warn("RabbitServer stopping before any message was received");
        }

    } catch (Exception e) {
        log.error("RabbitServer threw an exception:" + e);
        e.printStackTrace();
    }

}

From source file:v2.APAdmin.java

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

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

    channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//ww w  .  ja  v  a  2 s .c o  m
        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 '" + envelope.getRoutingKey() + "':'" + message + "'");
            //REPLY HERE
            String response = "You are in, breh";
            BasicProperties replyProps = new BasicProperties.Builder()
                    .correlationId(properties.getCorrelationId()).build();
            channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes());
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:v2.PBAAdmin.java

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

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

    channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//ww  w  .  ja  va 2 s .  c o  m
        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 '" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:v2.Student.java

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {/* w  w w  .  java  2  s . c  om*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");

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

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String routingKey = "AP.DAT";//getRouting(argv);
        String message = "Plz enroll me, breh";//getMessage(argv);
        String corrId = java.util.UUID.randomUUID().toString();
        String callbackQueueName = channel.queueDeclare().getQueue();
        String response = null;
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(callbackQueueName, true, consumer);

        BasicProperties props = new BasicProperties.Builder().replyTo(callbackQueueName).build();

        channel.basicPublish(EXCHANGE_NAME, routingKey, props, message.getBytes("UTF-8"));
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

        while (true) {
            System.out.println("blocked?");
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println("unblocked");
            System.out.println(delivery);
            response = new String(delivery.getBody());
            System.out.println(response);
            //        if (delivery.getProperties().getCorrelationId().equals(corrId)) {
            //            response = new String(delivery.getBody());
            //            System.out.println(response);
            //            break;
            //        }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}