[jboss-svn-commits] JBL Code SVN: r13544 - in labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta: tests/src/org/jboss/soa/esb/actions/routing and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Jul 16 05:59:20 EDT 2007
Author: beve
Date: 2007-07-16 05:59:19 -0400 (Mon, 16 Jul 2007)
New Revision: 13544
Modified:
labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/AbstractRouter.java
labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java
labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/JmsRouterIntegrationTest.java
Log:
Added the property continue-processing. Also made the process method in AbstractRouter non-private and have
hidden it in JMSRouter.
Modified: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/AbstractRouter.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/AbstractRouter.java 2007-07-16 09:58:00 UTC (rev 13543)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/AbstractRouter.java 2007-07-16 09:59:19 UTC (rev 13544)
@@ -63,7 +63,7 @@
/* (non-Javadoc)
* @see org.jboss.soa.esb.actions.ActionProcessor#process(java.lang.Object)
*/
- public final Message process(Message message) throws ActionProcessingException {
+ public Message process(Message message) throws ActionProcessingException {
if(unwrap) {
route(ActionUtils.getTaskObject(message));
} else {
Modified: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java 2007-07-16 09:58:00 UTC (rev 13543)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/actions/routing/JMSRouter.java 2007-07-16 09:59:19 UTC (rev 13544)
@@ -43,6 +43,7 @@
import org.jboss.internal.soa.esb.rosetta.pooling.JmsConnectionPoolContainer;
import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.ActionUtils;
import org.jboss.soa.esb.addressing.eprs.JMSEpr;
import org.jboss.soa.esb.common.Configuration;
import org.jboss.soa.esb.helpers.ConfigTree;
@@ -51,21 +52,27 @@
import org.jboss.soa.esb.util.Util;
/**
- * JSM Routing Action Processor.
+ * JMS Routing Action Processor.
* <p/>
* Sample Action Configuration:
* <pre>
* <action class="org.jboss.soa.esb.actions.routing.JMSRouter">
- * jndiName="queue/A"
- * message-prop-<i>>prop-name<</i>="<i>>prop-value<</i>" >!-- (Optional)--<
- * unwrap="true/false" >!-- (Optional - default false)--<
+ * <property name="jndiName" value="queue/A">
+ * <property name="message-prop-<i>>prop-name<</i>="<i>>prop-value<</i>" >!-- (Optional)--<
+ * <property name="unwrap" value="true/false" >!-- (Optional - default false)--<
+ * <property name="continue-processing" value="true/false" >!-- (Optional - default false)--<
* />
* </pre>
* Note how properties to be set on the message are prefixed with "message-prop-".
* <p/>
- * To unwrap the message payload from the message before routing, set the "unwrap" property to "true".
+ * <ul>
+ * <li>'unwrap' true will extract the message payload from the Message object before sending</li>
+ * <li>'unwrap' false false will send the serialized Message object</li>
+ * </ul>
* TODO: Add support for JMS Topic destinations.
+ *
* @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ * @author <a href="mailto:daniel.bevenius at redpill.se">daniel.bevenius at redpill.se</a>
* @since Version 4.0
*/
public class JMSRouter extends AbstractRouter {
@@ -74,6 +81,11 @@
*/
private static Logger logger = Logger.getLogger(JMSRouter.class);
/**
+ * Indicates whether processing of the action pipeline
+ * should continue or not.
+ */
+ public static final String ATTR_CONTINUE_ACTION_PIPELINE_PROCESSING = "continue-processing";
+ /**
* Routing properties.
*/
private List<KeyValuePair> properties;
@@ -81,6 +93,14 @@
* JMS Queue setup.
*/
private JMSSendQueueSetup queueSetup;
+ /**
+ * The JMS Queue name from the configuration
+ */
+ private String queueName;
+ /**
+ * Should action pipeline processing continue.
+ */
+ private boolean continueProcessing;
/**
* Public constructor.
@@ -106,68 +126,116 @@
this.properties = properties;
- String queueName = KeyValuePair.getValue("jndiName", properties);
+ queueName = KeyValuePair.getValue("jndiName", properties);
if(queueName == null) {
throw new ConfigurationException("JMSRouter must specify a 'jndiName' property.");
}
- try {
+ continueProcessing = Boolean.parseBoolean( KeyValuePair.getValue( ATTR_CONTINUE_ACTION_PIPELINE_PROCESSING, properties ) );
+ createQueueSetup( queueName );
+ this.properties = properties;
+ }
+
+ protected void createQueueSetup( String queueName )
+ throws ConfigurationException
+ {
+ try {
queueSetup = new JMSSendQueueSetup(queueName);
} catch (Throwable t) {
- new ConfigurationException("Failed to configure JMS Queue for routing.", t);
+ throw new ConfigurationException("Failed to configure JMS Queue for routing.", t);
}
- this.properties = properties;
- }
+ }
+
+
+ /**
+ * Will simply pass the message to the route method unmodified.
+ * @return <code>null</code>
+ */
+ @Override
+ public org.jboss.soa.esb.message.Message process( org.jboss.soa.esb.message.Message message )
+ throws ActionProcessingException
+ {
+ route ( message );
+
+ if ( continueProcessing )
+ return message;
+ else
+ return null;
+ }
- /* (non-Javadoc)
+ /* (non-Javadoc)
* @see org.jboss.soa.esb.actions.routing.AbstractRouter#route(java.lang.Object)
*/
- public void route(Object message) throws ActionProcessingException {
+ public void route(final Object message) throws ActionProcessingException {
- if(!(message instanceof Serializable) && !(message instanceof org.jboss.soa.esb.message.Message)) {
- throw new ActionProcessingException("Cannot send Object [" + message.getClass().getName() + "] to destination [" + queueSetup.queueName + "]. Object must be serializable.");
+ if(!(message instanceof org.jboss.soa.esb.message.Message)) {
+ throw new ActionProcessingException("Cannot send Object [" + message.getClass().getName() + "] to destination [" + queueName + "]. Object must be an instance of org.jboss.soa.esb.message.Message) .");
}
+ final org.jboss.soa.esb.message.Message esbMessage = (org.jboss.soa.esb.message.Message)message;
+
try {
Message jmsMessage = null;
- // Send the message to the queue...
- if (message instanceof org.jboss.soa.esb.message.Message) {
- org.jboss.soa.esb.message.Message esbMsg = (org.jboss.soa.esb.message.Message)message;
- message = Util.serialize(esbMsg);
- jmsMessage = createObjectMessage(message);
- setJMSProperties ( jmsMessage, esbMsg );
- } else if(message instanceof String) {
- jmsMessage = queueSetup.jmsSession.createTextMessage();
-
- if(logger.isDebugEnabled()) {
- logger.debug("Sending Text message: [" + message + "] to destination [" + queueSetup.queueName + "].");
- }
- ((TextMessage)jmsMessage).setText((String)message);
- } else if(message instanceof byte[]) {
- jmsMessage = queueSetup.jmsSession.createBytesMessage();
-
- if(logger.isDebugEnabled()) {
- logger.debug("Sending byte[] message: [" + message + "] to destination [" + queueSetup.queueName + "].");
- }
- ((BytesMessage)jmsMessage).writeBytes((byte[])message);
- } else {
- jmsMessage = createObjectMessage(message);
- }
+ if ( !unwrap )
+ {
+ jmsMessage = createObjectMessage(Util.serialize(esbMessage));
+ }
+ else
+ {
+ Object objectFromBody = ActionUtils.getTaskObject(esbMessage);
+ jmsMessage = createJMSMessageWithObjectType( objectFromBody );
+ }
+
setStringProperties(jmsMessage);
- queueSetup.jmsProducer.send(jmsMessage);
+ setJMSProperties ( jmsMessage, esbMessage );
+ send( jmsMessage );
+
} catch(Exception e) {
- String errorMessage = "Exception while sending message [" + message + "] to destination [";
+ StringBuilder sb = new StringBuilder();
+ sb.append("Exception while sending message [").append(message).append("] to destination [");
if (queueSetup != null)
- errorMessage += queueSetup.queueName + "].";
+ sb.append(queueSetup.queueName).append("].");
else
- errorMessage += "null ].";
+ sb.append("null ].");
+ String errorMessage = sb.toString();
logger.error(errorMessage, e);
throw new ActionProcessingException(errorMessage, e);
}
}
+ protected Message createJMSMessageWithObjectType( Object objectFromBody ) throws JMSException
+ {
+ Message jmsMessage = null;
+ if(objectFromBody instanceof String) {
+ jmsMessage = queueSetup.jmsSession.createTextMessage();
+
+ if(logger.isDebugEnabled()) {
+ logger.debug("Sending Text message: [" + objectFromBody + "] to destination [" + queueSetup.queueName + "].");
+ }
+
+ ((TextMessage)jmsMessage).setText((String)objectFromBody);
+ } else if(objectFromBody instanceof byte[]) {
+ jmsMessage = queueSetup.jmsSession.createBytesMessage();
+
+ if(logger.isDebugEnabled()) {
+ logger.debug("Sending byte[] message: [" + objectFromBody + "] to destination [" + queueSetup.queueName + "].");
+ }
+
+ ((BytesMessage)jmsMessage).writeBytes((byte[])objectFromBody);
+ } else {
+ jmsMessage = createObjectMessage(objectFromBody);
+ }
+
+ return jmsMessage;
+ }
+
+ protected void send( Message jmsMessage ) throws JMSException
+ {
+ queueSetup.jmsProducer.send(jmsMessage);
+ }
+
/**
* Set JMS Header fields on the outgoing JMS Message.
* </p>
@@ -184,7 +252,7 @@
jmsMessage.setJMSCorrelationID( esbMsg.getHeader().getCall().getMessageID().toString() );
}
- private Message createObjectMessage(Object message) throws JMSException {
+ protected Message createObjectMessage(Object message) throws JMSException {
Message jmsMessage;
jmsMessage = queueSetup.jmsSession.createObjectMessage();
Modified: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/JmsRouterIntegrationTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/JmsRouterIntegrationTest.java 2007-07-16 09:58:00 UTC (rev 13543)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/JmsRouterIntegrationTest.java 2007-07-16 09:59:19 UTC (rev 13544)
@@ -22,26 +22,123 @@
package org.jboss.soa.esb.actions.routing;
-import junit.framework.TestCase;
+import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import java.io.Serializable;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+
+import javax.jms.JMSException;
+import javax.jms.TextMessage;
+import javax.naming.NamingException;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.actions.ActionProcessingException;
import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.KeyValuePair;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.mockejb.jms.ObjectMessageImpl;
+import org.mockejb.jms.TextMessageImpl;
-public class JmsRouterIntegrationTest extends TestCase
+/**
+ *
+ * @author John Doe
+ * @author <a href="daniel.bevenius at redpill.se">Daniel Bevenius</a>
+ *
+ */
+public class JmsRouterIntegrationTest
{
- public JmsRouterIntegrationTest ()
+ @SuppressWarnings ( "unused")
+ private Logger log = Logger.getLogger( JmsRouterIntegrationTest.class );
+
+ private static JMSRouter router;
+ private final String messageID = "1234-junittest";
+ private final String bodyContent = "hello";
+ private Message msg;
+
+ @Before
+ public void setup() throws URISyntaxException
{
+ msg = createESBMessageObject( messageID, bodyContent );
}
+
+ /*
+ * Expected to fail as queue foobar does not exist.
+ */
+ @Test ( expected = ConfigurationException.class )
+ public void constructor() throws ConfigurationException, NamingException, JMSException
+ {
+ ConfigTree tree = createConfigTree();
+ router = new JMSRouter(tree);
+ }
- public void testRouter () throws Exception
+ @Test
+ public void process_unwrap_false() throws ConfigurationException, NamingException, JMSException, ActionProcessingException, URISyntaxException
{
- ConfigTree tree = new ConfigTree("test");
+ final ConfigTree tree = createConfigTree();
+ router = new MockJMSRouter(tree);
+ router.route( msg );
- tree.setAttribute("jndiName", "foobar");
+ assertProcessContract( messageID, msg );
+ }
+
+ @Test
+ public void process_unwrap_true() throws ConfigurationException, NamingException, JMSException, ActionProcessingException, URISyntaxException
+ {
+ final ConfigTree tree = createConfigTree();
+ tree.setAttribute( "unwrap", "true" );
+ router = new MockJMSRouter( tree );
+ router.route( msg );
- JMSRouter router = new JMSRouter(tree);
+ assertProcessContract( messageID, msg );
+ final javax.jms.Message jmsMessage = ((MockJMSRouter) router).getJmsMessage();
+
+ assertTrue ( jmsMessage instanceof TextMessage );
+ final javax.jms.TextMessage textMessage = (TextMessage) jmsMessage;
+
+ assertEquals ( textMessage.getJMSCorrelationID(), messageID );
+ assertEquals ( textMessage.getText(), bodyContent );
+
+ }
+
+ @Test ( expected = ActionProcessingException.class )
+ public void route_negative() throws ConfigurationException, NamingException, JMSException, ActionProcessingException
+ {
+ ConfigTree tree = createConfigTree();
+ router = new MockJMSRouter(tree);
+ router.route( "test" );
+ }
+
+ @Test
+ public void process_continue_processing() throws ConfigurationException, NamingException, JMSException, ActionProcessingException
+ {
+ final ConfigTree tree = createConfigTree();
+ tree.setAttribute( JMSRouter.ATTR_CONTINUE_ACTION_PIPELINE_PROCESSING, "true" );
+ router = new MockJMSRouter(tree);
+ Message message = router.process( msg );
+ assertNotNull( "ActionPipe Line processing should continue when 'continue-processing' is true", message );
+ }
+
+ /*
+ * Not quite sure that this is supposed to be testing.
+ * Can we remove this test? /Daniel
+ */
+ @Test
+ @Ignore
+ public void testRouter () throws Exception
+ {
boolean exception = false;
Message msg = MessageFactory.getInstance().getMessage();
@@ -79,4 +176,84 @@
router.getOkNotification(null);
}
+ private void assertProcessContract( final String messageID, final Message msg ) throws ActionProcessingException, JMSException
+ {
+ final Message message = router.process ( msg );
+ assertNull ( "Routers process should return null", message );
+ final javax.jms.Message jmsMessage = ((MockJMSRouter) router).getJmsMessage();
+ assertEquals ( jmsMessage.getJMSCorrelationID(), messageID );
+ }
+
+ private static class MockJMSRouter extends JMSRouter
+ {
+ @SuppressWarnings ( "unused" )
+ private Logger log = Logger.getLogger( MockJMSRouter.class );
+
+ private javax.jms.Message jmsMessage;
+
+ public MockJMSRouter(ConfigTree propertiesTree) throws ConfigurationException, NamingException, JMSException
+ {
+ super( propertiesTree );
+ }
+
+ public MockJMSRouter(String actionName, List<KeyValuePair> properties) throws ConfigurationException
+ {
+ super( actionName, properties );
+ }
+
+ @Override
+ protected void createQueueSetup( String queueName ) throws ConfigurationException { }
+
+ @Override
+ protected void send( javax.jms.Message jmsMessage ) throws JMSException
+ {
+ this.jmsMessage = jmsMessage;
+ }
+
+ public javax.jms.Message getJmsMessage()
+ {
+ return jmsMessage;
+ }
+
+ @Override
+ protected javax.jms.Message createJMSMessageWithObjectType( Object objectFromBody ) throws JMSException
+ {
+ TextMessageImpl textMessage = new TextMessageImpl();
+ textMessage.setText( new String ((byte[])objectFromBody) );
+ return textMessage;
+ }
+
+ @Override
+ protected javax.jms.Message createObjectMessage( Object message ) throws JMSException
+ {
+ ObjectMessageImpl impl = new ObjectMessageImpl();
+ impl.setObject( (Serializable) message );
+ return impl;
+ }
+
+ }
+
+ private Message createESBMessageObject( final String messageID, final String body) throws URISyntaxException
+ {
+ Message msg = MessageFactory.getInstance().getMessage();
+ msg.getHeader().getCall().setMessageID( new URI ( "1234-junittest" ) );
+ msg.getBody().setByteArray( body.getBytes());
+ return msg;
+ }
+
+ private ConfigTree createConfigTree()
+ {
+ ConfigTree tree = new ConfigTree("test");
+ tree.setAttribute("jndiName", "foobar");
+ return tree;
+ }
+
+ /*
+ * Just here to help Ant to find annotated test.
+ */
+ public static junit.framework.Test suite()
+ {
+ return new JUnit4TestAdapter( JmsRouterIntegrationTest.class );
+ }
+
}
More information about the jboss-svn-commits
mailing list