Top Description Fields Constructors Methods
org.springframework.jms.support.converter

public Class MarshallingMessageConverter

extends Object
implements MessageConverter, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.jms.support.converter.MessageConverter
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .StringReader, .StringWriter, javax.xml.transform.Result, .Source, javax.xml.transform.stream.StreamResult, .StreamSource, jakarta.jms.BytesMessage, .JMSException, .Message, .Session, .TextMessage, org.springframework.beans.factory.InitializingBean, org.springframework.lang.Nullable, org.springframework.oxm.Marshaller, .Unmarshaller, .XmlMappingException, org.springframework.util.Assert

Spring JMS MessageConverter that uses a Marshaller and Unmarshaller. Marshals an object to a BytesMessage, or to a TextMessage if the targetType is set to MessageType#TEXT. Unmarshals from a TextMessage or BytesMessage to an object.
Authors
Arjen Poutsma, Juergen Hoeller
Since
3.0

Field Summary

Modifier and TypeField and Description
private Marshaller
private MessageType
private Unmarshaller

Constructor Summary

AccessConstructor and Description
public
MarshallingMessageConverter()

Construct a new MarshallingMessageConverter with no Marshaller or Unmarshaller set.

public
MarshallingMessageConverter(Marshaller
object used as marshaller and unmarshaller
marshaller
)

Construct a new MarshallingMessageConverter with the given Marshaller set.

public
MarshallingMessageConverter(Marshaller
the Marshaller to use
marshaller
,
Unmarshaller
the Unmarshaller to use
unmarshaller
)

Construct a new MarshallingMessageConverter with the given Marshaller and Unmarshaller.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

public Object
fromMessage(Message
the message to convert
message
)

Implements org.springframework.jms.support.converter.MessageConverter.fromMessage.

This implementation unmarshals the given Message into an object.

protected BytesMessage

Returns:

the resulting message
marshalToBytesMessage
(Object
the object to be marshalled
object
,
Session
current JMS session
session
,
Marshaller
the marshaller to use
marshaller
)

Marshal the given object to a BytesMessage.

protected Message

Returns:

the resulting message
marshalToMessage
(Object
the object to marshal
object
,
Session
the JMS session
session
,
Marshaller
the marshaller to use
marshaller
,
MessageType
the target message type (other than TEXT or BYTES)
targetType
)

Template method that allows for custom message marshalling.

protected TextMessage

Returns:

the resulting message
marshalToTextMessage
(Object
the object to be marshalled
object
,
Session
current JMS session
session
,
Marshaller
the marshaller to use
marshaller
)

Marshal the given object to a TextMessage.

public void
setMarshaller(Marshaller marshaller)

Set the Marshaller to be used by this message converter.

public void
setTargetType(MessageType targetType)

Specify whether toMessage(Object, Session) should marshal to a BytesMessage or a TextMessage.

public void
setUnmarshaller(Unmarshaller unmarshaller)

Set the Unmarshaller to be used by this message converter.

public Message
toMessage(Object
the object to convert
object
,
Session
the Session to use for creating a JMS Message
session
)

Implements org.springframework.jms.support.converter.MessageConverter.toMessage.

This implementation marshals the given object to a jakarta.jms.TextMessage or jakarta.jms.BytesMessage.

protected Object

Returns:

the unmarshalled object
unmarshalFromBytesMessage
(BytesMessage
the message
message
,
Unmarshaller
the unmarshaller to use
unmarshaller
)

Unmarshal the given BytesMessage into an object.

protected Object

Returns:

the unmarshalled object
unmarshalFromMessage
(Message
the message
message
,
Unmarshaller
the unmarshaller to use
unmarshaller
)

Template method that allows for custom message unmarshalling.

protected Object

Returns:

the unmarshalled object
unmarshalFromTextMessage
(TextMessage
the message
message
,
Unmarshaller
the unmarshaller to use
unmarshaller
)

Unmarshal the given TextMessage into an object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

marshallerback to summary
private Marshaller marshaller
Annotations
@Nullable
targetTypeback to summary
private MessageType targetType
unmarshallerback to summary
private Unmarshaller unmarshaller
Annotations
@Nullable

Constructor Detail

MarshallingMessageConverterback to summary
public MarshallingMessageConverter()

Construct a new MarshallingMessageConverter with no Marshaller or Unmarshaller set. The marshaller must be set after construction by invoking setMarshaller(Marshaller) and setUnmarshaller(Unmarshaller).

MarshallingMessageConverterback to summary
public MarshallingMessageConverter(Marshaller marshaller)

Construct a new MarshallingMessageConverter with the given Marshaller set.

If the given Marshaller also implements the Unmarshaller interface, it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.

Note that all Marshaller implementations in Spring also implement the Unmarshaller interface, so that you can safely use this constructor.

Parameters
marshaller:Marshaller

object used as marshaller and unmarshaller

Exceptions
IllegalArgumentException:
when marshaller does not implement the Unmarshaller interface as well
MarshallingMessageConverterback to summary
public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller)

Construct a new MarshallingMessageConverter with the given Marshaller and Unmarshaller.

Parameters
marshaller:Marshaller

the Marshaller to use

unmarshaller:Unmarshaller

the Unmarshaller to use

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
fromMessageback to summary
public Object fromMessage(Message message) throws JMSException, MessageConversionException

Implements org.springframework.jms.support.converter.MessageConverter.fromMessage.

This implementation unmarshals the given Message into an object.

Parameters
message:Message

Doc from org.springframework.jms.support.converter.MessageConverter.fromMessage.

the message to convert

Returns:Object

Doc from org.springframework.jms.support.converter.MessageConverter.fromMessage.

the converted Java object

Annotations
@Override
Exceptions
JMSException:

Doc from org.springframework.jms.support.converter.MessageConverter.fromMessage.

if thrown by JMS API methods

MessageConversionException:

Doc from org.springframework.jms.support.converter.MessageConverter.fromMessage.

in case of conversion failure

See Also
unmarshalFromTextMessage, unmarshalFromBytesMessage
marshalToBytesMessageback to summary
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException

Marshal the given object to a BytesMessage.

Parameters
object:Object

the object to be marshalled

session:Session

current JMS session

marshaller:Marshaller

the marshaller to use

Returns:BytesMessage

the resulting message

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
XmlMappingException:
in case of OXM mapping errors
See Also
Session#createBytesMessage, Marshaller#marshal(Object, Result)
marshalToMessageback to summary
protected Message marshalToMessage(Object object, Session session, Marshaller marshaller, MessageType targetType) throws JMSException, IOException, XmlMappingException

Template method that allows for custom message marshalling. Invoked when setTargetType is not MessageType#TEXT or MessageType#BYTES.

The default implementation throws an IllegalArgumentException.

Parameters
object:Object

the object to marshal

session:Session

the JMS session

marshaller:Marshaller

the marshaller to use

targetType:MessageType

the target message type (other than TEXT or BYTES)

Returns:Message

the resulting message

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
XmlMappingException:
in case of OXM mapping errors
marshalToTextMessageback to summary
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException

Marshal the given object to a TextMessage.

Parameters
object:Object

the object to be marshalled

session:Session

current JMS session

marshaller:Marshaller

the marshaller to use

Returns:TextMessage

the resulting message

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
XmlMappingException:
in case of OXM mapping errors
See Also
Session#createTextMessage, Marshaller#marshal(Object, Result)
setMarshallerback to summary
public void setMarshaller(Marshaller marshaller)

Set the Marshaller to be used by this message converter.

setTargetTypeback to summary
public void setTargetType(MessageType targetType)

Specify whether toMessage(Object, Session) should marshal to a BytesMessage or a TextMessage.

The default is MessageType#BYTES, i.e. this converter marshals to a BytesMessage. Note that the default version of this converter supports MessageType#BYTES and MessageType#TEXT only.

See Also
MessageType#BYTES, MessageType#TEXT
setUnmarshallerback to summary
public void setUnmarshaller(Unmarshaller unmarshaller)

Set the Unmarshaller to be used by this message converter.

toMessageback to summary
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException

Implements org.springframework.jms.support.converter.MessageConverter.toMessage.

This implementation marshals the given object to a jakarta.jms.TextMessage or jakarta.jms.BytesMessage. The desired message type can be defined by setting the "marshalTo" property.

Parameters
object:Object

Doc from org.springframework.jms.support.converter.MessageConverter.toMessage.

the object to convert

session:Session

Doc from org.springframework.jms.support.converter.MessageConverter.toMessage.

the Session to use for creating a JMS Message

Returns:Message

Doc from org.springframework.jms.support.converter.MessageConverter.toMessage.

the JMS Message

Annotations
@Override
Exceptions
JMSException:

Doc from org.springframework.jms.support.converter.MessageConverter.toMessage.

if thrown by JMS API methods

MessageConversionException:

Doc from org.springframework.jms.support.converter.MessageConverter.toMessage.

in case of conversion failure

See Also
marshalToTextMessage, marshalToBytesMessage
unmarshalFromBytesMessageback to summary
protected Object unmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException

Unmarshal the given BytesMessage into an object.

Parameters
message:BytesMessage

the message

unmarshaller:Unmarshaller

the unmarshaller to use

Returns:Object

the unmarshalled object

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
XmlMappingException:
in case of OXM mapping errors
See Also
Unmarshaller#unmarshal(Source)
unmarshalFromMessageback to summary
protected Object unmarshalFromMessage(Message message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException

Template method that allows for custom message unmarshalling. Invoked when fromMessage(Message) is invoked with a message that is not a TextMessage or BytesMessage.

The default implementation throws an IllegalArgumentException.

Parameters
message:Message

the message

unmarshaller:Unmarshaller

the unmarshaller to use

Returns:Object

the unmarshalled object

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
XmlMappingException:
in case of OXM mapping errors
unmarshalFromTextMessageback to summary
protected Object unmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException

Unmarshal the given TextMessage into an object.

Parameters
message:TextMessage

the message

unmarshaller:Unmarshaller

the unmarshaller to use

Returns:Object

the unmarshalled object

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
XmlMappingException:
in case of OXM mapping errors
See Also
Unmarshaller#unmarshal(Source)