Search in sources :

Example 51 with MessageConsumer

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);
            }
        }
    });
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) BytesMessage(javax.jms.BytesMessage) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) MessageProducer(javax.jms.MessageProducer) BytesMessageInputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Setup(org.apache.axis2.transport.testkit.tests.Setup)

Example 52 with MessageConsumer

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));
}
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 53 with MessageConsumer

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();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage) Message(javax.jms.Message) ContentType(javax.mail.internet.ContentType) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage)

Example 54 with MessageConsumer

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);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) JMSException(javax.jms.JMSException) Message(javax.jms.Message)

Example 55 with MessageConsumer

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;
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3