Using XML configuration - I have MarshallingMessageConverter
working; however, I still want to send some messages as TextMessage
with simple String
values.
It seems that my configuration is forcing me to go from one ditch (No automatic JAXB marshalling) into the other (JAXB marshalling only):
Here's my relevant XML configuration:
<bean id="jaxbConverter" class="org.springframework.jms.support.converter.MarshallingMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
<bean id="jmsListenerContainerFactory"
class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
<property name="errorHandler" ref="jmsErrorHandler" />
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destinationResolver" ref="jmsDestinationResolver"/>
<property name="messageConverter" ref="jaxbConverter" />
</bean>
The JAXB marshalling works fine, but I sometimes want to send an empty body (header properties only) message; which causes an error like so:
.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
Which makes sense, because I'm sending a non-JAXB string (or nothing) in the body.
Is it possible to have the best of both worlds; the String
, byte[]
, Map
conversion behavior of org.springframework.jms.support.converter.SimpleMessageConverter
-and- org.springframework.jms.support.converter.MarshallingMessageConverter
?
Is the only way to accomplish this by making a second container factory with the other converter and explicitly using it in my @JmsListener
annotation?
Create a simple DelegatingMessageConverter
(implement MessageConverter
) and have it delegate to the SimpleMessageConverter
, MarshallingMessageConverter
(or even a MappingJackson2MessageConverter
) based on a message property, e.g. message.getStringProperty("contentType")
- text/plain
, application/xml
, application/json
.