Search in sources :

Example 46 with JMSProducer

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

JMSProducer (javax.jms.JMSProducer)46 Test (org.junit.Test)36 JMSContext (javax.jms.JMSContext)27 TextMessage (javax.jms.TextMessage)24 JMSConsumer (javax.jms.JMSConsumer)23 ConnectionFactory (javax.jms.ConnectionFactory)11 Message (javax.jms.Message)11 BytesMessage (javax.jms.BytesMessage)8 Queue (javax.jms.Queue)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 Destination (javax.jms.Destination)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 StreamMessage (javax.jms.StreamMessage)6 JMSException (javax.jms.JMSException)5 InitialContext (javax.naming.InitialContext)5 JMSRuntimeException (javax.jms.JMSRuntimeException)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 Topic (javax.jms.Topic)3 Path (java.nio.file.Path)2 CompletionListener (javax.jms.CompletionListener)2