Search in sources :

Example 16 with JMSConsumer

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

the class JMS2AMQTest method receive.

@Test
public void receive() 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() + ".receive#receiver");
            }
            try (final JMSContext context = cf.createContext()) {
                try (final JMSConsumer consumer = context.createConsumer(destination2)) {
                    ready.countDown();
                    assertEquals(text, consumer.receiveBody(String.class, TimeUnit.MINUTES.toMillis(1)));
                }
            } 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 17 with JMSConsumer

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

the class JMS2AMQTest method cdiListenerAPI.

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

        {
            setName(JMS2AMQTest.class.getName() + ".cdiListenerAPI#receiver");
        }

        @Override
        public void run() {
            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
            contextsService.startContext(RequestScoped.class, null);
            try {
                final JMSConsumer consumer = context.createConsumer(destination3);
                consumer.setMessageListener(new MessageListener() {

                    @Override
                    public void onMessage(final Message message) {
                        try {
                            assertEquals(text, message.getBody(String.class));
                        } catch (final Throwable e) {
                            error.set(e);
                        } finally {
                            over.countDown();
                            consumer.close();
                        }
                    }
                });
                ready.countDown();
            } catch (final Throwable t) {
                error.set(t);
            } finally {
                try {
                    over.await(1, TimeUnit.MINUTES);
                } catch (final InterruptedException e) {
                    Thread.interrupted();
                }
                contextsService.endContext(RequestScoped.class, null);
            }
        }
    }.start();
    ready.await(1, TimeUnit.MINUTES);
    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination3, 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 : ContextsService(org.apache.webbeans.spi.ContextsService) JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageListener(javax.jms.MessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestScoped(javax.enterprise.context.RequestScoped) 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