JAXB Conversion Utility

  • Convert a JMS message to a JAXB Object
  • Convert a TextMessage to an Object
  • Convert a TextMessage to an InputStream


package com.verio.adapt.javacore.util;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

* Created by david.king
public final class JaxbConversionUtil {

public static Object convertJMSMessageToJAXBObject(Class typeToConvertTo, Message jmsMessage) {
if (jmsMessage instanceof TextMessage) {
return convertTextMessageToObject(typeToConvertTo, jmsMessage);
return null;

private static Object convertTextMessageToObject(Class typeToConvertTo, Message jmsMessage) {
Object objToReturn = null;

try {
JAXBContext jaxbContext = JAXBContext.newInstance(typeToConvertTo);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
objToReturn = unmarshaller.unmarshal(convertTextMessageToInputStream(jmsMessage));
if (null == objToReturn) {
System.out.printf("[%s], %s", "XML Parsing Error", "The convertTextMessageToObject failed.");

return objToReturn;
} catch (JAXBException ex) {
return null;

private static InputStream convertTextMessageToInputStream(Message jmsMessage) {
TextMessage textMessage = (TextMessage)jmsMessage;
try (InputStream inputStream = new ByteArrayInputStream(textMessage.getText().getBytes("UTF-8"))) {
return inputStream;
} catch (IOException | JMSException ex) {
return null;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s