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

The following examples show how to use org.springframework.jms.support.converter.MessageConverter. 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: AbstractAdaptableMessageListener.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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
Source File: AbstractAdaptableMessageListener.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: MethodJmsListenerEndpointTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #4
Source File: AbstractAdaptableMessageListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 File: MethodJmsListenerEndpoint.java    From spring-analysis-note with 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 #6
Source File: ProductUpdListener.java    From Cloud-Native-Applications-in-Java with MIT License 5 votes vote down vote up
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
	MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
	converter.setTargetType(MessageType.BYTES);
	converter.setTypeIdPropertyName("_type");
	return converter;
}
 
Example #7
Source File: JmsMessageBrokerConfiguration.java    From piper with Apache License 2.0 5 votes vote down vote up
@Bean 
public MessageConverter jacksonJmsMessageConverter(ObjectMapper aObjectMapper) {
  MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
  converter.setObjectMapper(aObjectMapper);
  converter.setTargetType(MessageType.TEXT);
  converter.setTypeIdPropertyName("_type");
  return converter;
}
 
Example #8
Source File: AbstractAdaptableMessageListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: MessagingMessageListenerAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #11
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #12
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #13
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #14
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #15
Source File: AbstractAdaptableMessageListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #16
Source File: Application.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
    final MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    return converter;
}
 
Example #17
Source File: AbstractAdaptableMessageListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 #18
Source File: JmsMessagingTemplateTests.java    From spring-analysis-note with 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 #19
Source File: JmsMessagingTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void convertMessageNotWritableException() throws JMSException {
	Message<String> message = createTextMessage();
	MessageConverter messageConverter = mock(MessageConverter.class);
	willThrow(MessageNotWriteableException.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 #20
Source File: JMSConfig.java    From ElementVueSpringbootCodeTemplate with Apache License 2.0 5 votes vote down vote up
@Bean
public MessageConverter jacksonJmsMessageConverter() {
	MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();

	converter.setTargetType(MessageType.TEXT);
	converter.setTypeIdPropertyName("_type");

	return converter;
}
 
Example #21
Source File: JMSApplicationConfig.java    From POC with 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 File: JmsMessagingTemplateTests.java    From java-technology-stack with 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");
}
 
Example #23
Source File: JmsMessagingTemplateTests.java    From java-technology-stack with 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 #24
Source File: JmsMessagingTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #25
Source File: MethodJmsListenerEndpoint.java    From spring4-understanding with Apache License 2.0 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);
		}
	}
	MessageConverter messageConverter = container.getMessageConverter();
	if (messageConverter != null) {
		messageListener.setMessageConverter(messageConverter);
	}
	DestinationResolver destinationResolver = container.getDestinationResolver();
	if (destinationResolver != null) {
		messageListener.setDestinationResolver(destinationResolver);
	}
	return messageListener;
}
 
Example #26
Source File: MethodJmsListenerEndpoint.java    From java-technology-stack with 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 #27
Source File: JmsMessagingTemplateTests.java    From java-technology-stack with 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 #28
Source File: MessagingMessageListenerAdapterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #29
Source File: MessagingMessageListenerAdapterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #30
Source File: MethodJmsListenerEndpointTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"));
}