Search in sources :

Example 6 with MessageConverter

use of org.springframework.jms.support.converter.MessageConverter in project spring-framework by spring-projects.

the class MessagingMessageListenerAdapterTests method incomingMessageUsesMessageConverter.

@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());
}
Also used : MappingJackson2MessageConverter(org.springframework.jms.support.converter.MappingJackson2MessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 7 with MessageConverter

use of org.springframework.jms.support.converter.MessageConverter in project spring-framework by spring-projects.

the class JmsMessagingTemplateTests method convertMessageConversionExceptionOnReceive.

@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");
}
Also used : SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) GenericMessageConverter(org.springframework.messaging.converter.GenericMessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter) Test(org.junit.Test)

Example 8 with MessageConverter

use of org.springframework.jms.support.converter.MessageConverter in project spring-framework by spring-projects.

the class JmsMessagingTemplateTests method convertMessageFormatException.

@Test
public void convertMessageFormatException() throws JMSException {
    Message<String> message = createTextMessage();
    MessageConverter messageConverter = mock(MessageConverter.class);
    willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), any());
    messagingTemplate.setJmsMessageConverter(messageConverter);
    invokeMessageCreator("myQueue");
    thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
    messagingTemplate.send("myQueue", message);
}
Also used : SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) GenericMessageConverter(org.springframework.messaging.converter.GenericMessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter) Test(org.junit.Test)

Example 9 with MessageConverter

use of org.springframework.jms.support.converter.MessageConverter in project spring-framework by spring-projects.

the class MethodJmsListenerEndpoint method createMessageListener.

@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;
}
Also used : MessagingMessageListenerAdapter(org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter) DestinationResolver(org.springframework.jms.support.destination.DestinationResolver) InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) MessageConverter(org.springframework.jms.support.converter.MessageConverter)

Example 10 with MessageConverter

use of org.springframework.jms.support.converter.MessageConverter in project spring-framework by spring-projects.

the class AbstractAdaptableMessageListener method buildMessage.

/**
	 * 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;
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) SmartMessageConverter(org.springframework.jms.support.converter.SmartMessageConverter) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessagingMessageConverter(org.springframework.jms.support.converter.MessagingMessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter)

Aggregations

MessageConverter (org.springframework.jms.support.converter.MessageConverter)10 Test (org.junit.Test)8 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)5 GenericMessageConverter (org.springframework.messaging.converter.GenericMessageConverter)4 MappingJackson2MessageConverter (org.springframework.jms.support.converter.MappingJackson2MessageConverter)3 Session (javax.jms.Session)2 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)2 DestinationResolver (org.springframework.jms.support.destination.DestinationResolver)2 BytesMessage (javax.jms.BytesMessage)1 Message (javax.jms.Message)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 StubTextMessage (org.springframework.jms.StubTextMessage)1 MessageConversionException (org.springframework.jms.support.converter.MessageConversionException)1 MessagingMessageConverter (org.springframework.jms.support.converter.MessagingMessageConverter)1 SmartMessageConverter (org.springframework.jms.support.converter.SmartMessageConverter)1 InvocableHandlerMethod (org.springframework.messaging.handler.invocation.InvocableHandlerMethod)1