Class MarshallingMessageConverter
- java.lang.Object
- org.springframework.jms.support.converter.MarshallingMessageConverter
- All Implemented Interfaces:
InitializingBean,MessageConverter
public class MarshallingMessageConverter extends Object implements MessageConverter, InitializingBean
Spring JMSMessageConverterthat uses aMarshallerandUnmarshaller. Marshals an object to aBytesMessage, or to aTextMessageif thetargetTypeis set toMessageType.TEXT. Unmarshals from aTextMessageorBytesMessageto an object.- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller
Constructor Summary
Constructors Constructor Description MarshallingMessageConverter()MarshallingMessageConverter(Marshaller marshaller)Construct a newMarshallingMessageConverterwith the givenMarshallerset.MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller)Construct a newMarshallingMessageConverterwith the given Marshaller and Unmarshaller.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()Invoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.ObjectfromMessage(Message message)This implementation unmarshals the givenMessageinto an object.protected BytesMessagemarshalToBytesMessage(Object object, Session session, Marshaller marshaller)Marshal the given object to aBytesMessage.protected MessagemarshalToMessage(Object object, Session session, Marshaller marshaller, MessageType targetType)Template method that allows for custom message marshalling.protected TextMessagemarshalToTextMessage(Object object, Session session, Marshaller marshaller)Marshal the given object to aTextMessage.voidsetMarshaller(Marshaller marshaller)Set theMarshallerto be used by this message converter.voidsetTargetType(MessageType targetType)voidsetUnmarshaller(Unmarshaller unmarshaller)Set theUnmarshallerto be used by this message converter.MessagetoMessage(Object object, Session session)This implementation marshals the given object to aTextMessageorBytesMessage.protected ObjectunmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller)Unmarshal the givenBytesMessageinto an object.protected ObjectunmarshalFromMessage(Message message, Unmarshaller unmarshaller)Template method that allows for custom message unmarshalling.protected ObjectunmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller)Unmarshal the givenTextMessageinto an object.
Constructor Detail
MarshallingMessageConverter
public MarshallingMessageConverter()
Construct a newMarshallingMessageConverterwith noMarshallerorUnmarshallerset. The marshaller must be set after construction by invokingsetMarshaller(Marshaller)andsetUnmarshaller(Unmarshaller).
MarshallingMessageConverter
public MarshallingMessageConverter(Marshaller marshaller)
Construct a newMarshallingMessageConverterwith the givenMarshallerset.If the given
Marshalleralso implements theUnmarshallerinterface, it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.Note that all
Marshallerimplementations in Spring also implement theUnmarshallerinterface, so that you can safely use this constructor.- Parameters:
marshaller- object used as marshaller and unmarshaller- Throws:
IllegalArgumentException- whenmarshallerdoes not implement theUnmarshallerinterface as well
MarshallingMessageConverter
public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller)
Construct a newMarshallingMessageConverterwith the given Marshaller and Unmarshaller.- Parameters:
marshaller- the Marshaller to useunmarshaller- the Unmarshaller to use
Method Detail
setMarshaller
public void setMarshaller(Marshaller marshaller)
Set theMarshallerto be used by this message converter.
setUnmarshaller
public void setUnmarshaller(Unmarshaller unmarshaller)
Set theUnmarshallerto be used by this message converter.
setTargetType
public void setTargetType(MessageType targetType)
Specify whethertoMessage(Object, Session)should marshal to aBytesMessageor aTextMessage.The default is
MessageType.BYTES, i.e. this converter marshals to aBytesMessage. Note that the default version of this converter supportsMessageType.BYTESandMessageType.TEXTonly.- See Also:
MessageType.BYTES,MessageType.TEXT
afterPropertiesSet
public void afterPropertiesSet()
Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSetin interfaceInitializingBean
toMessage
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException
This implementation marshals the given object to aTextMessageorBytesMessage. The desired message type can be defined by setting the"marshalTo"property.- Specified by:
toMessagein interfaceMessageConverter- Parameters:
object- the object to convertsession- the Session to use for creating a JMS Message- Returns:
- the JMS Message
- Throws:
JMSException- if thrown by JMS API methodsMessageConversionException- in case of conversion failure- See Also:
marshalToTextMessage(java.lang.Object, javax.jms.Session, org.springframework.oxm.Marshaller),marshalToBytesMessage(java.lang.Object, javax.jms.Session, org.springframework.oxm.Marshaller)
fromMessage
public Object fromMessage(Message message) throws JMSException, MessageConversionException
This implementation unmarshals the givenMessageinto an object.- Specified by:
fromMessagein interfaceMessageConverter- Parameters:
message- the message to convert- Returns:
- the converted Java object
- Throws:
JMSException- if thrown by JMS API methodsMessageConversionException- in case of conversion failure- See Also:
unmarshalFromTextMessage(javax.jms.TextMessage, org.springframework.oxm.Unmarshaller),unmarshalFromBytesMessage(javax.jms.BytesMessage, org.springframework.oxm.Unmarshaller)
marshalToTextMessage
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException
Marshal the given object to aTextMessage.- Parameters:
object- the object to be marshalledsession- current JMS sessionmarshaller- the marshaller to use- Returns:
- the resulting message
- Throws:
JMSException- if thrown by JMS methodsIOException- in case of I/O errorsXmlMappingException- in case of OXM mapping errors- See Also:
Session.createTextMessage(),Marshaller.marshal(Object, Result)
marshalToBytesMessage
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException
Marshal the given object to aBytesMessage.- Parameters:
object- the object to be marshalledsession- current JMS sessionmarshaller- the marshaller to use- Returns:
- the resulting message
- Throws:
JMSException- if thrown by JMS methodsIOException- in case of I/O errorsXmlMappingException- in case of OXM mapping errors- See Also:
Session.createBytesMessage(),Marshaller.marshal(Object, Result)
marshalToMessage
protected Message marshalToMessage(Object object, Session session, Marshaller marshaller, MessageType targetType) throws JMSException, IOException, XmlMappingException
Template method that allows for custom message marshalling. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)is notMessageType.TEXTorMessageType.BYTES.The default implementation throws an
IllegalArgumentException.- Parameters:
object- the object to marshalsession- the JMS sessionmarshaller- the marshaller to usetargetType- the target message type (other than TEXT or BYTES)- Returns:
- the resulting message
- Throws:
JMSException- if thrown by JMS methodsIOException- in case of I/O errorsXmlMappingException- in case of OXM mapping errors
unmarshalFromTextMessage
protected Object unmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException
Unmarshal the givenTextMessageinto an object.- Parameters:
message- the messageunmarshaller- the unmarshaller to use- Returns:
- the unmarshalled object
- Throws:
JMSException- if thrown by JMS methodsIOException- in case of I/O errorsXmlMappingException- in case of OXM mapping errors- See Also:
Unmarshaller.unmarshal(Source)
unmarshalFromBytesMessage
protected Object unmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException
Unmarshal the givenBytesMessageinto an object.- Parameters:
message- the messageunmarshaller- the unmarshaller to use- Returns:
- the unmarshalled object
- Throws:
JMSException- if thrown by JMS methodsIOException- in case of I/O errorsXmlMappingException- in case of OXM mapping errors- See Also:
Unmarshaller.unmarshal(Source)
unmarshalFromMessage
protected Object unmarshalFromMessage(Message message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException
Template method that allows for custom message unmarshalling. Invoked whenfromMessage(Message)is invoked with a message that is not aTextMessageorBytesMessage.The default implementation throws an
IllegalArgumentException.- Parameters:
message- the messageunmarshaller- the unmarshaller to use- Returns:
- the unmarshalled object
- Throws:
JMSException- if thrown by JMS methodsIOException- in case of I/O errorsXmlMappingException- in case of OXM mapping errors