Search in sources :

Example 1 with CompletionListener

use of javax.jms.CompletionListener in project hono by eclipse.

the class SendReceiveIT method testMalformedTelemetryMessageGetsRejected.

/**
 * Verifies that the Telemetry endpoint rejects a malformed message.
 *
 * @throws Exception if the test fails.
 */
@Test
public void testMalformedTelemetryMessageGetsRejected() throws Exception {
    givenAReceiver();
    givenASender();
    final CountDownLatch rejected = new CountDownLatch(1);
    // prepare consumer to get some credits for sending
    final MessageConsumer messageConsumer = receiver.getTelemetryConsumer();
    messageConsumer.setMessageListener(message -> {
    });
    final MessageProducer messageProducer = sender.getTelemetryProducer();
    // message lacks device ID and registration assertion
    final Message message = sender.newMessage("body", null, null);
    messageProducer.send(message, new CompletionListener() {

        @Override
        public void onException(final Message message, final Exception exception) {
            LOG.debug("failed to send message as expected: {}", exception.getMessage());
            rejected.countDown();
        }

        @Override
        public void onCompletion(final Message message) {
        // should not happen
        }
    });
    assertTrue(rejected.await(DEFAULT_TEST_TIMEOUT, TimeUnit.MILLISECONDS));
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) CompletionListener(javax.jms.CompletionListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 2 with CompletionListener

use of javax.jms.CompletionListener in project activemq-artemis by apache.

the class JMSCompletionListenerExample method main.

public static void main(final String[] args) throws Exception {
    JMSContext jmsContext = null;
    try {
        // Step 2. Perfom a lookup on the queue
        Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?confirmationWindowSize=10240");
        // Step 4.Create a JMS Context
        jmsContext = cf.createContext();
        // Step 5. Create a message producer.
        JMSProducer producer = jmsContext.createProducer();
        final CountDownLatch latch = new CountDownLatch(1);
        // Step 6. We want to send the message Asynchronously and be notified when the Broker receives it so we set a completion handler
        producer.setAsync(new CompletionListener() {

            @Override
            public void onCompletion(Message message) {
                System.out.println("message acknowledged by ActiveMQ");
                latch.countDown();
            }

            @Override
            public void onException(Message message, Exception e) {
                e.printStackTrace();
            }
        });
        // Step 6. Send the Message
        producer.send(queue, "this is a string");
        // Step 7. wait for the Completion handler
        if (!latch.await(5, TimeUnit.SECONDS)) {
            throw new IllegalStateException("Completion listener not called as expected.");
        }
    } finally {
        if (jmsContext != null) {
            jmsContext.close();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) CompletionListener(javax.jms.CompletionListener) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) JMSContext(javax.jms.JMSContext)

Example 3 with CompletionListener

use of javax.jms.CompletionListener in project activemq-artemis by apache.

the class ActiveMQJMSProducer method send.

@Override
public JMSProducer send(Destination destination, Message message) {
    if (message == null) {
        throw new MessageFormatRuntimeException("null message");
    }
    try {
        if (jmsHeaderCorrelationID != null) {
            message.setJMSCorrelationID(jmsHeaderCorrelationID);
        }
        if (jmsHeaderCorrelationIDAsBytes != null && jmsHeaderCorrelationIDAsBytes.length > 0) {
            message.setJMSCorrelationIDAsBytes(jmsHeaderCorrelationIDAsBytes);
        }
        if (jmsHeaderReplyTo != null) {
            message.setJMSReplyTo(jmsHeaderReplyTo);
        }
        if (jmsHeaderType != null) {
            message.setJMSType(jmsHeaderType);
        }
        // XXX HORNETQ-1209 "JMS 2.0" can this be a foreign msg?
        // if so, then "SimpleString" properties will trigger an error.
        setProperties(message);
        if (completionListener != null) {
            CompletionListener wrapped = new CompletionListenerWrapper(completionListener);
            producer.send(destination, message, wrapped);
        } else {
            producer.send(destination, message);
        }
    } catch (JMSException e) {
        throw JmsExceptionUtils.convertToRuntimeException(e);
    }
    return this;
}
Also used : CompletionListener(javax.jms.CompletionListener) JMSException(javax.jms.JMSException) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException)

Example 4 with CompletionListener

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

CompletionListener (javax.jms.CompletionListener)4 JMSException (javax.jms.JMSException)3 Message (javax.jms.Message)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 JMSProducer (javax.jms.JMSProducer)2 ConnectionFactory (javax.jms.ConnectionFactory)1 JMSContext (javax.jms.JMSContext)1 JMSRuntimeException (javax.jms.JMSRuntimeException)1 MessageConsumer (javax.jms.MessageConsumer)1 MessageFormatRuntimeException (javax.jms.MessageFormatRuntimeException)1 MessageProducer (javax.jms.MessageProducer)1 Queue (javax.jms.Queue)1 NamingException (javax.naming.NamingException)1 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)1 Test (org.junit.Test)1