Search in sources :

Example 6 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class TtlIT method publishAmqpConsumeJms.

private Message publishAmqpConsumeJms(boolean expiration) throws Exception {
    Channel channel = connection.createChannel();
    channel.queueDeclare(AMQP_QUEUE_NAME, // durable
    false, // non-exclusive
    false, // autoDelete
    true, // options
    null);
    Map<String, Object> hdrs = new HashMap<>();
    // 
    hdrs.put("JMSType", "TextMessage");
    AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
    propsBuilder.headers(hdrs);
    if (expiration) {
        propsBuilder.expiration("10000");
    }
    String content = UUID.randomUUID().toString();
    channel.basicPublish("", AMQP_QUEUE_NAME, propsBuilder.build(), content.getBytes());
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = new RMQDestination(AMQP_QUEUE_NAME, null, null, AMQP_QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message received");
    assertEquals(content, message.getText(), "Payload doesn't match");
    return message;
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) HashMap(java.util.HashMap) Channel(com.rabbitmq.client.Channel) AMQP(com.rabbitmq.client.AMQP)

Example 7 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class RpcWithAmqpDirectReplyIT method doRpc.

Message doRpc(String messageContent) throws Exception {
    Session session = clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TextMessage message = session.createTextMessage(messageContent);
    message.setJMSCorrelationID(messageContent);
    RMQDestination replyQueue = new RMQDestination("amq.rabbitmq.reply-to", "", "amq.rabbitmq.reply-to", "amq.rabbitmq.reply-to");
    replyQueue.setDeclared(true);
    MessageProducer producer = session.createProducer(session.createQueue(QUEUE_NAME));
    MessageConsumer responseConsumer = session.createConsumer(replyQueue);
    BlockingQueue<Message> queue = new ArrayBlockingQueue<>(1);
    responseConsumer.setMessageListener(msg -> queue.add(msg));
    message.setJMSReplyTo(replyQueue);
    producer.send(message);
    Message response = queue.poll(2, TimeUnit.SECONDS);
    responseConsumer.close();
    return response;
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 8 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class RmqDestinationIT method createRmqDestinationWithArguments.

@Test
void createRmqDestinationWithArguments() throws Exception {
    this.queueConn.start();
    QueueSession session = this.queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = new RMQDestination(QUEUE_NAME, true, false, Collections.singletonMap("x-queue-type", "quorum"));
    QueueSender sender = session.createSender(queue);
    drainQueue(session, queue);
    String body = String.valueOf(System.currentTimeMillis());
    TextMessage msg = session.createTextMessage(body);
    sender.send(msg);
    CountDownLatch consumerLatch = new CountDownLatch(1);
    AtomicReference<TextMessage> message = new AtomicReference<>();
    MessageConsumer consumer = session.createConsumer(queue);
    consumer.setMessageListener(m -> {
        message.set((TextMessage) m);
        consumerLatch.countDown();
    });
    assertThat(consumerLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(message.get().getText()).isEqualTo(body);
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) MessageConsumer(javax.jms.MessageConsumer) QueueSender(javax.jms.QueueSender) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test) RMQDestinationTest(com.rabbitmq.jms.admin.RMQDestinationTest)

Example 9 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendFromJmsAndReceiveJmsTextMessage.

@Test
public void testSendFromJmsAndReceiveJmsTextMessage() throws Exception {
    String queueName = UUID.randomUUID().toString();
    channel.queueDeclare(queueName, // durable
    false, // non-exclusive
    false, // autoDelete
    true, // options
    null);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // write-only AMQP-mapped queue
    Queue queue = new RMQDestination(queueName, "", queueName, null);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    message.setStringProperty("JMSType", "TextMessage");
    message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    queueSender.send(message);
    queueSession.close();
    queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // read-only AMQP-mapped queue
    queue = new RMQDestination(queueName, null, null, queueName);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    channel.queueDelete(queueName);
    assertNotNull(message, "No message received");
    assertEquals(MESSAGE, message.getText(), "Payload doesn't match");
    assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
    assertThat(message.getJMSTimestamp()).isGreaterThan(0);
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 10 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveBytesMessage.

@Test
public void testSendToAmqpAndReceiveBytesMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME, // durable
    false, // exclusive
    true, // autoDelete
    true, // options
    null);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // write-only AMQP-mapped queue
    Queue queue = new RMQDestination(QUEUE_NAME, "", QUEUE_NAME, null);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    queueSender.setPriority(9);
    BytesMessage message = queueSession.createBytesMessage();
    message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    message.writeBytes(BYTE_ARRAY);
    queueSender.send(message);
    queueConn.close();
    GetResponse response = channel.basicGet(QUEUE_NAME, false);
    assertNotNull(response, "basicGet failed to retrieve a response");
    byte[] body = response.getBody();
    assertNotNull(body, "body of response is null");
    {
        Map<String, Object> hdrs = response.getProps().getHeaders();
        assertEquals(new HashSet<String>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME)), hdrs.keySet(), "Some keys missing");
        assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
        // toString is a bit wiffy
        assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong");
        assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
    }
    assertArrayEquals(BYTE_ARRAY, body, "Message received not identical to message sent");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) BytesMessage(javax.jms.BytesMessage) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

RMQDestination (com.rabbitmq.jms.admin.RMQDestination)18 Test (org.junit.jupiter.api.Test)8 Queue (javax.jms.Queue)7 QueueSession (javax.jms.QueueSession)7 TextMessage (javax.jms.TextMessage)6 HashMap (java.util.HashMap)5 QueueSender (javax.jms.QueueSender)5 HashSet (java.util.HashSet)4 AMQP (com.rabbitmq.client.AMQP)3 GetResponse (com.rabbitmq.client.GetResponse)3 MessageConsumer (javax.jms.MessageConsumer)3 QueueReceiver (javax.jms.QueueReceiver)3 Map (java.util.Map)2 BytesMessage (javax.jms.BytesMessage)2 JMSException (javax.jms.JMSException)2 Session (javax.jms.Session)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Channel (com.rabbitmq.client.Channel)1 RMQDestinationTest (com.rabbitmq.jms.admin.RMQDestinationTest)1