Search in sources :

Example 31 with JMSContext

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

the class LegacyJMSTestCase method doSendAndReceive.

private void doSendAndReceive(String connectionFactoryLookup, String destinationLoookup) throws Exception {
    Destination destination = (Destination) remoteContext.lookup(destinationLoookup);
    assertNotNull(destination);
    ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup(connectionFactoryLookup);
    assertNotNull(cf);
    try (JMSContext producerContext = cf.createContext("guest", "guest");
        JMSContext consumerContext = cf.createContext("guest", "guest")) {
        final CountDownLatch latch = new CountDownLatch(10);
        final List<String> result = new ArrayList<String>();
        JMSConsumer consumer = consumerContext.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                TextMessage msg = (TextMessage) message;
                try {
                    result.add(msg.getText());
                    latch.countDown();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        JMSProducer producer = producerContext.createProducer();
        for (int i = 0; i < 10; i++) {
            String text = "Test" + i;
            producer.send(destination, text);
        }
        assertTrue(latch.await(3, SECONDS));
        assertEquals(10, result.size());
        for (int i = 0; i < result.size(); i++) {
            assertEquals("Test" + i, result.get(i));
        }
    }
}
Also used : Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionFactory(javax.jms.ConnectionFactory) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 32 with JMSContext

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

the class InjectedJMSContext method getDelegate.

/**
 * Return the actual JMSContext used by this injection.
 *
 * The use of the correct AbstractJMSContext (one with the @RequestScoped, the other
 * with the @TransactionScoped) is determined by the presence on an active transaction.
 */
@Override
JMSContext getDelegate() {
    boolean inTx = isInTransaction();
    AbstractJMSContext jmsContext = inTx ? transactedJMSContext.get() : requestedJMSContext;
    ROOT_LOGGER.debugf("using %s to create the injected JMSContext", jmsContext, id);
    ConnectionFactory connectionFactory = getConnectionFactory();
    JMSContext contextInstance = jmsContext.getContext(id, info, connectionFactory);
    // Correct phase to call close is afterCompletion {@see TransactionSynchronizationRegistry.registerInterposedSynchronization}
    if (inTx) {
        TransactedJMSContext transactedJMSContext = (TransactedJMSContext) jmsContext;
        transactedJMSContext.registerCleanUpListener(transactionSynchronizationRegistry, contextInstance);
    }
    return contextInstance;
}
Also used : JMSConnectionFactory(javax.jms.JMSConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) JMSContext(javax.jms.JMSContext)

Example 33 with JMSContext

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

the class NotClosingInjectedContextTestCase method testLeakingConnection.

@Test
public void testLeakingConnection() throws Exception {
    // length of log before execution
    String sizeBefore = getLogLineCount(managementClient.getControllerClient());
    try (JMSContext context = factory.createContext();
        JMSConsumer consumer = context.createConsumer(queueVerify)) {
        int j = 0;
        while (true) {
            String t = consumer.receiveBody(String.class, adjust(2000));
            if (t == null) {
                LOGGER.info("Received null message");
                break;
            } else {
                LOGGER.info("Received message:" + (++j));
            }
        }
        Assert.assertEquals("There should be 2 received messages.", 2, j);
        // size of log after execution
        String sizeAfter = getLogLineCount(managementClient.getControllerClient());
        long difference = Long.parseLong(sizeAfter) - Long.parseLong(sizeBefore);
        // validate, that there is no "IJ000100: Closing a connection for you. Please close them yourself"
        List<ModelNode> lines = getLogs(managementClient.getControllerClient(), difference);
        for (ModelNode line : lines) {
            if (line.asString().contains("IJ000100:")) {
                Assert.fail("JMS context is not closed");
            }
        }
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) ModelNode(org.jboss.dmr.ModelNode) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 34 with JMSContext

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

the class NotClosingInjectedContextTestCase method tearDown.

@After
public void tearDown() throws JMSException {
    // drain the queue to remove any pending messages from it
    try (JMSContext context = factory.createContext()) {
        JMSConsumer consumer = context.createConsumer(queue);
        Message m;
        do {
            m = consumer.receiveNoWait();
        } while (m != null);
        consumer = context.createConsumer(queueVerify);
        do {
            m = consumer.receiveNoWait();
        } while (m != null);
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) JMSContext(javax.jms.JMSContext) After(org.junit.After)

Example 35 with JMSContext

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

Aggregations

JMSContext (javax.jms.JMSContext)120 Test (org.junit.Test)57 JMSConsumer (javax.jms.JMSConsumer)44 ConnectionFactory (javax.jms.ConnectionFactory)35 Destination (javax.jms.Destination)30 TextMessage (javax.jms.TextMessage)28 JMSProducer (javax.jms.JMSProducer)27 Message (javax.jms.Message)18 JMSRuntimeException (javax.jms.JMSRuntimeException)13 TemporaryQueue (javax.jms.TemporaryQueue)13 InitialContext (javax.naming.InitialContext)12 JMSException (javax.jms.JMSException)10 Queue (javax.jms.Queue)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)7 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 QueueConnectionFactory (javax.jms.QueueConnectionFactory)4 Context (javax.naming.Context)4 NamingException (javax.naming.NamingException)4