Example usage for com.rabbitmq.client Connection createChannel

List of usage examples for com.rabbitmq.client Connection createChannel

Introduction

In this page you can find the example usage for com.rabbitmq.client Connection createChannel.

Prototype

Channel createChannel() throws IOException;

Source Link

Document

Create a new channel, using an internally allocated channel number.

Usage

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

/**
 * Sends a message to the specified queue
 * @param queue//ww w  .  j  a  va2 s. c  om
 * @param in
 * @param host
 * @throws IOException
 * @throws InterruptedException 
 * @throws TimeoutException
 */
public void sendMessage(String queue, Object in, String host)
        throws IOException, InterruptedException, TimeoutException {
    String message;
    Builder propBuilder = MessageProperties.MINIMAL_PERSISTENT_BASIC.builder();
    if (in.getClass().equals(String.class)) {
        message = (String) in;
    } else {
        message = Utils.objectToJson(in);
        if (in.getClass().equals(WorkerState.class)) {
            host = ((WorkerState) in).getResource().getHostName();
        }
    }
    if (host != null) {
        Map<String, Object> headers = new HashMap();
        headers.put("host", host);
        propBuilder.headers(headers);
    }
    BasicProperties mProp = propBuilder.build();

    Connection connectionSend = getRmqConn();
    Channel channel = connectionSend.createChannel();
    channel.confirmSelect();
    channel.queueDeclare(queue, true, false, false, null);
    channel.basicPublish("", queue, mProp, message.getBytes());
    channel.waitForConfirmsOrDie(5000);
    logger.info(queue + " sent: " + message);
    channel.close();
    closeRmqConn(connectionSend);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public void sendFile(String queue, String host, File f)
        throws IOException, InterruptedException, TimeoutException {
    Map<String, Object> headers = new HashMap();
    headers.put("host", host);
    BasicProperties mProp = MessageProperties.MINIMAL_PERSISTENT_BASIC.builder().headers(headers).build();
    Connection connectionSend = getRmqConn();
    Channel channel = connectionSend.createChannel();
    channel.confirmSelect();//from   w ww.jav  a  2  s. c  om
    channel.queueDeclare(queue, true, false, false, null);
    channel.basicPublish("", queue, mProp, Files.readAllBytes(f.toPath()));
    channel.waitForConfirmsOrDie(5000);
    logger.info(queue + " file: " + f.getAbsolutePath());
    channel.close();
    closeRmqConn(connectionSend);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public void cleanQueue(String queue, String match) throws IOException, InterruptedException {
    logger.trace(queue.concat(": ").concat(match));
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);
    Set seen = new HashSet();
    while (delivery != null) {
        String body = new String(delivery.getBody());
        if (seen.contains(body)) {
            break;
        }/*from www .  j  a v a  2s  . com*/
        if (body.contains(match)) {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else {
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
            seen.add(body);
        }
        delivery = consumer.nextDelivery(1000);
    }
    channel.close();
    closeRmqConn(connectionRcv);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public List<File> getFiles(String queue, Path outFolder, boolean ack) throws IOException, InterruptedException {
    List files = new ArrayList();
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);

    Set seen = new HashSet();

    while (delivery != null) {
        String host = delivery.getProperties().getHeaders().get("host").toString();
        File outTo = Paths.get(outFolder.toString(), host + ".tar.gz").toFile();
        FileUtils.writeByteArrayToFile(outTo, delivery.getBody());
        if (ack) {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else {//  w ww  .  j a va2 s.c  om
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
        }
        if (seen.contains(delivery.getProperties().getHeaders().get("host"))) {
            break;
        }
        seen.add(delivery.getProperties().getHeaders().get("host"));
        files.add(outTo);
        logger.info(queue + " retrieved: " + outTo.getAbsolutePath());
        delivery = consumer.nextDelivery(1000);
    }
    logger.warn("getFiles done");
    channel.close();
    closeRmqConn(connectionRcv);
    return files;
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public WorkerState getWorkerState(String queue, long wait) throws IOException, InterruptedException {
    WorkerState ws = null;//ww w.ja  v  a  2 s . c o m
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery;
    if (wait == -1) {
        delivery = consumer.nextDelivery(); // will block until response
    } else {
        delivery = consumer.nextDelivery(wait);
    }
    if (delivery != null) {
        String message = new String(delivery.getBody());
        ws = (WorkerState) Utils.jsonToObject(message, WorkerState.class);
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
    channel.close();
    closeRmqConn(connectionRcv);
    return ws;
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

/**
 * Gets a single message from a queue, ideal for getting an item of work.
 * @param queue//from  w  ww .  j ava  2s  . c o  m
 * @param wait
 * @return A JSON string representing an object, you need to know what type of object the queue will return and handle this outside of here
 * @throws IOException
 * @throws InterruptedException 
 */
public String getMessageString(String queue, long wait) throws IOException, InterruptedException {
    String message = null;
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery;
    if (wait == -1) {
        delivery = consumer.nextDelivery(); // will block until response
    } else {
        delivery = consumer.nextDelivery(wait);
    }
    if (delivery != null) {
        message = new String(delivery.getBody());
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        logger.info(queue + " recieved: " + message);
    }
    channel.close();
    closeRmqConn(connectionRcv);
    return message;
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public void removeFromStateQueue(String queue, String hostToRemove) throws IOException, InterruptedException {
    logger.trace(queue.concat(": ").concat(hostToRemove));
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    int maxTries = channel.queueDeclare(queue, true, false, false, null).getMessageCount();
    logger.trace("Queue : messages\t" + queue + " : " + maxTries);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(200);
    logger.trace(maxTries);/*from ww w  .j  ava2  s . co  m*/
    while (delivery != null && maxTries > 0) {
        // the toString in the middle of this is needed as it is wrapped with another type that can hold 4GB
        Map<String, Object> headers = delivery.getProperties().getHeaders();
        if (headers != null && headers.get("host").toString().equals(hostToRemove)) {
            logger.trace(headers.get("host").toString().concat(" remove"));
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else {
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
        }
        delivery = consumer.nextDelivery(200);
        maxTries--;
    }
    channel.close();
    closeRmqConn(connectionRcv);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

/**
 * Gets all the messages in a queue, best for queues which receive status updates.
 * @param queue// w w  w.j  a v  a  2 s .  com
 * @param wait
 * @return List of JSON strings representing objects, you need to know what type of object the queue will return and handle this outside of here
 * @throws IOException
 * @throws InterruptedException 
 */
public List<String> getMessageStrings(String queue, long wait) throws IOException, InterruptedException {
    List<String> responses = new ArrayList();
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, true, consumer);
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(wait);
        if (delivery == null) {
            break;
        }
        String message = new String(delivery.getBody());
        logger.info(queue + " recieved: " + message);
        responses.add(message);
    }
    channel.close();
    closeRmqConn(connectionRcv);
    return responses;
}

From source file:uk.org.openeyes.oink.hl7v2.test.Hl7ITSupport.java

License:Open Source License

protected static Channel getChannel(ConnectionFactory factory) throws IOException {
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    return channel;
}

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// w  w  w  . j  av 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);

}