Search in sources :

Example 6 with JMSConsumer

use of javax.jms.JMSConsumer 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, 1000);
        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 7 with JMSConsumer

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

the class BeanManagedMessageConsumer method receive.

public boolean receive(Destination destination, String expectedText) throws Exception {
    transaction.begin();
    JMSConsumer consumer = context.createConsumer(destination);
    String text = consumer.receiveBody(String.class, 1000);
    assertNotNull(text);
    assertEquals(expectedText, text);
    transaction.commit();
    try {
        consumer.receiveBody(String.class, 1000);
        Assert.fail("call must fail as the injected JMSContext is closed when the transaction is committed");
    } catch (JMSRuntimeException e) {
    // exception is expected
    } catch (Exception e) {
        throw e;
    }
    return true;
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSRuntimeException(javax.jms.JMSRuntimeException) JMSRuntimeException(javax.jms.JMSRuntimeException)

Example 8 with JMSConsumer

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

the class ClusteredMessagingTestCase method testClusteredTopic.

@Test
public void testClusteredTopic() throws Exception {
    InitialContext contextFromServer0 = createJNDIContextFromServer0();
    InitialContext contextFromServer1 = createJNDIContextFromServer1();
    try (JMSContext jmsContext0 = createJMSContext(createJNDIContextFromServer0());
        JMSContext jmsContext1 = createJMSContext(createJNDIContextFromServer1())) {
        JMSConsumer consumer0 = jmsContext0.createConsumer((Destination) contextFromServer0.lookup(jmsTopicLookup));
        JMSConsumer consumer1 = jmsContext1.createConsumer((Destination) contextFromServer1.lookup(jmsTopicLookup));
        String text = UUID.randomUUID().toString();
        // WIP test if the problem is that the view is not yet propagated
        Thread.sleep(ClusteringTestConstants.GRACE_TIME_TO_MEMBERSHIP_CHANGE);
        // send a message to the topic on server 0
        sendMessage(contextFromServer0, jmsTopicLookup, text);
        // consumers receive it on both servers
        receiveMessage(consumer0, text);
        receiveMessage(consumer1, text);
        String anotherText = UUID.randomUUID().toString();
        // send another message to topic on server 1
        sendMessage(contextFromServer1, jmsTopicLookup, anotherText);
        // consumers receive it on both servers
        receiveMessage(consumer0, anotherText);
        receiveMessage(consumer1, anotherText);
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 9 with JMSConsumer

use of javax.jms.JMSConsumer 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 10 with JMSConsumer

use of javax.jms.JMSConsumer in project tomee by apache.

the class JMS2AMQTest method receiveGetBody.

@Test
public void receiveGetBody() throws InterruptedException {
    final String text = TEXT + "2";
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final CountDownLatch ready = new CountDownLatch(1);
    final CountDownLatch over = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            {
                setName(JMS2AMQTest.class.getName() + ".receiveGetBody#receiver");
            }
            try (final JMSContext context = cf.createContext()) {
                try (final JMSConsumer consumer = context.createConsumer(destination2)) {
                    ready.countDown();
                    final Message receive = consumer.receive(TimeUnit.MINUTES.toMillis(1));
                    assertEquals(text, receive.getBody(String.class));
                }
            } catch (final Throwable ex) {
                error.set(ex);
            } finally {
                over.countDown();
            }
        }
    }.start();
    ready.await(1, TimeUnit.MINUTES);
    // just to ensure we called receive already
    sleep(150);
    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination2, text);
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
    over.await(1, TimeUnit.MINUTES);
    // ensure we got the message and no exception
    final Throwable exception = error.get();
    if (exception != null) {
        exception.printStackTrace();
    }
    assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Aggregations

JMSConsumer (javax.jms.JMSConsumer)17 JMSContext (javax.jms.JMSContext)15 Test (org.junit.Test)8 Destination (javax.jms.Destination)5 Message (javax.jms.Message)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ConnectionFactory (javax.jms.ConnectionFactory)4 JMSRuntimeException (javax.jms.JMSRuntimeException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 TemporaryQueue (javax.jms.TemporaryQueue)3 TextMessage (javax.jms.TextMessage)3 MessageListener (javax.jms.MessageListener)2 ArrayList (java.util.ArrayList)1 PostConstruct (javax.annotation.PostConstruct)1 RequestScoped (javax.enterprise.context.RequestScoped)1 JMSException (javax.jms.JMSException)1 JMSProducer (javax.jms.JMSProducer)1 InitialContext (javax.naming.InitialContext)1 ContextsService (org.apache.webbeans.spi.ContextsService)1 After (org.junit.After)1