Java Code Examples for org.springframework.jms.support.converter.MessageConverter

The following examples show how to use org.springframework.jms.support.converter.MessageConverter. 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
/**
 * Build a JMS message to be sent as response based on the given result object.
 * @param session the JMS Session to operate on
 * @param result the content of the message, as returned from the listener method
 * @return the JMS {@code Message} (never {@code null})
 * @throws JMSException if thrown by JMS API methods
 * @see #setMessageConverter
 */
protected Message buildMessage(Session session, Object result) throws JMSException {
	Object content = preProcessResponse(result instanceof JmsResponse
			? ((JmsResponse<?>) result).getResponse() : result);

	MessageConverter converter = getMessageConverter();
	if (converter != null) {
		if (content instanceof org.springframework.messaging.Message) {
			return this.messagingMessageConverter.toMessage(content, session);
		}
		else {
			return converter.toMessage(content, session);
		}
	}

	if (!(content instanceof Message)) {
		throw new MessageConversionException(
				"No MessageConverter specified - cannot handle message [" + content + "]");
	}
	return (Message) content;
}
 
Example 2
/**
 * Build a JMS message to be sent as response based on the given result object.
 * @param session the JMS Session to operate on
 * @param result the content of the message, as returned from the listener method
 * @return the JMS {@code Message} (never {@code null})
 * @throws JMSException if thrown by JMS API methods
 * @see #setMessageConverter
 */
protected Message buildMessage(Session session, Object result) throws JMSException {
	Object content = (result instanceof JmsResponse ? ((JmsResponse<?>) result).getResponse() : result);
	if (content instanceof org.springframework.messaging.Message) {
		return this.messagingMessageConverter.toMessage(content, session);
	}

	MessageConverter converter = getMessageConverter();
	if (converter != null) {
		return converter.toMessage(content, session);
	}

	if (!(content instanceof Message)) {
		throw new MessageConversionException(
				"No MessageConverter specified - cannot handle message [" + content + "]");
	}
	return (Message) content;
}
 
Example 3
/**
 * Extract the message body from the given JMS message.
 * @param message the JMS {@code Message}
 * @return the content of the message, to be passed into the listener method
 * as an argument
 * @throws MessageConversionException if the message could not be extracted
 */
protected Object extractMessage(Message message)  {
	try {
		MessageConverter converter = getMessageConverter();
		if (converter != null) {
			return converter.fromMessage(message);
		}
		return message;
	}
	catch (JMSException ex) {
		throw new MessageConversionException("Could not convert JMS message", ex);
	}
}
 
Example 4
@Override
protected Message createMessageForPayload(Object payload, Session session, @Nullable Object conversionHint)
		throws JMSException {

	MessageConverter converter = getMessageConverter();
	if (converter == null) {
		throw new IllegalStateException("No message converter, cannot handle '" + payload + "'");
	}
	if (converter instanceof SmartMessageConverter) {
		return ((SmartMessageConverter) converter).toMessage(payload, session, conversionHint);

	}
	return converter.toMessage(payload, session);
}
 
Example 5
Source Project: spring-analysis-note   Source File: JmsMessageEndpointManager.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public MessageConverter getMessageConverter() {
	JmsActivationSpecConfig config = getActivationSpecConfig();
	if (config != null) {
		return config.getMessageConverter();
	}
	return null;
}
 
Example 6
/**
 * Extract the message body from the given JMS message.
 * @param message the JMS {@code Message}
 * @return the content of the message, to be passed into the listener method
 * as an argument
 * @throws MessageConversionException if the message could not be extracted
 */
protected Object extractMessage(Message message)  {
	try {
		MessageConverter converter = getMessageConverter();
		if (converter != null) {
			return converter.fromMessage(message);
		}
		return message;
	}
	catch (JMSException ex) {
		throw new MessageConversionException("Could not convert JMS message", ex);
	}
}
 
Example 7
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 8
Source Project: spring-analysis-note   Source File: JmsTemplate.java    License: MIT License 5 votes vote down vote up
private MessageConverter getRequiredMessageConverter() throws IllegalStateException {
	MessageConverter converter = getMessageConverter();
	if (converter == null) {
		throw new IllegalStateException("No 'messageConverter' specified. Check configuration of JmsTemplate.");
	}
	return converter;
}
 
Example 9
@Test
public void replyUsesMessageConverterForPayload() throws JMSException {
	Session session = mock(Session.class);
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(messageConverter.toMessage("Response", session)).willReturn(new StubTextMessage("Response"));

	Message<String> result = MessageBuilder.withPayload("Response").build();
	MessagingMessageListenerAdapter listener = getSimpleInstance("echo", Message.class);
	listener.setMessageConverter(messageConverter);
	javax.jms.Message replyMessage = listener.buildMessage(session, result);

	verify(messageConverter, times(1)).toMessage("Response", session);
	assertNotNull("reply should never be null", replyMessage);
	assertEquals("Response", ((TextMessage) replyMessage).getText());
}
 
Example 10
@Test
public void convertMessageConversionExceptionOnReceive() throws JMSException {
	javax.jms.Message message = createJmsTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
			.given(messageConverter).fromMessage(message);
	messagingTemplate.setJmsMessageConverter(messageConverter);
	given(jmsTemplate.receive("myQueue")).willReturn(message);

	thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
	messagingTemplate.receive("myQueue");
}
 
Example 11
@Test
public void incomingMessageUsesMessageConverter() throws JMSException {
	javax.jms.Message jmsMessage = mock(javax.jms.Message.class);
	Session session = mock(Session.class);
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(messageConverter.fromMessage(jmsMessage)).willReturn("FooBar");
	MessagingMessageListenerAdapter listener = getSimpleInstance("simple", Message.class);
	listener.setMessageConverter(messageConverter);
	listener.onMessage(jmsMessage, session);
	verify(messageConverter, times(1)).fromMessage(jmsMessage);
	assertEquals(1, sample.simples.size());
	assertEquals("FooBar", sample.simples.get(0).getPayload());
}
 
Example 12
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void payloadConverterIsConsistentConstructor() {
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
	JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate(this.jmsTemplate);
	messagingTemplate.afterPropertiesSet();
	assertPayloadConverter(messagingTemplate, messageConverter);
}
 
Example 13
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void payloadConverterIsConsistentSetter() {
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
	JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate();
	messagingTemplate.setJmsTemplate(this.jmsTemplate);
	messagingTemplate.afterPropertiesSet();
	assertPayloadConverter(messagingTemplate, messageConverter);
}
 
Example 14
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
private void assertPayloadConverter(JmsMessagingTemplate messagingTemplate,
		MessageConverter messageConverter) {
	MessageConverter jmsMessageConverter = messagingTemplate.getJmsMessageConverter();
	assertNotNull(jmsMessageConverter);
	assertEquals(MessagingMessageConverter.class, jmsMessageConverter.getClass());
	assertSame(messageConverter, new DirectFieldAccessor(jmsMessageConverter)
			.getPropertyValue("payloadConverter"));
}
 
Example 15
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void convertMessageConversionExceptionOnSend() throws JMSException {
	Message<String> message = createTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
			.given(messageConverter).toMessage(eq(message), any());
	this.messagingTemplate.setJmsMessageConverter(messageConverter);
	invokeMessageCreator();

	assertThatExceptionOfType(org.springframework.messaging.converter.MessageConversionException.class).isThrownBy(() ->
			this.messagingTemplate.send("myQueue", message));
}
 
Example 16
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void convertMessageConversionExceptionOnReceive() throws JMSException {
	javax.jms.Message message = createJmsTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
			.given(messageConverter).fromMessage(message);
	this.messagingTemplate.setJmsMessageConverter(messageConverter);
	given(this.jmsTemplate.receive("myQueue")).willReturn(message);

	assertThatExceptionOfType(org.springframework.messaging.converter.MessageConversionException.class).isThrownBy(() ->
			this.messagingTemplate.receive("myQueue"));
}
 
Example 17
Source Project: spring-analysis-note   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void convertMessageFormatException() throws JMSException {
	Message<String> message = createTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), any());
	this.messagingTemplate.setJmsMessageConverter(messageConverter);
	invokeMessageCreator();

	assertThatExceptionOfType(org.springframework.messaging.converter.MessageConversionException.class).isThrownBy(() ->
			this.messagingTemplate.send("myQueue", message));
}
 
Example 18
@Override
protected Message createMessageForPayload(Object payload, Session session) throws JMSException {
	MessageConverter converter = getMessageConverter();
	if (converter != null) {
		return converter.toMessage(payload, session);
	}
	throw new IllegalStateException("No message converter - cannot handle [" + payload + "]");
}
 
Example 19
/**
 * Extract the message body from the given JMS message.
 * @param message the JMS {@code Message}
 * @return the content of the message, to be passed into the listener method
 * as an argument
 * @throws MessageConversionException if the message could not be extracted
 */
protected Object extractMessage(Message message)  {
	try {
		MessageConverter converter = getMessageConverter();
		if (converter != null) {
			return converter.fromMessage(message);
		}
		return message;
	}
	catch (JMSException ex) {
		throw new MessageConversionException("Could not convert JMS message", ex);
	}
}
 
Example 20
@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), container);
	DirectFieldAccessor accessor = new DirectFieldAccessor(listener);
	assertSame(messageConverter, accessor.getPropertyValue("messageConverter"));
	assertSame(destinationResolver, accessor.getPropertyValue("destinationResolver"));
}
 
Example 21
Source Project: POC   Source File: JMSApplicationConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public MessageConverter jacksonJmsMessageConverter() {
	final MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
	converter.setTargetType(MessageType.TEXT);
	converter.setTypeIdPropertyName("_type");
	return converter;
}
 
Example 22
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;
}
 
Example 23
@Test
public void payloadConversionLazilyInvoked() throws JMSException {
	javax.jms.Message jmsMessage = mock(javax.jms.Message.class);
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(messageConverter.fromMessage(jmsMessage)).willReturn("FooBar");
	MessagingMessageListenerAdapter listener = getSimpleInstance("simple", Message.class);
	listener.setMessageConverter(messageConverter);
	Message<?> message = listener.toMessagingMessage(jmsMessage);
	verify(messageConverter, never()).fromMessage(jmsMessage);
	assertEquals("FooBar", message.getPayload());
	verify(messageConverter, times(1)).fromMessage(jmsMessage);
}
 
Example 24
@Test
public void incomingMessageUsesMessageConverter() throws JMSException {
	javax.jms.Message jmsMessage = mock(javax.jms.Message.class);
	Session session = mock(Session.class);
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(messageConverter.fromMessage(jmsMessage)).willReturn("FooBar");
	MessagingMessageListenerAdapter listener = getSimpleInstance("simple", Message.class);
	listener.setMessageConverter(messageConverter);
	listener.onMessage(jmsMessage, session);
	verify(messageConverter, times(1)).fromMessage(jmsMessage);
	assertEquals(1, sample.simples.size());
	assertEquals("FooBar", sample.simples.get(0).getPayload());
}
 
Example 25
@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 26
Source Project: java-technology-stack   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void payloadConverterIsConsistentSetter() {
	MessageConverter messageConverter = mock(MessageConverter.class);
	given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
	JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate();
	messagingTemplate.setJmsTemplate(this.jmsTemplate);
	messagingTemplate.afterPropertiesSet();
	assertPayloadConverter(messagingTemplate, messageConverter);
}
 
Example 27
@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);
		}
	}
	MessageConverter messageConverter = container.getMessageConverter();
	if (messageConverter != null) {
		messageListener.setMessageConverter(messageConverter);
	}
	DestinationResolver destinationResolver = container.getDestinationResolver();
	if (destinationResolver != null) {
		messageListener.setDestinationResolver(destinationResolver);
	}
	return messageListener;
}
 
Example 28
@Test
public void convertMessageNotWritableException() throws JMSException {
	Message<String> message = createTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), anyObject());
	messagingTemplate.setJmsMessageConverter(messageConverter);
	invokeMessageCreator("myQueue");

	thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
	messagingTemplate.send("myQueue", message);
}
 
Example 29
Source Project: java-technology-stack   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void convertMessageConversionExceptionOnSend() throws JMSException {
	Message<String> message = createTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
			.given(messageConverter).toMessage(eq(message), any());
	this.messagingTemplate.setJmsMessageConverter(messageConverter);
	invokeMessageCreator();

	this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
	this.messagingTemplate.send("myQueue", message);
}
 
Example 30
Source Project: java-technology-stack   Source File: JmsMessagingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void convertMessageConversionExceptionOnReceive() throws JMSException {
	javax.jms.Message message = createJmsTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
			.given(messageConverter).fromMessage(message);
	this.messagingTemplate.setJmsMessageConverter(messageConverter);
	given(this.jmsTemplate.receive("myQueue")).willReturn(message);

	this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
	this.messagingTemplate.receive("myQueue");
}