Search in sources :

Example 61 with MessageConsumer

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

the class Messages method receiveMessage.

public String receiveMessage() throws JMSException {
    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        // Create a Session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create a MessageConsumer from the Session to the Topic or Queue
        consumer = session.createConsumer(chatQueue);
        // Wait for a message
        TextMessage message = (TextMessage) consumer.receive(1000);
        return message.getText();
    } finally {
        if (consumer != null) {
            consumer.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 62 with MessageConsumer

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

the class ChatBeanTest method test.

public void test() throws Exception {
    EJBContainer.createEJBContainer().getContext().bind("inject", this);
    final Connection connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final MessageProducer questions = session.createProducer(questionQueue);
    final MessageConsumer answers = session.createConsumer(answerQueue);
    sendText("Hello World!", questions, session);
    assertEquals("Hello, Test Case!", receiveText(answers));
    sendText("How are you?", questions, session);
    assertEquals("I'm doing well.", receiveText(answers));
    sendText("Still spinning?", questions, session);
    assertEquals("Once every day, as usual.", receiveText(answers));
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 63 with MessageConsumer

use of javax.jms.MessageConsumer in project gocd by gocd.

the class ActiveMqMessagingService method addListener.

public JMSMessageListenerAdapter addListener(String topic, final GoMessageListener listener) {
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(session.createTopic(topic));
        return JMSMessageListenerAdapter.startListening(consumer, listener, daemonThreadStatsCollector);
    } catch (Exception e) {
        throw bomb(e);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 64 with MessageConsumer

use of javax.jms.MessageConsumer in project gocd by gocd.

the class JMSMessageListenerAdapterTest method shouldNotKillTheThreadWhenThereIsAnException.

@Test
public void shouldNotKillTheThreadWhenThereIsAnException() throws Exception {
    MessageConsumer consumer = mock(MessageConsumer.class);
    when(consumer.receive()).thenThrow(new RuntimeException("should swallow me"));
    GoMessageListener mockListener = new GoMessageListener() {

        public void onMessage(GoMessage message) {
            throw new UnsupportedOperationException("not implemented yet");
        }

        @Override
        public String toString() {
            return "test-listener";
        }
    };
    JMSMessageListenerAdapter listenerAdapter = JMSMessageListenerAdapter.startListening(consumer, mockListener, mock(DaemonThreadStatsCollector.class));
    try {
        listenerAdapter.runImpl();
    } catch (Exception e) {
        e.printStackTrace();
        fail("expected no exception get: " + e);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) DaemonThreadStatsCollector(com.thoughtworks.go.server.service.support.DaemonThreadStatsCollector) GoMessageListener(com.thoughtworks.go.server.messaging.GoMessageListener) GoMessage(com.thoughtworks.go.server.messaging.GoMessage) Test(org.junit.Test)

Example 65 with MessageConsumer

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

the class JAXRSJmsTest method checkBookInResponse.

private void checkBookInResponse(Session session, Destination replyToDestination, long bookId, String bookName) throws Exception {
    MessageConsumer consumer = session.createConsumer(replyToDestination);
    BytesMessage jmsMessage = (BytesMessage) consumer.receive(5000);
    if (jmsMessage == null) {
        throw new RuntimeException("No response recieved on " + replyToDestination);
    }
    byte[] bytes = new byte[(int) jmsMessage.getBodyLength()];
    jmsMessage.readBytes(bytes);
    InputStream is = new ByteArrayInputStream(bytes);
    Book b = readBook(is);
    assertEquals(bookId, b.getId());
    assertEquals(bookName, b.getName());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Book(org.apache.cxf.systest.jaxrs.Book) BytesMessage(javax.jms.BytesMessage)

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