Java Code Examples for org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter

The following examples show how to use org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter. These examples are extracted from open source projects. 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
@Test
public void processAndReply() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDefaultResponseDestination(replyDestination);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);
	assertDefaultListenerMethodInvocation();

	verify(reply).setJMSCorrelationID(correlationId);
	verify(queueSender).send(reply);
	verify(queueSender).close();
}
 
Example 2
@Test
public void processAndReply() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDefaultResponseDestination(replyDestination);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);
	assertDefaultListenerMethodInvocation();

	verify(reply).setJMSCorrelationID(correlationId);
	verify(queueSender).send(reply);
	verify(queueSender).close();
}
 
Example 3
Source Project: spring-analysis-note   Source File: MethodJmsListenerEndpoint.java    License: MIT License 5 votes vote down vote up
@Override
protected MessagingMessageListenerAdapter createMessageListener(MessageListenerContainer container) {
	Assert.state(this.messageHandlerMethodFactory != null,
			"Could not create message listener - MessageHandlerMethodFactory not set");
	MessagingMessageListenerAdapter messageListener = createMessageListenerInstance();
	Object bean = getBean();
	Method method = getMethod();
	Assert.state(bean != null && method != null, "No bean+method set on endpoint");
	InvocableHandlerMethod invocableHandlerMethod =
			this.messageHandlerMethodFactory.createInvocableHandlerMethod(bean, method);
	messageListener.setHandlerMethod(invocableHandlerMethod);
	String responseDestination = getDefaultResponseDestination();
	if (StringUtils.hasText(responseDestination)) {
		if (container.isReplyPubSubDomain()) {
			messageListener.setDefaultResponseTopicName(responseDestination);
		}
		else {
			messageListener.setDefaultResponseQueueName(responseDestination);
		}
	}
	QosSettings responseQosSettings = container.getReplyQosSettings();
	if (responseQosSettings != null) {
		messageListener.setResponseQosSettings(responseQosSettings);
	}
	MessageConverter messageConverter = container.getMessageConverter();
	if (messageConverter != null) {
		messageListener.setMessageConverter(messageConverter);
	}
	DestinationResolver destinationResolver = container.getDestinationResolver();
	if (destinationResolver != null) {
		messageListener.setDestinationResolver(destinationResolver);
	}
	return messageListener;
}
 
Example 4
/**
 * Test for {@link ValidationBean} with a validator ({@link TestValidator}) specified
 * in a custom {@link org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory}.
 *
 * The test should throw a {@link org.springframework.jms.listener.adapter.ListenerExecutionFailedException}
 */
public void testJmsHandlerMethodFactoryConfiguration(ApplicationContext context) throws JMSException {
	JmsListenerContainerTestFactory simpleFactory =
			context.getBean("defaultFactory", JmsListenerContainerTestFactory.class);
	assertEquals(1, simpleFactory.getListenerContainers().size());
	MethodJmsListenerEndpoint endpoint = (MethodJmsListenerEndpoint)
			simpleFactory.getListenerContainers().get(0).getEndpoint();

	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	endpoint.setupListenerContainer(container);
	MessagingMessageListenerAdapter listener = (MessagingMessageListenerAdapter) container.getMessageListener();
	listener.onMessage(new StubTextMessage("failValidation"), mock(Session.class));
}
 
Example 5
@Test
public void setExtraCollaborators() {
	MessageConverter messageConverter = mock(MessageConverter.class);
	DestinationResolver destinationResolver = mock(DestinationResolver.class);
	this.container.setMessageConverter(messageConverter);
	this.container.setDestinationResolver(destinationResolver);

	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod("resolveObjectPayload", MyBean.class), this.container);
	DirectFieldAccessor accessor = new DirectFieldAccessor(listener);
	assertSame(messageConverter, accessor.getPropertyValue("messageConverter"));
	assertSame(destinationResolver, accessor.getPropertyValue("destinationResolver"));
}
 
Example 6
@Test
public void processAndReplyWithSendToTopic() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setPubSubDomain(true);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, true);
	assertListenerMethodInvocation(sample, methodName);
}
 
Example 7
@Test
public void resolveGenericMessage() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(Message.class);

	Session session = mock(Session.class);
	listener.onMessage(createSimpleJmsTextMessage("test"), session);
	assertDefaultListenerMethodInvocation();
}
 
Example 8
@Test
public void resolveHeaderAndPayload() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, int.class);

	Session session = mock(Session.class);
	StubTextMessage message = createSimpleJmsTextMessage("my payload");
	message.setIntProperty("myCounter", 55);
	listener.onMessage(message, session);
	assertDefaultListenerMethodInvocation();
}
 
Example 9
@Test
public void resolveCustomHeaderNameAndPayload() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, int.class);

	Session session = mock(Session.class);
	StubTextMessage message = createSimpleJmsTextMessage("my payload");
	message.setIntProperty("myCounter", 24);
	listener.onMessage(message, session);
	assertDefaultListenerMethodInvocation();
}
 
Example 10
@Test
public void resolveCustomHeaderNameAndPayloadWithHeaderNameSet() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, int.class);

	Session session = mock(Session.class);
	StubTextMessage message = createSimpleJmsTextMessage("my payload");
	message.setIntProperty("myCounter", 24);
	listener.onMessage(message, session);
	assertDefaultListenerMethodInvocation();
}
 
Example 11
@Test
public void resolveHeaders() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, Map.class);

	Session session = mock(Session.class);
	StubTextMessage message = createSimpleJmsTextMessage("my payload");
	message.setIntProperty("customInt", 1234);
	message.setJMSMessageID("abcd-1234");
	listener.onMessage(message, session);
	assertDefaultListenerMethodInvocation();
}
 
Example 12
@Test
public void validatePayloadValid() throws JMSException {
	String methodName = "validatePayload";

	DefaultMessageHandlerMethodFactory customFactory = new DefaultMessageHandlerMethodFactory();
	customFactory.setValidator(testValidator("invalid value"));
	initializeFactory(customFactory);

	Method method = getListenerMethod(methodName, String.class);
	MessagingMessageListenerAdapter listener = createInstance(customFactory, method);
	Session session = mock(Session.class);
	listener.onMessage(createSimpleJmsTextMessage("test"), session); // test is a valid value
	assertListenerMethodInvocation(sample, methodName);
}
 
Example 13
@Test
public void processFromTopicAndReplyWithSendToQueue() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setPubSubDomain(true);
	container.setReplyPubSubDomain(false);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, false);
	assertListenerMethodInvocation(sample, methodName);
}
 
Example 14
@Test
public void resolveObjectPayload() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(MyBean.class);
	MyBean myBean = new MyBean();
	myBean.name = "myBean name";

	Session session = mock(Session.class);
	ObjectMessage message = mock(ObjectMessage.class);
	given(message.getObject()).willReturn(myBean);

	listener.onMessage(message, session);
	assertDefaultListenerMethodInvocation();
}
 
Example 15
@Test
public void resolveConvertedPayload() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(Integer.class);

	Session session = mock(Session.class);

	listener.onMessage(createSimpleJmsTextMessage("33"), session);
	assertDefaultListenerMethodInvocation();
}
 
Example 16
@Test
public void emptySendTo() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);

	TextMessage reply = mock(TextMessage.class);
	Session session = mock(Session.class);
	given(session.createTextMessage("content")).willReturn(reply);

	thrown.expect(ReplyFailureException.class);
	thrown.expectCause(Matchers.isA(InvalidDestinationException.class));
	listener.onMessage(createSimpleJmsTextMessage("content"), session);
}
 
Example 17
@Test
public void processAndReplyWithSendToQueue() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, "replyDestination", false);
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 18
@Test
public void processFromTopicAndReplyWithSendToQueue() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setPubSubDomain(true);
	container.setReplyPubSubDomain(false);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, "replyDestination", false);
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 19
@Test
public void processAndReplyWithSendToTopic() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setPubSubDomain(true);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, "replyDestination", true);
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 20
@Test
public void processFromQueueAndReplyWithSendToTopic() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setReplyPubSubDomain(true);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, "replyDestination", true);
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 21
@Test
public void processAndReplyWithCustomReplyQosSettings() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	QosSettings replyQosSettings = new QosSettings(1, 6, 6000);
	container.setReplyQosSettings(replyQosSettings);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, "replyDestination", false, replyQosSettings);
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 22
@Test
public void processAndReplyWithNullReplyQosSettings() throws JMSException {
	String methodName = "processAndReplyWithSendTo";
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setReplyQosSettings(null);
	MessagingMessageListenerAdapter listener = createInstance(this.factory,
			getListenerMethod(methodName, String.class), container);
	processAndReplyWithSendTo(listener, "replyDestination", false);
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 23
private void processAndReplyWithSendTo(MessagingMessageListenerAdapter listener,
		String replyDestinationName, boolean pubSubDomain,
		QosSettings replyQosSettings) throws JMSException {
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	DestinationResolver destinationResolver = mock(DestinationResolver.class);
	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);

	given(destinationResolver.resolveDestinationName(session, replyDestinationName, pubSubDomain))
			.willReturn(replyDestination);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDestinationResolver(destinationResolver);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);

	verify(destinationResolver).resolveDestinationName(session, replyDestinationName, pubSubDomain);
	verify(reply).setJMSCorrelationID(correlationId);
	if (replyQosSettings != null) {
		verify(queueSender).send(reply, replyQosSettings.getDeliveryMode(),
				replyQosSettings.getPriority(), replyQosSettings.getTimeToLive());
	}
	else {
		verify(queueSender).send(reply);
	}
	verify(queueSender).close();
}
 
Example 24
@Test
public void emptySendTo() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);

	TextMessage reply = mock(TextMessage.class);
	Session session = mock(Session.class);
	given(session.createTextMessage("content")).willReturn(reply);

	assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() ->
			listener.onMessage(createSimpleJmsTextMessage("content"), session))
		.withCauseInstanceOf(InvalidDestinationException.class);
}
 
Example 25
@Test
public void validatePayloadValid() throws JMSException {
	String methodName = "validatePayload";

	DefaultMessageHandlerMethodFactory customFactory = new DefaultMessageHandlerMethodFactory();
	customFactory.setValidator(testValidator("invalid value"));
	initializeFactory(customFactory);

	Method method = getListenerMethod(methodName, String.class);
	MessagingMessageListenerAdapter listener = createInstance(customFactory, method);
	Session session = mock(Session.class);
	listener.onMessage(createSimpleJmsTextMessage("test"), session); // test is a valid value
	assertListenerMethodInvocation(this.sample, methodName);
}
 
Example 26
@Test
public void validatePayloadInvalid() throws JMSException {
	DefaultMessageHandlerMethodFactory customFactory = new DefaultMessageHandlerMethodFactory();
	customFactory.setValidator(testValidator("invalid value"));

	Method method = getListenerMethod("validatePayload", String.class);
	MessagingMessageListenerAdapter listener = createInstance(customFactory, method);
	Session session = mock(Session.class);

	// test is an invalid value
	assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() ->
			listener.onMessage(createSimpleJmsTextMessage("invalid value"), session));

}
 
Example 27
@Test
public void invalidPayloadType() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(Integer.class);
	Session session = mock(Session.class);

	// test is not a valid integer
	assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() ->
			listener.onMessage(createSimpleJmsTextMessage("test"), session))
		.withCauseInstanceOf(MessageConversionException.class)
		.withMessageContaining(getDefaultListenerMethod(Integer.class).toGenericString()); // ref to method
}
 
Example 28
private MessagingMessageListenerAdapter createInstance(
		DefaultMessageHandlerMethodFactory factory, Method method, MessageListenerContainer container) {

	MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint();
	endpoint.setBean(sample);
	endpoint.setMethod(method);
	endpoint.setMessageHandlerMethodFactory(factory);
	return endpoint.createMessageListener(container);
}
 
Example 29
private MessagingMessageListenerAdapter createInstance(
		DefaultMessageHandlerMethodFactory factory, Method method, MessageListenerContainer container) {

	MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint();
	endpoint.setBean(this.sample);
	endpoint.setMethod(method);
	endpoint.setMessageHandlerMethodFactory(factory);
	return endpoint.createMessageListener(container);
}
 
Example 30
Source Project: java-technology-stack   Source File: MethodJmsListenerEndpoint.java    License: MIT License 5 votes vote down vote up
@Override
protected MessagingMessageListenerAdapter createMessageListener(MessageListenerContainer container) {
	Assert.state(this.messageHandlerMethodFactory != null,
			"Could not create message listener - MessageHandlerMethodFactory not set");
	MessagingMessageListenerAdapter messageListener = createMessageListenerInstance();
	InvocableHandlerMethod invocableHandlerMethod =
			this.messageHandlerMethodFactory.createInvocableHandlerMethod(getBean(), getMethod());
	messageListener.setHandlerMethod(invocableHandlerMethod);
	String responseDestination = getDefaultResponseDestination();
	if (StringUtils.hasText(responseDestination)) {
		if (container.isReplyPubSubDomain()) {
			messageListener.setDefaultResponseTopicName(responseDestination);
		}
		else {
			messageListener.setDefaultResponseQueueName(responseDestination);
		}
	}
	QosSettings responseQosSettings = container.getReplyQosSettings();
	if (responseQosSettings != null) {
		messageListener.setResponseQosSettings(responseQosSettings);
	}
	MessageConverter messageConverter = container.getMessageConverter();
	if (messageConverter != null) {
		messageListener.setMessageConverter(messageConverter);
	}
	DestinationResolver destinationResolver = container.getDestinationResolver();
	if (destinationResolver != null) {
		messageListener.setDestinationResolver(destinationResolver);
	}
	return messageListener;
}