Search in sources :

Example 21 with JMSRuntimeException

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

Example 22 with JMSRuntimeException

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

the class JMS2AMQTest method sendMessageToMdb.

@Test
public void sendMessageToMdb() throws Exception {
    try (final JMSContext context = cf.createContext()) {
        Message message = context.createMessage();
        message.setStringProperty("text", TEXT);
        context.createProducer().send(destination, message);
        assertTrue(Listener.sync());
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 23 with JMSRuntimeException

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

the class JMS2AMQTest method sendToMdb.

@Test
public void sendToMdb() throws Exception {
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination, TEXT);
        assertTrue(Listener.sync());
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
}
Also used : JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 24 with JMSRuntimeException

use of javax.jms.JMSRuntimeException in project pentaho-kettle by pentaho.

the class JmsStreamSource method receiveLoop.

/**
 * Will receive messages from consumer.  If timeout is hit, consumer.receive(timeout)
 * will return null, and the observable will be completed.
 */
private void receiveLoop() {
    Message message;
    try {
        while (!closed.get() && (message = consumer.receive(receiverTimeout)) != null) {
            streamStep.logDebug(message.toString());
            Date date = new Date(message.getJMSTimestamp());
            DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss a");
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            String jmsTimestamp = formatter.format(date);
            acceptRows(singletonList(Arrays.asList(message.getBody(Object.class), jmsDelegate.destinationName, message.getJMSMessageID(), jmsTimestamp, message.getJMSRedelivered())));
        }
    } catch (JMSRuntimeException | JMSException jmsException) {
        error(jmsException);
    } finally {
        super.close();
        if (!closed.get()) {
            close();
            streamStep.logBasic(getString(PKG, "JmsStreamSource.HitReceiveTimeout"));
        }
    }
}
Also used : Message(javax.jms.Message) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JMSException(javax.jms.JMSException) BaseMessages.getString(org.pentaho.di.i18n.BaseMessages.getString) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) JMSRuntimeException(javax.jms.JMSRuntimeException)

Example 25 with JMSRuntimeException

use of javax.jms.JMSRuntimeException in project javaee7-samples by javaee-samples.

the class MessageSenderAsync method sendMessage.

/**
 * Send a message to the JMS queue. Prin
 *
 * @param message the contents of the message.
 * @throws JMSRuntimeException if an error occurs in accessing the queue.
 */
public void sendMessage(String message) throws JMSRuntimeException {
    JMSProducer producer = context.createProducer();
    try {
        producer.setAsync(new CompletionListener() {

            @Override
            public void onCompletion(Message msg) {
                try {
                    System.out.println(msg.getBody(String.class));
                } catch (JMSException ex) {
                    Logger.getLogger(MessageSenderAsync.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            @Override
            public void onException(Message msg, Exception e) {
                try {
                    System.out.println(msg.getBody(String.class));
                } catch (JMSException ex) {
                    Logger.getLogger(MessageSenderAsync.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    } catch (JMSRuntimeException ex) {
        System.out.println("Caught RuntimeException trying to invoke setAsync - not permitted in Java EE. Resorting to synchronous sending...");
    }
    producer.send(asyncQueue, message);
}
Also used : Message(javax.jms.Message) CompletionListener(javax.jms.CompletionListener) JMSProducer(javax.jms.JMSProducer) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) JMSRuntimeException(javax.jms.JMSRuntimeException) JMSRuntimeException(javax.jms.JMSRuntimeException)

Aggregations

JMSRuntimeException (javax.jms.JMSRuntimeException)25 Test (org.junit.Test)13 JMSContext (javax.jms.JMSContext)12 Message (javax.jms.Message)9 JMSException (javax.jms.JMSException)8 JMSConsumer (javax.jms.JMSConsumer)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 JMSProducer (javax.jms.JMSProducer)4 TextMessage (javax.jms.TextMessage)4 Destination (javax.jms.Destination)3 MessageFormatRuntimeException (javax.jms.MessageFormatRuntimeException)3 RequestScoped (javax.enterprise.context.RequestScoped)2 JMSSecurityException (javax.jms.JMSSecurityException)2 JMSSecurityRuntimeException (javax.jms.JMSSecurityRuntimeException)2 ActiveMQPropertyConversionException (org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ContextsService (org.apache.webbeans.spi.ContextsService)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1