Search in sources :

Example 56 with MessageConsumer

use of javax.jms.MessageConsumer in project cxf by apache.

the class TestReceiver method drainQueue.

private void drainQueue() {
    try (ResourceCloser closer = new ResourceCloser()) {
        Connection connection = closer.register(connectionFactory.createConnection());
        connection.start();
        Session session = closer.register(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
        MessageConsumer consumer = closer.register(session.createConsumer(session.createQueue(receiveQueueName)));
        javax.jms.Message message = null;
        do {
            message = consumer.receive(100);
        } while (message != null);
    } catch (JMSException e) {
        throw JMSUtil.convertJmsException(e);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 57 with MessageConsumer

use of javax.jms.MessageConsumer in project cxf by apache.

the class PooledConnectionTempQueueTest method receiveAndRespondWithMessageIdAsCorrelationId.

public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
    Connection con = connectionFactory.createConnection();
    Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
    final javax.jms.Message inMessage = consumer.receive();
    // String requestMessageId = inMessage.getJMSMessageID();
    // System.out.println("Received message " + requestMessageId);
    final TextMessage replyMessage = session.createTextMessage("Result");
    replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
    final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
    // System.out.println("Sending reply to " + inMessage.getJMSReplyTo());
    producer.send(replyMessage);
    producer.close();
    consumer.close();
    session.close();
    con.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 58 with MessageConsumer

use of javax.jms.MessageConsumer in project tomee by apache.

the class JMSContextImpl method createDurableConsumer.

@Override
public JMSConsumer createDurableConsumer(final Topic topic, final String name) {
    try {
        // JMS 2 only: final JMSConsumerImpl consumer = new JMSConsumerImpl(this, session().createDurableConsumer(topic, name));
        final MessageConsumer delegate = session().createDurableSubscriber(topic, name);
        checkAutoStart();
        return new JMSConsumerImpl(this, delegate);
    } catch (final JMSException e) {
        throw toRuntimeException(e);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) JMSException(javax.jms.JMSException)

Example 59 with MessageConsumer

use of javax.jms.MessageConsumer in project tomee by apache.

the class JmsTest method createSender.

@SuppressWarnings("unchecked")
private synchronized void createSender(final Connection connection, final Destination requestQueue) throws JMSException {
    Session session = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        // create request
        final Map<String, Object> request = new TreeMap<String, Object>();
        request.put("args", new Object[] { "cheese" });
        // create a new temp response queue
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Destination responseQueue = session.createTemporaryQueue();
        // Create a request messages
        final ObjectMessage requestMessage = session.createObjectMessage();
        requestMessage.setJMSReplyTo(responseQueue);
        requestMessage.setObject((Serializable) request);
        // Send the request message
        producer = session.createProducer(requestQueue);
        producer.send(requestMessage);
        // wait for the response message
        consumer = session.createConsumer(responseQueue);
        final Message message = consumer.receive(30000);
        // verify message
        assertNotNull("Did not get a response message", message);
        assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
        final ObjectMessage responseMessage = (ObjectMessage) message;
        final Serializable object = responseMessage.getObject();
        assertNotNull("Response ObjectMessage contains a null object");
        assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
        final Map<String, String> response = (Map<String, String>) object;
        // process results
        final String returnValue = response.get("return");
        assertEquals("test-cheese", returnValue);
    } finally {
        MdbUtil.close(consumer);
        MdbUtil.close(producer);
        MdbUtil.close(session);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TreeMap(java.util.TreeMap) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) Map(java.util.Map) TreeMap(java.util.TreeMap) Session(javax.jms.Session)

Example 60 with MessageConsumer

use of javax.jms.MessageConsumer in project tomee by apache.

the class JmsProxyTest method testProxy.

public void testProxy() throws Exception {
    // create reciever object
    final JmsProxyTest.TestObject testObject = new JmsProxyTest.TestObject("foo");
    final MdbInvoker mdbInvoker = new MdbInvoker(connectionFactory, testObject);
    // Create a Session
    final Connection connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    // Create the request Queue
    final Destination requestQueue = session.createQueue(REQUEST_QUEUE_NAME);
    final MessageConsumer consumer = session.createConsumer(requestQueue);
    consumer.setMessageListener(mdbInvoker);
    // create in invoker
    final JmsProxyTest.TestInterface testInterface = MdbProxy.newProxyInstance(JmsProxyTest.TestInterface.class, connectionFactory, REQUEST_QUEUE_NAME);
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
    assertEquals("foobar", testInterface.echo("bar"));
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) Session(javax.jms.Session)

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