mirror of https://github.com/apache/activemq.git
Added an adaptive message transformation. More details here: https://issues.apache.org/activemq/browse/AMQ-1208
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@548562 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
34a8028770
commit
d2fe512fa4
|
@ -44,38 +44,69 @@ public class XStreamMessageTransformer extends MessageTransformerSupport {
|
|||
private XStream xStream;
|
||||
|
||||
/**
|
||||
* Defines the direction in which transformation goes.
|
||||
* If 'false' (default),
|
||||
* Defines the type of transformation.
|
||||
* If XML (default),
|
||||
* - producer transformation transforms from Object to XML.
|
||||
* - consumer transformation transforms from XML to Object.
|
||||
* If 'true' transformation directions changes,
|
||||
* If OBJECT,
|
||||
* - producer transformation transforms from XML to Object.
|
||||
* - consumer transformation transforms from Object to XML.
|
||||
* If ADAPTIVE,
|
||||
* - producer transformation transforms from Object to XML, or XML to Object
|
||||
* depending on the type of the original message
|
||||
* - consumer transformation transforms from XML to Object, or Object to XML
|
||||
* depending on the type of the original message
|
||||
*/
|
||||
private boolean reverse;
|
||||
public enum MessageTransform {XML, OBJECT, ADAPTIVE};
|
||||
|
||||
public XStreamMessageTransformer() {
|
||||
new XStreamMessageTransformer(false);
|
||||
protected MessageTransform transformType;
|
||||
|
||||
public XStreamMessageTransformer() {
|
||||
this(MessageTransform.XML);
|
||||
}
|
||||
|
||||
public XStreamMessageTransformer(boolean direction) {
|
||||
this.reverse = direction;
|
||||
public XStreamMessageTransformer(MessageTransform transformType) {
|
||||
this.transformType = transformType;
|
||||
}
|
||||
|
||||
public Message consumerTransform(Session session, MessageConsumer consumer, Message message) throws JMSException {
|
||||
if (reverse) {
|
||||
return objectToText(session, message);
|
||||
} else {
|
||||
return textToObject(session, message);
|
||||
}
|
||||
}
|
||||
switch (transformType) {
|
||||
case XML:
|
||||
return (message instanceof TextMessage) ?
|
||||
textToObject(session, (TextMessage)message) :
|
||||
message;
|
||||
case OBJECT:
|
||||
return (message instanceof ObjectMessage) ?
|
||||
objectToText(session, (ObjectMessage)message) :
|
||||
message;
|
||||
case ADAPTIVE:
|
||||
return (message instanceof TextMessage) ?
|
||||
textToObject(session, (TextMessage)message) :
|
||||
(message instanceof ObjectMessage) ?
|
||||
objectToText(session, (ObjectMessage)message) :
|
||||
message;
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
public Message producerTransform(Session session, MessageProducer producer, Message message) throws JMSException {
|
||||
if (reverse) {
|
||||
return textToObject(session, message);
|
||||
} else {
|
||||
return objectToText(session, message);
|
||||
}
|
||||
switch (transformType) {
|
||||
case XML:
|
||||
return (message instanceof ObjectMessage) ?
|
||||
objectToText(session, (ObjectMessage)message) :
|
||||
message;
|
||||
case OBJECT:
|
||||
return (message instanceof TextMessage) ?
|
||||
textToObject(session, (TextMessage)message) :
|
||||
message;
|
||||
case ADAPTIVE:
|
||||
return (message instanceof TextMessage) ?
|
||||
textToObject(session, (TextMessage)message) :
|
||||
(message instanceof ObjectMessage) ?
|
||||
objectToText(session, (ObjectMessage)message) :
|
||||
message;
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
// Properties
|
||||
|
@ -97,52 +128,44 @@ public class XStreamMessageTransformer extends MessageTransformerSupport {
|
|||
return new XStream();
|
||||
}
|
||||
|
||||
|
||||
public boolean isReverse() {
|
||||
return reverse;
|
||||
public MessageTransform getTransformType() {
|
||||
return transformType;
|
||||
}
|
||||
|
||||
public void setReverse(boolean reverse) {
|
||||
this.reverse = reverse;
|
||||
public void setTransformType(MessageTransform transformType) {
|
||||
this.transformType = transformType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an incoming XML encoded {@link TextMessage} to an {@link ObjectMessage}
|
||||
* @param session - JMS session currently being used
|
||||
* @param message - if this is a TextMessage, it will be transformed to an ObjectMessage
|
||||
* @return ObjectMessage, if the incoming message is a TextMessage, the original message otherwise
|
||||
* @param textMessage - text message to transform to object message
|
||||
* @return ObjectMessage
|
||||
* @throws JMSException
|
||||
*/
|
||||
protected Message textToObject(Session session, Message message) throws JMSException {
|
||||
if (message instanceof TextMessage) {
|
||||
TextMessage textMessage = (TextMessage) message;
|
||||
Object object = unmarshall(session, textMessage);
|
||||
if (object instanceof Serializable) {
|
||||
ObjectMessage answer = session.createObjectMessage((Serializable) object);
|
||||
copyProperties(message, answer);
|
||||
return answer;
|
||||
}
|
||||
else {
|
||||
throw new JMSException("Object is not serializable: " + object);
|
||||
}
|
||||
protected ObjectMessage textToObject(Session session, TextMessage textMessage) throws JMSException {
|
||||
Object object = unmarshall(session, textMessage);
|
||||
if (object instanceof Serializable) {
|
||||
ObjectMessage answer = session.createObjectMessage((Serializable) object);
|
||||
copyProperties(textMessage, answer);
|
||||
return answer;
|
||||
}
|
||||
else {
|
||||
throw new JMSException("Object is not serializable: " + object);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an incoming {@link ObjectMessage} to an XML encoded {@link TextMessage}
|
||||
* @param session - JMS session currently being used
|
||||
* @param message - if this is an ObjectMessage, it will be transformed to a TextMessage
|
||||
* @return XML encoded TextMessage, if the incoming messsage is an ObjectMessge, the original message otherwise
|
||||
* @param objectMessage - object message to transform to text message
|
||||
* @return XML encoded TextMessage
|
||||
* @throws JMSException
|
||||
*/
|
||||
protected Message objectToText(Session session, Message message) throws JMSException {
|
||||
if (message instanceof ObjectMessage) {
|
||||
TextMessage answer = session.createTextMessage(marshall(session, (ObjectMessage) message));
|
||||
copyProperties(message, answer);
|
||||
return answer;
|
||||
}
|
||||
return message;
|
||||
protected TextMessage objectToText(Session session, ObjectMessage objectMessage) throws JMSException {
|
||||
TextMessage answer = session.createTextMessage(marshall(session, objectMessage));
|
||||
copyProperties(objectMessage, answer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,6 +31,8 @@ import junit.framework.TestCase;
|
|||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.ActiveMQMessageConsumer;
|
||||
|
||||
import static org.apache.activemq.util.xstream.XStreamMessageTransformer.MessageTransform.*;
|
||||
|
||||
/**
|
||||
* @version $Revision$
|
||||
*/
|
||||
|
@ -40,7 +42,7 @@ public class XStreamTransformTest extends TestCase {
|
|||
protected long timeout = 5000;
|
||||
|
||||
public void testSendObjectMessageReceiveAsTextMessageAndObjectMessage() throws Exception {
|
||||
connectionFactory.setTransformer(new XStreamMessageTransformer());
|
||||
connectionFactory.setTransformer(new XStreamMessageTransformer(XML));
|
||||
connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
|
@ -87,8 +89,7 @@ public class XStreamTransformTest extends TestCase {
|
|||
}
|
||||
|
||||
public void testSendTextMessageReceiveAsObjectMessageAndTextMessage() throws Exception {
|
||||
// Set reverse to true
|
||||
connectionFactory.setTransformer(new XStreamMessageTransformer(true));
|
||||
connectionFactory.setTransformer(new XStreamMessageTransformer(OBJECT));
|
||||
connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
|
@ -138,6 +139,89 @@ public class XStreamTransformTest extends TestCase {
|
|||
|
||||
}
|
||||
|
||||
public void testAdaptiveTransform() throws Exception {
|
||||
connectionFactory.setTransformer(new XStreamMessageTransformer(ADAPTIVE));
|
||||
connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
// lets create the consumers
|
||||
Session adaptiveSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
Destination destination = adaptiveSession.createTopic(getClass().getName());
|
||||
MessageConsumer adaptiveConsumer = adaptiveSession.createConsumer(destination);
|
||||
|
||||
Session origSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
MessageConsumer origConsumer = origSession.createConsumer(destination);
|
||||
// lets clear the transformer on this consumer so we see the message as it really is
|
||||
((ActiveMQMessageConsumer) origConsumer).setTransformer(null);
|
||||
|
||||
// Create producer
|
||||
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
MessageProducer producer = producerSession.createProducer(destination);
|
||||
|
||||
Message message;
|
||||
ObjectMessage objectMessage;
|
||||
TextMessage textMessage;
|
||||
SamplePojo body;
|
||||
Object object;
|
||||
String text;
|
||||
|
||||
// Send a text message
|
||||
String xmlText =
|
||||
"<org.apache.activemq.util.xstream.SamplePojo>" +
|
||||
"<name>James</name>" +
|
||||
"<city>London</city>" +
|
||||
"</org.apache.activemq.util.xstream.SamplePojo>";
|
||||
|
||||
TextMessage txtRequest = producerSession.createTextMessage(xmlText);
|
||||
producer.send(txtRequest);
|
||||
|
||||
// lets consume it as a text message
|
||||
message = adaptiveConsumer.receive(timeout);
|
||||
assertNotNull("Should have received a message!", message);
|
||||
assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage);
|
||||
textMessage = (TextMessage) message;
|
||||
text = textMessage.getText();
|
||||
assertTrue("Text should be non-empty!", text != null && text.length() > 0);
|
||||
|
||||
// lets consume it as an object message
|
||||
message = origConsumer.receive(timeout);
|
||||
assertNotNull("Should have received a message!", message);
|
||||
assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage);
|
||||
objectMessage = (ObjectMessage) message;
|
||||
object = objectMessage.getObject();
|
||||
assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
|
||||
body = (SamplePojo) object;
|
||||
assertEquals("name", "James", body.getName());
|
||||
assertEquals("city", "London", body.getCity());
|
||||
|
||||
// Send object message
|
||||
ObjectMessage objRequest = producerSession.createObjectMessage(new SamplePojo("James", "London"));
|
||||
producer.send(objRequest);
|
||||
|
||||
// lets consume it as an object message
|
||||
message = adaptiveConsumer.receive(timeout);
|
||||
assertNotNull("Should have received a message!", message);
|
||||
assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage);
|
||||
objectMessage = (ObjectMessage) message;
|
||||
object = objectMessage.getObject();
|
||||
assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
|
||||
body = (SamplePojo) object;
|
||||
assertEquals("name", "James", body.getName());
|
||||
assertEquals("city", "London", body.getCity());
|
||||
|
||||
|
||||
// lets consume it as a text message
|
||||
message = origConsumer.receive(timeout);
|
||||
assertNotNull("Should have received a message!", message);
|
||||
assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage);
|
||||
textMessage = (TextMessage) message;
|
||||
text = textMessage.getText();
|
||||
assertTrue("Text should be non-empty!", text != null && text.length() > 0);
|
||||
System.out.println("Received XML...");
|
||||
System.out.println(text);
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
|
|
Loading…
Reference in New Issue