Search in sources :

Example 41 with QueueSender

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

the class RecoverMessagesIT method testRecoverTextMessageAsyncSync.

@Test
public void testRecoverTextMessageAsyncSync() throws Exception {
    final ArrayList<Message> messages = new ArrayList<Message>();
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    final CountDownLatch latch = new CountDownLatch(2);
    queueReceiver.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            messages.add(message);
            latch.countDown();
        }
    });
    // allow subscription to take place
    Thread.sleep(100);
    // we should have received one message
    assertEquals(1, messages.size());
    TextMessage tmsg1 = (TextMessage) messages.get(0);
    assertFalse(tmsg1.getJMSRedelivered());
    queueSession.recover();
    latch.await(1000, TimeUnit.MILLISECONDS);
    // we should have received two messages
    // There is no synchronisation so no guarantee we see the latest messages!
    assertEquals(2, messages.size());
    TextMessage tmsg2 = (TextMessage) messages.get(1);
    assertEquals(tmsg1, tmsg2);
    assertTrue(tmsg2.getJMSRedelivered());
    tmsg2.acknowledge();
    queueReceiver.setMessageListener(null);
    TextMessage tmsg3 = (TextMessage) queueReceiver.receiveNoWait();
    assertNull(tmsg3);
}
Also used : TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 42 with QueueSender

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

the class RecoverMessagesIT method testRecoverTextMessageSync.

@Test
public void testRecoverTextMessageSync() throws Exception {
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage tmsg1 = (TextMessage) queueReceiver.receive();
    assertFalse(tmsg1.getJMSRedelivered());
    queueSession.recover();
    TextMessage tmsg2 = (TextMessage) queueReceiver.receive();
    assertEquals(tmsg1, tmsg2);
    assertTrue(tmsg2.getJMSRedelivered());
    tmsg2.acknowledge();
    TextMessage tmsg3 = (TextMessage) queueReceiver.receiveNoWait();
    assertNull(tmsg3);
}
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 43 with QueueSender

use of javax.jms.QueueSender 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 44 with QueueSender

use of javax.jms.QueueSender 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 45 with QueueSender

use of javax.jms.QueueSender 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

QueueSender (javax.jms.QueueSender)59 QueueSession (javax.jms.QueueSession)54 Queue (javax.jms.Queue)36 TextMessage (javax.jms.TextMessage)28 QueueReceiver (javax.jms.QueueReceiver)25 JMSException (javax.jms.JMSException)14 Message (javax.jms.Message)14 QueueConnection (javax.jms.QueueConnection)14 Test (org.junit.jupiter.api.Test)14 Serializable (java.io.Serializable)11 ObjectMessage (javax.jms.ObjectMessage)10 Test (org.junit.Test)8 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)5 InitialContext (javax.naming.InitialContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MapMessage (javax.jms.MapMessage)4 QueueConnectionFactory (javax.jms.QueueConnectionFactory)4 IFSAQueue (com.ing.ifsa.IFSAQueue)3 GetResponse (com.rabbitmq.client.GetResponse)3