Search in sources :

Example 41 with QueueReceiver

use of javax.jms.QueueReceiver 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 42 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendFromAmqpAndReceiveBytesMessage.

@Test
public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE, // durable
    false, // non-exclusive
    false, // autoDelete
    true, // options
    null);
    Map<String, Object> hdrs = new HashMap<String, Object>();
    hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    hdrs.put("JMSType", USER_JMS_TYPE_SETTING);
    hdrs.put("JMSPriority", 21);
    hdrs.put("JMSDeliveryMode", 2);
    hdrs.put("DummyProp", 42);
    hdrs.put("rmq.jms.silly", "silly attempt");
    AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(1).priority(6).headers(hdrs).build();
    channel.basicPublish("", QUEUE_NAME_NON_EXCLUSIVE, props, BYTE_ARRAY);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // read-only AMQP-mapped queue
    Queue queue = new RMQDestination(QUEUE_NAME_NON_EXCLUSIVE, null, null, QUEUE_NAME_NON_EXCLUSIVE);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    BytesMessage message = (BytesMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message received");
    byte[] bytes = new byte[BYTE_ARRAY.length + 2];
    int bytesIn = message.readBytes(bytes);
    assertEquals(BYTE_ARRAY.length, bytesIn, "Message payload not correct size");
    byte[] bytesTrunc = new byte[bytesIn];
    System.arraycopy(bytes, 0, bytesTrunc, 0, bytesIn);
    assertArrayEquals(BYTE_ARRAY, bytesTrunc, "Payload doesn't match");
    // override should work
    assertEquals(21, message.getJMSPriority(), "Priority incorrect");
    // override should fail
    assertEquals(1, message.getJMSDeliveryMode(), "Delivery mode incorrect");
    // override should work
    assertEquals(USER_JMS_TYPE_SETTING, message.getJMSType(), "JMSType not set correctly");
    Enumeration<?> propNames = message.getPropertyNames();
    Set<String> propNameSet = new HashSet<String>();
    while (propNames.hasMoreElements()) {
        propNameSet.add((String) propNames.nextElement());
    }
    assertEquals(new HashSet<>(Arrays.asList(USER_STRING_PROPERTY_NAME, "DummyProp")), propNameSet, "Headers not set correctly");
    assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
    assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
    assertThat(message.getJMSTimestamp()).isZero();
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) HashMap(java.util.HashMap) BytesMessage(javax.jms.BytesMessage) AMQP(com.rabbitmq.client.AMQP) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 43 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class TemporaryQueueIT method testQueueSendAndReceiveTwoSessions.

@Test
public void testQueueSendAndReceiveTwoSessions() throws Exception {
    queueConn.start();
    QueueSession queueSession1 = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    QueueSession queueSession2 = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession1.createTemporaryQueue();
    QueueSender queueSender = queueSession1.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession1.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession2.createReceiver(queue);
    message = (TextMessage) queueReceiver.receive(1000);
    assertNotNull(message);
    assertEquals(MESSAGE, message.getText());
}
Also used : 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 44 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class TemporaryQueueIT method testQueueSendAndReceiveSingleSession.

@Test
public void testQueueSendAndReceiveSingleSession() throws Exception {
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createTemporaryQueue();
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    message = (TextMessage) queueReceiver.receive(1000);
    assertNotNull(message);
    assertEquals(MESSAGE, message.getText());
}
Also used : 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 45 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class SimpleQueueMessageDefaultsIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    Message m = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    mtt.check(m, (Serializable) queue);
    mtt.checkAttrs(m, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY);
}
Also used : Serializable(java.io.Serializable) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Aggregations

QueueReceiver (javax.jms.QueueReceiver)56 QueueSession (javax.jms.QueueSession)47 Queue (javax.jms.Queue)40 TextMessage (javax.jms.TextMessage)28 Message (javax.jms.Message)25 QueueSender (javax.jms.QueueSender)25 Test (org.junit.jupiter.api.Test)17 QueueConnection (javax.jms.QueueConnection)13 Serializable (java.io.Serializable)11 JMSException (javax.jms.JMSException)10 Test (org.junit.Test)10 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 BytesMessage (javax.jms.BytesMessage)5 InitialContext (javax.naming.InitialContext)5 Destination (javax.jms.Destination)4 MessageProducer (javax.jms.MessageProducer)4 NamingException (javax.naming.NamingException)4 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)3 Enumeration (java.util.Enumeration)3 HashMap (java.util.HashMap)3