Search in sources :

Example 76 with MessageConsumer

use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.

the class OrderIntakeClient method main.

/**
     * Main routing for OrderIntakeClient
     * @param args command-line args
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    HornetQMixIn hqMixIn = new HornetQMixIn(false).setUser(USER).setPassword(PASSWD);
    hqMixIn.initialize();
    try {
        Session session = hqMixIn.getJMSSession();
        MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(ORDER_QUEUE_NAME));
        String orderTxt = readFileContent(args[0]);
        System.out.println("Submitting Order" + "\n" + "----------------------------\n" + orderTxt + "\n----------------------------");
        producer.send(hqMixIn.createJMSMessage(orderTxt));
        MessageConsumer consumer = session.createConsumer(HornetQMixIn.getJMSQueue(ORDERACK_QUEUE_NAME));
        System.out.println("Order submitted ... waiting for reply.");
        TextMessage reply = (TextMessage) consumer.receive(3000);
        if (reply == null) {
            System.out.println("No reply received.");
        } else {
            String str = reply.getText();
            System.out.println("Received reply" + "\n" + "----------------------------\n" + str + "\n----------------------------");
        }
    } finally {
        hqMixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageProducer(javax.jms.MessageProducer) HornetQMixIn(org.switchyard.component.test.mixins.hornetq.HornetQMixIn) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 77 with MessageConsumer

use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.

the class HornetQClient method main.

/**
     * Only execution point for this application.
     * @param ignored not used.
     * @throws Exception if something goes wrong.
     */
public static void main(final String[] args) throws Exception {
    String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
    if (args.length != 0) {
        orders = args;
    }
    HornetQMixIn hqMixIn = new HornetQMixIn(false).setUser(USER).setPassword(PASSWD);
    hqMixIn.initialize();
    try {
        Session session = hqMixIn.createJMSSession();
        MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(ORDER_QUEUE));
        for (String order : orders) {
            final TextMessage message = session.createTextMessage();
            message.setText(order);
            producer.send(message);
        }
        session.close();
        session = hqMixIn.createJMSSession();
        System.out.println("* * *  SHIPPING ORDERS  * * *");
        MessageConsumer consumer = session.createConsumer(HornetQMixIn.getJMSQueue(SHIPPING_QUEUE));
        Message msg = null;
        while ((msg = consumer.receive(1000)) != null) {
            System.out.println(" - " + hqMixIn.readStringFromJMSMessage(msg));
        }
        System.out.println();
        System.out.println("* * *  PENDING ORDERS (FILLING STOCK)  * * *");
        consumer = session.createConsumer(HornetQMixIn.getJMSQueue(FILLING_STOCK_QUEUE));
        while ((msg = consumer.receive(1000)) != null) {
            System.out.println(" - " + hqMixIn.readStringFromJMSMessage(msg));
        }
        session.close();
        Thread.sleep(2000);
    } finally {
        hqMixIn.uninitialize();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) HornetQMixIn(org.switchyard.component.test.mixins.hornetq.HornetQMixIn) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 78 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class JmsTemplateTests method doTestReceive.

private void doTestReceive(boolean explicitDestination, boolean useDefaultDestination, boolean testConverter, boolean clientAcknowledge, boolean messageSelector, boolean noLocal, long timeout) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    if (noLocal) {
        template.setPubSubNoLocal(true);
    }
    template.setReceiveTimeout(timeout);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    String selectorString = "selector";
    given(session.createConsumer(queue, messageSelector ? selectorString : null)).willReturn(messageConsumer);
    if (!useTransactedTemplate() && !useTransactedSession()) {
        given(session.getAcknowledgeMode()).willReturn(clientAcknowledge ? Session.CLIENT_ACKNOWLEDGE : Session.AUTO_ACKNOWLEDGE);
    }
    TextMessage textMessage = mock(TextMessage.class);
    if (testConverter) {
        given(textMessage.getText()).willReturn("Hello World!");
    }
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(textMessage);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(textMessage);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(textMessage);
    }
    Message message = null;
    String textFromMessage = null;
    if (useDefaultDestination) {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(selectorString) : template.receiveAndConvert());
        } else {
            message = (messageSelector ? template.receiveSelected(selectorString) : template.receive());
        }
    } else if (explicitDestination) {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(queue, selectorString) : template.receiveAndConvert(queue));
        } else {
            message = (messageSelector ? template.receiveSelected(queue, selectorString) : template.receive(queue));
        }
    } else {
        if (testConverter) {
            textFromMessage = (String) (messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) : template.receiveAndConvert(destinationName));
        } else {
            message = (messageSelector ? template.receiveSelected(destinationName, selectorString) : template.receive(destinationName));
        }
    }
    if (testConverter) {
        assertEquals("Message text should be equal", "Hello World!", textFromMessage);
    } else {
        assertEquals("Messages should refer to the same object", message, textMessage);
    }
    verify(connection).start();
    verify(connection).close();
    if (useTransactedTemplate()) {
        verify(session).commit();
    }
    verify(session).close();
    if (!useTransactedSession() && clientAcknowledge) {
        verify(textMessage).acknowledge();
    }
    verify(messageConsumer).close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) TextMessage(javax.jms.TextMessage)

Example 79 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class SimpleMessageListenerContainerTests method testDestroyClosesConsumersSessionsAndConnectionInThatOrder.

@Test
public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() throws Exception {
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new TestMessageListener());
    this.container.afterPropertiesSet();
    this.container.start();
    this.container.destroy();
    verify(messageConsumer).close();
    verify(session).close();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
    verify(connection).close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

Example 80 with MessageConsumer

use of javax.jms.MessageConsumer in project spring-framework by spring-projects.

the class SimpleMessageListenerContainerTests method testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse.

@Test
public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception {
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new TestMessageListener());
    this.container.setAutoStartup(false);
    this.container.afterPropertiesSet();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    verify(connection).setExceptionListener(this.container);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ConnectionFactory(javax.jms.ConnectionFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

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