Search in sources :

Example 11 with MessageConsumer

use of javax.jms.MessageConsumer in project pinpoint by naver.

the class ActiveMQClientITBase method testQueuePull.

@Test
public void testQueuePull() throws Exception {
    // Given
    final String testQueueName = "TestPullQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    // create producer
    ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    MessageProducer producer = producerSession.createProducer(testQueue);
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // When
    ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    MessageConsumer consumer = consumerSession.createConsumer(testQueue);
    // Then
    producer.send(expectedTextMessage);
    Message message = consumer.receive(1000L);
    Assert.assertEquals(testMessage, ((TextMessage) message).getText());
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    awaitAndVerifyTraceCount(5, 5000L);
    // trace count : 1
    verifyProducerSendEvent(testQueue);
    // trace count : 4
    verifyConsumerPullEvent(testQueue, consumer, expectedTextMessage);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQSession(org.apache.activemq.ActiveMQSession) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 12 with MessageConsumer

use of javax.jms.MessageConsumer in project pinpoint by naver.

the class MessageConsumerBuilder method build.

public MessageConsumer build() throws Exception {
    MessageConsumer consumer = null;
    if (waitTillStarted) {
        ConsumerEventSource consumerEventSource = new ConsumerEventSource(session.getConnection(), destination);
        final CountDownLatch latch = new CountDownLatch(1);
        consumerEventSource.setConsumerListener(new ConsumerListener() {

            @Override
            public void onConsumerEvent(ConsumerEvent event) {
                latch.countDown();
            }
        });
        try {
            consumerEventSource.start();
            consumer = this.session.createConsumer(this.destination);
            if (!latch.await(5L, TimeUnit.SECONDS)) {
                throw new TimeoutException("Timed out waiting for MessageConsumer start event.");
            }
        } finally {
            consumerEventSource.stop();
        }
    } else {
        consumer = this.session.createConsumer(this.destination);
    }
    if (this.messageListener != null) {
        consumer.setMessageListener(this.messageListener);
    }
    return consumer;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ConsumerEventSource(org.apache.activemq.advisory.ConsumerEventSource) ConsumerEvent(org.apache.activemq.advisory.ConsumerEvent) ConsumerListener(org.apache.activemq.advisory.ConsumerListener) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with MessageConsumer

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

the class JmsInvokerClientInterceptor method doExecuteRequest.

/**
	 * Actually execute the given request, sending the invoker request message
	 * to the specified target queue and waiting for a corresponding response.
	 * <p>The default implementation is based on standard JMS send/receive,
	 * using a {@link javax.jms.TemporaryQueue} for receiving the response.
	 * @param session the JMS Session to use
	 * @param queue the resolved target Queue to send to
	 * @param requestMessage the JMS Message to send
	 * @return the RemoteInvocationResult object
	 * @throws JMSException in case of JMS failure
	 */
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
    TemporaryQueue responseQueue = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        responseQueue = session.createTemporaryQueue();
        producer = session.createProducer(queue);
        consumer = session.createConsumer(responseQueue);
        requestMessage.setJMSReplyTo(responseQueue);
        producer.send(requestMessage);
        long timeout = getReceiveTimeout();
        return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeMessageProducer(producer);
        if (responseQueue != null) {
            responseQueue.delete();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer)

Example 14 with MessageConsumer

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

the class SimpleMessageListenerContainer method doShutdown.

/**
	 * Destroy the registered JMS Sessions and associated MessageConsumers.
	 */
@Override
protected void doShutdown() throws JMSException {
    synchronized (this.consumersMonitor) {
        if (this.consumers != null) {
            logger.debug("Closing JMS MessageConsumers");
            for (MessageConsumer consumer : this.consumers) {
                JmsUtils.closeMessageConsumer(consumer);
            }
            logger.debug("Closing JMS Sessions");
            for (Session session : this.sessions) {
                JmsUtils.closeSession(session);
            }
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Session(javax.jms.Session)

Example 15 with MessageConsumer

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

the class SimpleMessageListenerContainerTests method testContextRefreshedEventStartsTheConnectionByDefault.

@Test
public void testContextRefreshedEventStartsTheConnectionByDefault() 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();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
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)82 Session (javax.jms.Session)53 Message (javax.jms.Message)40 MessageProducer (javax.jms.MessageProducer)40 Test (org.junit.Test)39 Connection (javax.jms.Connection)38 TextMessage (javax.jms.TextMessage)38 ConnectionFactory (javax.jms.ConnectionFactory)14 JMSException (javax.jms.JMSException)14 Destination (javax.jms.Destination)12 Queue (javax.jms.Queue)8 TemporaryQueue (javax.jms.TemporaryQueue)8 ObjectMessage (javax.jms.ObjectMessage)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Serializable (java.io.Serializable)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 MessageListener (javax.jms.MessageListener)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 ArrayList (java.util.ArrayList)3