Search in sources :

Example 21 with GetResponse

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

the class CcRoutes method expect.

private void expect(String[] expectedQueues, boolean usedCc) throws IOException {
    GetResponse getResponse;
    List<String> expectedList = Arrays.asList(expectedQueues);
    for (String q : queues) {
        getResponse = basicGet(q);
        if (expectedList.contains(q)) {
            assertNotNull(getResponse);
            assertEquals(0, getResponse.getMessageCount());
            Map<?, ?> headers = getResponse.getProps().getHeaders();
            if (headers != null) {
                assertEquals(usedCc, headers.containsKey("CC"));
                assertFalse(headers.containsKey("BCC"));
            }
        } else {
            assertNull(getResponse);
        }
    }
}
Also used : GetResponse(com.rabbitmq.client.GetResponse)

Example 22 with GetResponse

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

the class AlternateExchange method checkGet.

/**
 * Perform an auto-acking 'basic.get' on each of the queues named
 * in {@link #resources} and check whether a message can be
 * retrieved when expected.
 *
 * @param expected an array of booleans that is zipped with {@link
 *        #resources} and indicates whether a messages is expected
 *        to be retrievable from the respective queue
 */
protected void checkGet(boolean[] expected) throws IOException {
    for (int i = 0; i < resources.length; i++) {
        String q = resources[i];
        GetResponse r = channel.basicGet(q, true);
        assertEquals("check " + q, expected[i], r != null);
    }
}
Also used : GetResponse(com.rabbitmq.client.GetResponse)

Example 23 with GetResponse

use of com.rabbitmq.client.GetResponse in project nifi by apache.

the class AMQPConsumerTest method validateSuccessfullConsumeWithEmptyQueueDefaultExchange.

@Test
public void validateSuccessfullConsumeWithEmptyQueueDefaultExchange() throws Exception {
    Map<String, List<String>> routingMap = new HashMap<>();
    routingMap.put("queue1", Arrays.asList("queue1"));
    Map<String, String> exchangeToRoutingKeymap = new HashMap<>();
    exchangeToRoutingKeymap.put("", "queue1");
    Connection conn = new TestConnection(exchangeToRoutingKeymap, routingMap);
    try (AMQPConsumer consumer = new AMQPConsumer(conn, "queue1")) {
        GetResponse response = consumer.consume();
        assertNull(response);
    }
}
Also used : HashMap(java.util.HashMap) Connection(com.rabbitmq.client.Connection) List(java.util.List) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.Test)

Example 24 with GetResponse

use of com.rabbitmq.client.GetResponse in project nifi by apache.

the class AMQPConsumerTest method validateSuccessfullConsumeWithEmptyQueue.

@Test
public void validateSuccessfullConsumeWithEmptyQueue() throws Exception {
    Map<String, List<String>> routingMap = new HashMap<>();
    routingMap.put("key1", Arrays.asList("queue1"));
    Map<String, String> exchangeToRoutingKeymap = new HashMap<>();
    exchangeToRoutingKeymap.put("myExchange", "key1");
    Connection conn = new TestConnection(exchangeToRoutingKeymap, routingMap);
    conn.createChannel().basicPublish("myExchange", "key1", null, "hello Joe".getBytes());
    try (AMQPConsumer consumer = new AMQPConsumer(conn, "queue1")) {
        GetResponse response = consumer.consume();
        assertNotNull(response);
    }
}
Also used : HashMap(java.util.HashMap) Connection(com.rabbitmq.client.Connection) List(java.util.List) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.Test)

Example 25 with GetResponse

use of com.rabbitmq.client.GetResponse in project wso2-synapse by wso2.

the class RabbitMQConsumer method receive.

public MessageContext receive() {
    if (!checkConnection()) {
        if (!reconnect()) {
            if (logger.isDebugEnabled()) {
                logger.debug(getId() + " cannot receive message from store. Can not reconnect.");
            }
            return null;
        } else {
            logger.info(getId() + " reconnected to store.");
            isReceiveError = false;
        }
    }
    // setting channel
    if (channel != null) {
        if (!channel.isOpen()) {
            if (!setChannel()) {
                logger.info(getId() + " unable to create the channel.");
                return null;
            }
        }
    } else {
        if (!setChannel()) {
            logger.info(getId() + " unable to create the channel.");
            return null;
        }
    }
    // receive messages
    try {
        GetResponse delivery = null;
        delivery = channel.basicGet(queueName, false);
        if (delivery != null) {
            // deserilizing message
            StorableMessage storableMessage = null;
            ByteArrayInputStream bis = new ByteArrayInputStream(delivery.getBody());
            ObjectInput in = new ObjectInputStream(bis);
            try {
                storableMessage = (StorableMessage) in.readObject();
            } catch (ClassNotFoundException e) {
                logger.error(getId() + "unable to read the stored message" + e);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
            bis.close();
            in.close();
            org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
            MessageContext synapseMc = store.newSynapseMc(axis2Mc);
            synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
            updateCache(delivery, synapseMc, null, false);
            if (logger.isDebugEnabled()) {
                logger.debug(getId() + " Received MessageId:" + delivery.getProps().getMessageId());
            }
            return synapseMc;
        }
    } catch (ShutdownSignalException sse) {
        logger.error(getId() + " connection error when receiving messages" + sse);
    } catch (IOException ioe) {
        logger.error(getId() + " connection error when receiving messages" + ioe);
    }
    return null;
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) MessageContext(org.apache.synapse.MessageContext) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

GetResponse (com.rabbitmq.client.GetResponse)55 Test (org.junit.Test)27 Channel (com.rabbitmq.client.Channel)10 IOException (java.io.IOException)10 Envelope (com.rabbitmq.client.Envelope)8 Connection (com.rabbitmq.client.Connection)7 Message (de.gessnerfl.rabbitmq.queue.management.model.Message)6 HashMap (java.util.HashMap)6 AMQP (com.rabbitmq.client.AMQP)5 LongString (com.rabbitmq.client.LongString)5 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)4 BasicProperties (de.gessnerfl.rabbitmq.queue.management.model.BasicProperties)4 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Queue (javax.jms.Queue)3 QueueSender (javax.jms.QueueSender)3 QueueSession (javax.jms.QueueSession)3