use of javax.jms.MessageConsumer in project wso2-axis2-transports by wso2.
the class MockEchoEndpoint method setUp.
@Setup
@SuppressWarnings("unused")
private void setUp(JMSTestEnvironment env, JMSRequestResponseChannel channel) throws Exception {
Destination destination = channel.getDestination();
Destination replyDestination = channel.getReplyDestination();
connection = env.getConnectionFactory().createConnection();
connection.setExceptionListener(this);
connection.start();
replyConnection = env.getConnectionFactory().createConnection();
replyConnection.setExceptionListener(this);
final Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = replySession.createProducer(replyDestination);
MessageConsumer consumer = connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
log.info("Message received: ID = " + message.getJMSMessageID());
Message reply;
if (message instanceof BytesMessage) {
reply = replySession.createBytesMessage();
IOUtils.copy(new BytesMessageInputStream((BytesMessage) message), new BytesMessageOutputStream((BytesMessage) reply));
} else if (message instanceof TextMessage) {
reply = replySession.createTextMessage();
((TextMessage) reply).setText(((TextMessage) message).getText());
} else {
// TODO
throw new UnsupportedOperationException("Unsupported message type");
}
reply.setJMSCorrelationID(message.getJMSMessageID());
reply.setStringProperty(BaseConstants.CONTENT_TYPE, message.getStringProperty(BaseConstants.CONTENT_TYPE));
producer.send(reply);
log.info("Message sent: ID = " + reply.getJMSMessageID());
} catch (Throwable ex) {
fireEndpointError(ex);
}
}
});
}
use of javax.jms.MessageConsumer 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.MessageConsumer in project wso2-axis2-transports by wso2.
the class JMSRequestResponseClient method sendMessage.
public IncomingMessage<T> sendMessage(ClientOptions options, ContentType contentType, T message) throws Exception {
String correlationId = doSendMessage(options, contentType, message);
MessageConsumer consumer = replySession.createConsumer(replyDestination, "JMSCorrelationID = '" + correlationId + "'");
try {
Message replyMessage = consumer.receive(8000);
return new IncomingMessage<T>(new ContentType(replyMessage.getStringProperty("Content-Type")), jmsMessageFactory.parseMessage(replyMessage));
} finally {
consumer.close();
}
}
use of javax.jms.MessageConsumer in project cxf by apache.
the class JMSUtil method receive.
public static Message receive(Session session, Destination replyToDestination, String correlationId, long receiveTimeout, boolean pubSubNoLocal) {
try (ResourceCloser closer = new ResourceCloser()) {
String messageSelector = correlationId == null ? null : "JMSCorrelationID = '" + correlationId + "'";
MessageConsumer consumer = closer.register(session.createConsumer(replyToDestination, messageSelector, pubSubNoLocal));
javax.jms.Message replyMessage = consumer.receive(receiveTimeout);
if (replyMessage == null) {
throw new RuntimeException("Timeout receiving message with correlationId " + correlationId);
}
return replyMessage;
} catch (JMSException e) {
throw convertJmsException(e);
}
}
use of javax.jms.MessageConsumer in project cxf by apache.
the class PollingMessageListenerContainer method createConsumer.
private MessageConsumer createConsumer(final Connection connection, final Session session) throws JMSException {
final MessageConsumer consumer;
if (jmsConfig != null && jmsConfig.isOneSessionPerConnection()) {
Destination destination;
if (!isReply()) {
destination = jmsConfig.getTargetDestination(session);
} else {
destination = jmsConfig.getReplyDestination(session);
}
consumer = createConsumer(destination, session);
connection.start();
} else {
consumer = createConsumer(session);
}
return consumer;
}
Aggregations