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));
}
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();
}
}
}
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;
}
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);
}
Aggregations