Java Code Examples for javax.jms.MessageListener#onMessage()

The following examples show how to use javax.jms.MessageListener#onMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DummyMessageQuery.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(ActiveMQDestination destination, MessageListener listener) throws Exception {
   LOG.info("Initial query is creating: " + MESSAGE_COUNT + " messages");
   for (int i = 0; i < MESSAGE_COUNT; i++) {
      ActiveMQTextMessage message = new ActiveMQTextMessage();
      message.setText("Initial message: " + i + " loaded from query");
      listener.onMessage(message);
   }
}
 
Example 2
Source File: JmsNamespaceHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testListeners() throws Exception {
	TestBean testBean1 = context.getBean("testBean1", TestBean.class);
	TestBean testBean2 = context.getBean("testBean2", TestBean.class);
	TestMessageListener testBean3 = context.getBean("testBean3", TestMessageListener.class);

	assertNull(testBean1.getName());
	assertNull(testBean2.getName());
	assertNull(testBean3.message);

	TextMessage message1 = mock(TextMessage.class);
	given(message1.getText()).willReturn("Test1");

	MessageListener listener1 = getListener("listener1");
	listener1.onMessage(message1);
	assertEquals("Test1", testBean1.getName());

	TextMessage message2 = mock(TextMessage.class);
	given(message2.getText()).willReturn("Test2");

	MessageListener listener2 = getListener("listener2");
	listener2.onMessage(message2);
	assertEquals("Test2", testBean2.getName());

	TextMessage message3 = mock(TextMessage.class);

	MessageListener listener3 = getListener(DefaultMessageListenerContainer.class.getName() + "#0");
	listener3.onMessage(message3);
	assertSame(message3, testBean3.message);
}
 
Example 3
Source File: JmsNamespaceHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testListeners() throws Exception {
	TestBean testBean1 = context.getBean("testBean1", TestBean.class);
	TestBean testBean2 = context.getBean("testBean2", TestBean.class);
	TestMessageListener testBean3 = context.getBean("testBean3", TestMessageListener.class);

	assertNull(testBean1.getName());
	assertNull(testBean2.getName());
	assertNull(testBean3.message);

	TextMessage message1 = mock(TextMessage.class);
	given(message1.getText()).willReturn("Test1");

	MessageListener listener1 = getListener("listener1");
	listener1.onMessage(message1);
	assertEquals("Test1", testBean1.getName());

	TextMessage message2 = mock(TextMessage.class);
	given(message2.getText()).willReturn("Test2");

	MessageListener listener2 = getListener("listener2");
	listener2.onMessage(message2);
	assertEquals("Test2", testBean2.getName());

	TextMessage message3 = mock(TextMessage.class);

	MessageListener listener3 = getListener(DefaultMessageListenerContainer.class.getName() + "#0");
	listener3.onMessage(message3);
	assertSame(message3, testBean3.message);
}
 
Example 4
Source File: AmqpClientActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private void testConsumeMessageAndExpectForwardToConciergeForwarder(final Connection connection,
        final int consumers,
        final Consumer<Command> commandConsumer,
        @Nullable final Consumer<ActorRef> postStep) throws JMSException {

    new TestKit(actorSystem) {{
        final Props props =
                AmqpClientActor.propsForTests(connection, getRef(), getRef(),
                        (ac, el) -> mockConnection);
        final ActorRef amqpClientActor = actorSystem.actorOf(props);

        amqpClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
        expectMsg(CONNECTED_SUCCESS);

        final ArgumentCaptor<MessageListener> captor = ArgumentCaptor.forClass(MessageListener.class);
        verify(mockConsumer, timeout(1000).atLeast(consumers)).setMessageListener(captor.capture());
        for (final MessageListener messageListener : captor.getAllValues()) {
            messageListener.onMessage(mockMessage());
        }

        for (int i = 0; i < consumers; i++) {
            final Command command = expectMsgClass(Command.class);
            assertThat((CharSequence) command.getEntityId()).isEqualTo(TestConstants.Things.THING_ID);
            assertThat(command.getDittoHeaders().getCorrelationId()).contains(TestConstants.CORRELATION_ID);
            commandConsumer.accept(command);
        }

        if (postStep != null) {
            postStep.accept(amqpClientActor);
        }
    }};
}
 
Example 5
Source File: AmqpClientActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private void testConsumeMessageAndExpectForwardToConciergeForwarderAndReceiveResponse(final Connection connection,
        final BiFunction<ThingId, DittoHeaders, CommandResponse> responseSupplier,
        final String expectedAddressPrefix,
        final Predicate<String> messageTextPredicate) throws JMSException {

    new TestKit(actorSystem) {{
        final Props props =
                AmqpClientActor.propsForTests(connection, getRef(), getRef(),
                        (ac, el) -> mockConnection);
        final ActorRef amqpClientActor = actorSystem.actorOf(props);

        amqpClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
        expectMsg(CONNECTED_SUCCESS);

        final ArgumentCaptor<MessageListener> captor = ArgumentCaptor.forClass(MessageListener.class);
        verify(mockConsumer, timeout(1000).atLeastOnce()).setMessageListener(captor.capture());
        final MessageListener messageListener = captor.getValue();
        messageListener.onMessage(mockMessage());

        final ThingCommand command = expectMsgClass(ThingCommand.class);
        assertThat((CharSequence) command.getEntityId()).isEqualTo(TestConstants.Things.THING_ID);
        assertThat(command.getDittoHeaders().getCorrelationId()).contains(TestConstants.CORRELATION_ID);
        assertThat(command).isInstanceOf(ModifyThing.class);

        getLastSender().tell(responseSupplier.apply(command.getEntityId(), command.getDittoHeaders()), getRef());

        final ArgumentCaptor<JmsMessage> messageCaptor = ArgumentCaptor.forClass(JmsMessage.class);
        // verify that the message is published via the producer with the correct destination
        final MessageProducer messageProducer =
                getProducerForAddress(expectedAddressPrefix + command.getEntityId());
        verify(messageProducer, timeout(2000)).send(messageCaptor.capture(), any(CompletionListener.class));

        final Message message = messageCaptor.getValue();
        assertThat(message).isNotNull();
        assertThat(messageTextPredicate).accepts(message.getBody(String.class));
    }};
}
 
Example 6
Source File: JmsNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testListeners() throws Exception {
	TestBean testBean1 = context.getBean("testBean1", TestBean.class);
	TestBean testBean2 = context.getBean("testBean2", TestBean.class);
	TestMessageListener testBean3 = context.getBean("testBean3", TestMessageListener.class);

	assertNull(testBean1.getName());
	assertNull(testBean2.getName());
	assertNull(testBean3.message);

	TextMessage message1 = mock(TextMessage.class);
	given(message1.getText()).willReturn("Test1");

	MessageListener listener1 = getListener("listener1");
	listener1.onMessage(message1);
	assertEquals("Test1", testBean1.getName());

	TextMessage message2 = mock(TextMessage.class);
	given(message2.getText()).willReturn("Test2");

	MessageListener listener2 = getListener("listener2");
	listener2.onMessage(message2);
	assertEquals("Test2", testBean2.getName());

	TextMessage message3 = mock(TextMessage.class);

	MessageListener listener3 = getListener(DefaultMessageListenerContainer.class.getName() + "#0");
	listener3.onMessage(message3);
	assertSame(message3, testBean3.message);
}
 
Example 7
Source File: AmqpClientActorTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testConsumerRecreationFailureWhenConnected() throws JMSException {
    new TestKit(actorSystem) {{
        final Props props =
                AmqpClientActor.propsForTests(singleConsumerConnection(), getRef(), getRef(),
                        (ac, el) -> mockConnection);
        final TestActorRef<AmqpClientActor> amqpClientActorRef = TestActorRef.apply(props, actorSystem);
        final AmqpClientActor amqpClientActor = amqpClientActorRef.underlyingActor();

        amqpClientActorRef.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
        expectMsg(CONNECTED_SUCCESS);

        // GIVEN: JMS session fails, but the JMS connection can create a new functional session
        final Session mockSession2 = Mockito.mock(Session.class);
        final MessageConsumer mockConsumer2 = Mockito.mock(JmsMessageConsumer.class);
        when(mockSession.createConsumer(any()))
                .thenThrow(new IllegalStateException("expected exception"));
        doReturn(mockSession2).when(mockConnection).createSession(anyInt());
        doReturn(mockConsumer2).when(mockSession2).createConsumer(any());

        // WHEN: consumer is closed and cannot be recreated
        final ActorRef amqpConsumerActor = amqpClientActor.context().children().toStream()
                .find(child -> child.path().name().startsWith(AmqpConsumerActor.ACTOR_NAME_PREFIX))
                .get();
        final Throwable error = new IllegalStateException("Forcibly detached");
        final Status.Failure failure = new Status.Failure(new AskTimeoutException("Consumer creation timeout"));
        amqpClientActor.connectionListener.onConsumerClosed(mockConsumer, error);
        verify(mockSession, atLeastOnce()).createConsumer(any());
        amqpConsumerActor.tell(failure, amqpConsumerActor);

        // THEN: connection gets restarted
        verify(mockConnection).createSession(anyInt());
        final ArgumentCaptor<MessageListener> captor = ArgumentCaptor.forClass(MessageListener.class);
        verify(mockConsumer2, timeout(1000).atLeastOnce()).setMessageListener(captor.capture());
        final MessageListener messageListener = captor.getValue();

        // THEN: recreated connection is working
        messageListener.onMessage(mockMessage());
        expectMsgClass(Command.class);
    }};
}
 
Example 8
Source File: AbstractMessageListenerContainer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Invoke the specified listener as standard JMS MessageListener.
 * <p>Default implementation performs a plain invocation of the
 * {@code onMessage} method.
 * @param listener the JMS MessageListener to invoke
 * @param message the received JMS Message
 * @throws JMSException if thrown by JMS API methods
 * @see javax.jms.MessageListener#onMessage
 */
protected void doInvokeListener(MessageListener listener, Message message) throws JMSException {
	listener.onMessage(message);
}
 
Example 9
Source File: AbstractMessageListenerContainer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Invoke the specified listener as standard JMS MessageListener.
 * <p>Default implementation performs a plain invocation of the
 * {@code onMessage} method.
 * @param listener the JMS MessageListener to invoke
 * @param message the received JMS Message
 * @throws JMSException if thrown by JMS API methods
 * @see javax.jms.MessageListener#onMessage
 */
protected void doInvokeListener(MessageListener listener, Message message) throws JMSException {
	listener.onMessage(message);
}
 
Example 10
Source File: AbstractMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Invoke the specified listener as standard JMS MessageListener.
 * <p>Default implementation performs a plain invocation of the
 * {@code onMessage} method.
 * @param listener the JMS MessageListener to invoke
 * @param message the received JMS Message
 * @throws JMSException if thrown by JMS API methods
 * @see javax.jms.MessageListener#onMessage
 */
protected void doInvokeListener(MessageListener listener, Message message) throws JMSException {
	listener.onMessage(message);
}