Search in sources :

Example 36 with TemporaryQueue

use of javax.jms.TemporaryQueue in project wildfly by wildfly.

the class ScopedInjectedJMSContextTestCase method requestScoped.

/**
 * Test that a request-scoped JMSContext is properly cleaned up after the transaction completion.
 */
@Test
public void requestScoped() throws Exception {
    String text = UUID.randomUUID().toString();
    try (JMSContext context = factory.createContext()) {
        TemporaryQueue tempQueue = context.createTemporaryQueue();
        // send a request/reply message to ensure that the MDB as received the message
        // and set its consumer field
        context.createProducer().setJMSReplyTo(tempQueue).setDeliveryMode(NON_PERSISTENT).send(queueForRequestScope, text);
        JMSConsumer consumer = context.createConsumer(tempQueue);
        String reply = consumer.receiveBody(String.class, TimeoutUtil.adjust(5000));
        assertNotNull(reply);
        JMSConsumer consumerInMDB = RequestScopedMDB.consumer;
        assertNotNull(consumerInMDB);
        try {
            consumerInMDB.receiveBody(String.class);
            fail("the EJB must throw an exception as its injected JMSContext must be closed after the MDB handled the message");
        } catch (Exception e) {
        }
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 37 with TemporaryQueue

use of javax.jms.TemporaryQueue in project wildfly by wildfly.

the class InjectedJMSContextTestCase method sendAndReceiveWithContext.

@Test
public void sendAndReceiveWithContext() throws JMSException {
    String text = UUID.randomUUID().toString();
    try (JMSContext context = factory.createContext()) {
        TemporaryQueue tempQueue = context.createTemporaryQueue();
        context.createProducer().send(tempQueue, text);
        assertMessageIsReceived(tempQueue, context, text, false);
    }
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 38 with TemporaryQueue

use of javax.jms.TemporaryQueue in project wildfly by wildfly.

the class MDBTestCase method doDeliveryActive.

private void doDeliveryActive(Destination destination, String mdbName) throws Exception {
    // ReplyingMDB has been deployed with deliveryActive set to false
    assertMDBDeliveryIsActive(mdbName, false);
    Connection connection = null;
    try {
        connection = cf.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        // send a message to the MDB
        MessageProducer producer = session.createProducer(destination);
        Message message = session.createMessage();
        message.setJMSReplyTo(replyQueue);
        producer.send(message);
        // the MDB did not reply to the message because its delivery is not active
        MessageConsumer consumer = session.createConsumer(replyQueue);
        Message reply = consumer.receive(TIMEOUT);
        assertNull(reply);
        executeMDBOperation(mdbName, "start-delivery");
        assertMDBDeliveryIsActive(mdbName, true);
        // WFLY-4470 check duplicate message when start delivery twice. Last assertNull(reply) should still be valid
        executeMDBOperation(mdbName, "start-delivery");
        // the message was delivered to the MDB which replied
        reply = consumer.receive(TIMEOUT);
        assertNotNull(reply);
        assertEquals(message.getJMSMessageID(), reply.getJMSCorrelationID());
        executeMDBOperation(mdbName, "stop-delivery");
        assertMDBDeliveryIsActive(mdbName, false);
        // send again a message to the MDB
        message = session.createMessage();
        message.setJMSReplyTo(replyQueue);
        producer.send(message);
        // the MDB did not reply to the message because its delivery is not active
        reply = consumer.receive(TIMEOUT);
        assertNull(reply);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 39 with TemporaryQueue

use of javax.jms.TemporaryQueue in project wildfly by wildfly.

the class ContainerManagedTransactionNotSupportedTestCase method doSetRollbackOnlyInContainerManagedTransactionNotSupportedMDBThrowsIllegalStateException.

private void doSetRollbackOnlyInContainerManagedTransactionNotSupportedMDBThrowsIllegalStateException(Destination destination) throws Exception {
    try (JMSContext context = cf.createContext()) {
        TemporaryQueue replyTo = context.createTemporaryQueue();
        String text = UUID.randomUUID().toString();
        TextMessage message = context.createTextMessage(text);
        message.setJMSReplyTo(replyTo);
        context.createProducer().send(destination, message);
        Message reply = context.createConsumer(replyTo).receive(adjust(5000));
        assertNotNull(reply);
        assertEquals(message.getJMSMessageID(), reply.getJMSCorrelationID());
        assertTrue("messageDrivenContext.setRollbackOnly() did not throw the expected IllegalStateException", reply.getBooleanProperty(EXCEPTION_PROP_NAME));
    }
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 40 with TemporaryQueue

use of javax.jms.TemporaryQueue in project wildfly by wildfly.

the class JMSHelper method assertSendAndReceiveTextMessage.

public static void assertSendAndReceiveTextMessage(ConnectionFactory cf, Destination destination, String text) throws JMSException {
    try (JMSContext context = cf.createContext(AUTO_ACKNOWLEDGE)) {
        TemporaryQueue replyTo = context.createTemporaryQueue();
        context.createProducer().setJMSReplyTo(replyTo).send(destination, text);
        String replyText = context.createConsumer(replyTo).receiveBody(String.class, TimeoutUtil.adjust(5000));
        assertEquals(text, replyText);
    }
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext)

Aggregations

TemporaryQueue (javax.jms.TemporaryQueue)91 Session (javax.jms.Session)56 Test (org.junit.Test)47 Connection (javax.jms.Connection)45 MessageProducer (javax.jms.MessageProducer)38 MessageConsumer (javax.jms.MessageConsumer)35 Message (javax.jms.Message)27 JMSException (javax.jms.JMSException)25 TextMessage (javax.jms.TextMessage)24 JMSContext (javax.jms.JMSContext)13 BytesMessage (javax.jms.BytesMessage)10 Destination (javax.jms.Destination)8 Queue (javax.jms.Queue)8 ConnectionFactory (javax.jms.ConnectionFactory)7 ObjectMessage (javax.jms.ObjectMessage)7 JMSConsumer (javax.jms.JMSConsumer)6 TemporaryTopic (javax.jms.TemporaryTopic)6 HashMap (java.util.HashMap)5 MapMessage (javax.jms.MapMessage)5 StreamMessage (javax.jms.StreamMessage)5