Search in sources :

Example 46 with QueueSender

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

the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveTextMessage.

@Test
public void testSendToAmqpAndReceiveTextMessage() 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);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    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<>(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");
    }
    assertEquals(MESSAGE, new String(body, "UTF-8"), "Message received not identical to message sent");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 47 with QueueSender

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

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

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

Example 50 with QueueSender

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

the class SimpleSendReceiveTransactionIT method testQueueSendAndRollback.

@Test
public void testQueueSendAndRollback() throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(true, Session.DUPS_OK_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);
        queueSession.rollback();
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage message = (TextMessage) queueReceiver.receiveNoWait();
    assertNull(message);
}
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)

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