[jboss-svn-commits] JBL Code SVN: r12994 - in labs/jbossesb/trunk/product/rosetta: tests/src/org/jboss/soa/esb/notification and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jul 2 02:17:28 EDT 2007


Author: beve
Date: 2007-07-02 02:17:28 -0400 (Mon, 02 Jul 2007)
New Revision: 12994

Modified:
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyQueues.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyTopics.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyQueuesUnitTest.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java
Log:
Work for JIRA452, "Refactor Notifiers"
"Pulled up" the sendToAll method from NotifyQueues and NotifyTopics as most the code was
duplicated in both classes. 
Made sendToAll a template method that calls send on the specific 
subclass (the code that was different in NotifyQueues and NotifyTopics.

sendToAll now throws JMSException is any of the send fail. This will still try to send to all
configured queues/topics but will later throw a jms message with the error and stacktrace of 
all failed sends. Previously exceptions were simply being logged but no NotificationException was
getting throws from sendNotfication().

Also added test for the methods in question (not added in both unit test as I it enough to test one)



Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java	2007-07-02 04:33:18 UTC (rev 12993)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyJMS.java	2007-07-02 06:17:28 UTC (rev 12994)
@@ -22,6 +22,8 @@
 
 package org.jboss.soa.esb.notification;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.Iterator;
 import java.util.Properties;
 
@@ -54,14 +56,18 @@
 public abstract class NotifyJMS extends NotificationTarget
 {
     protected Logger log = Logger.getLogger(this.getClass());
+	
+	
+	
 	/**
 	 * Abstract method - All classes that extend NotifyJMS must implement it
 	 * according to their own javax.jms.Destination needs (Queue/Topic)
 	 * 
-	 * @param p_oMsg
-	 *            Message
+	 * @param p_oMsg			the JMS Message to send
+	 * @param msgProducer		the JMS Message Producer which will perform the sending
+	 * @throws JMSException 	if the send was not sucessful
 	 */
-	protected abstract void sendToAll (Message p_oMsg);
+	protected abstract void send(final Message p_oMsg, MessageProducer msgProducer ) throws JMSException;
 
 	/**
 	 * Element name mnemonic to search for child elements in the ConfigTree at
@@ -204,5 +210,61 @@
 			throw new NotificationException(ex);
 		}
 	} // __________________________________
-
+	
+	/**
+	 * Template method for sending JMS messages to destinations.
+	 * </p>
+	 * Subclasses implement send(Message, MessageProducer) which 
+	 * performs sending/publishing.
+	 * 
+	 * @param p_oMsg			the JMS Message to send.
+	 * @throws JMSException 	if any of the sends fail a JMSExcpetion will be
+	 * 							thrown. The exception will contain an error
+	 * 							message and stacktrace for every failed send. 
+	 */
+	protected void sendToAll (final Message p_oMsg) throws JMSException
+	{
+		try 
+		{
+            final StringBuilder jmsExceptions = new StringBuilder();
+			for (int i1 = 0; i1 < m_oaMssProd.length; i1++)
+			{
+				try
+				{
+					send( p_oMsg, m_oaMssProd[i1]);
+				}
+				catch (final JMSException e)
+				{
+					final String msg = "[JMSException while sending to : " + m_oaMssProd[i1].getDestination();
+	                log.error(msg, e);
+	                jmsExceptions.append( createExceptionErrorString( msg, e ));
+				}
+			}
+			if ( jmsExceptions.length() > 0 )
+				throw new JMSException( jmsExceptions.toString() );
+		}
+		finally
+		{
+			release();
+		}
+	}
+	
+	/*
+	 * Util method for generating an error message containing the
+	 * stacktrace for the passed in Exception instance.
+	 * 
+	 * Note that there is no need to close a StringWriter instance.
+	 */
+	protected String createExceptionErrorString( final String msg, final Exception e )
+	{
+		StringBuilder sb = new StringBuilder();
+        sb.append("[").append(msg);
+        sb.append(", Stacktrace : ");
+        StringWriter sw = new StringWriter();
+        e.printStackTrace(new PrintWriter(sw));
+        sb.append(sw.toString());
+        sb.append("]");
+        return sb.toString();
+	}
+	
 } // ____________________________________________________________________________

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyQueues.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyQueues.java	2007-07-02 04:33:18 UTC (rev 12993)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyQueues.java	2007-07-02 06:17:28 UTC (rev 12994)
@@ -139,24 +139,11 @@
 			throw new ConfigurationException(ex);
 		}
 	}
-	/**
-	 * Send a message to all the configured queues.
-	 */
-	protected void sendToAll (Message p_oMsg)
+	
+	protected void send (final Message p_oMsg, MessageProducer msgProducer ) throws JMSException
 	{
-		for (int i1 = 0; i1 < m_oaMssProd.length; i1++)
-		{
-			QueueSender oCurr = (QueueSender) m_oaMssProd[i1];
-			try
-			{
-				oCurr.send(p_oMsg);
-			}
-			catch (Exception e)
-			{
-				log.error("Could not send message " + p_oMsg, e);
-			}
-		}
-		release();
+		QueueSender oCurr = (QueueSender) msgProducer;
+		oCurr.send(p_oMsg);
 	}
 
 }

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyTopics.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyTopics.java	2007-07-02 04:33:18 UTC (rev 12993)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/notification/NotifyTopics.java	2007-07-02 06:17:28 UTC (rev 12994)
@@ -119,22 +119,10 @@
 		}
 	}
 
-	protected void sendToAll (Message p_oMsg)
+	protected void send (final Message p_oMsg, MessageProducer msgProducer ) throws JMSException
 	{
-		for (int i1 = 0; i1 < m_oaMssProd.length; i1++)
-		{
-			TopicPublisher oCurr = (TopicPublisher) m_oaMssProd[i1];
-			
-			try
-			{
-				oCurr.publish(p_oMsg);
-			}
-			catch (Exception e)
-			{
-                log.error(e.getMessage(), e);
-			}
-		}
-		release();
-	} // __________________________________
-
+		TopicPublisher oCurr = (TopicPublisher) msgProducer;
+		oCurr.publish(p_oMsg);
+	}
+	
 } // ____________________________________________________________________________

Modified: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyQueuesUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyQueuesUnitTest.java	2007-07-02 04:33:18 UTC (rev 12993)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyQueuesUnitTest.java	2007-07-02 06:17:28 UTC (rev 12994)
@@ -21,37 +21,65 @@
  */
 package org.jboss.soa.esb.notification;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.Serializable;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.nio.ByteBuffer;
 
+import javax.jms.BytesMessage;
+import javax.jms.Destination;
 import javax.jms.ExceptionListener;
 import javax.jms.JMSException;
+import javax.jms.MapMessage;
 import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
 import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.QueueBrowser;
 import javax.jms.QueueConnection;
+import javax.jms.QueueSender;
+import javax.jms.Session;
+import javax.jms.StreamMessage;
+import javax.jms.TemporaryQueue;
+import javax.jms.TemporaryTopic;
 import javax.jms.TextMessage;
+import javax.jms.Topic;
+import javax.jms.TopicSubscriber;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
-import junit.framework.TestCase;
+import junit.framework.JUnit4TestAdapter;
 
 import org.apache.log4j.Logger;
+import org.jboss.internal.soa.esb.rosetta.pooling.ConnectionException;
+import org.jboss.internal.soa.esb.rosetta.pooling.JmsConnectionPool;
+import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.message.format.MessageFactory;
 import org.jboss.soa.esb.message.format.MessageType;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 import org.mockejb.jms.MockQueue;
 import org.mockejb.jms.MockTopic;
+import org.mockejb.jms.ObjectMessageImpl;
 import org.mockejb.jms.QueueConnectionFactoryImpl;
+import org.mockejb.jms.TextMessageImpl;
 import org.mockejb.jndi.MockContextFactory;
 
 /**
  * NotifyQueues unit tests.
  * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
  */
-public class NotifyQueuesUnitTest extends TestCase {
+public class NotifyQueuesUnitTest {
 	
 	private Logger log = Logger.getLogger( NotifyQueuesUnitTest.class );
 
@@ -59,13 +87,16 @@
 	private MockQueue mockQueue2;
 	private NotifyQueues notifyQueues;
 	
-	protected void setUp() {
+	private ConfigTree rootEl;
+	
+	@Before
+	public void setUp() {
         try {
     		MockContextFactory.setAsInitial();		
     		Context ctx = new InitialContext();
     		ctx.rebind(NotifyQueues.CONNECTION_FACTORY, new MockQueueConnectionFactory());
     		ctx.close();
-    		ConfigTree rootEl = new ConfigTree("rootEl");
+    		rootEl = new ConfigTree("rootEl");
     
     		addMessagePropertyConfigs(rootEl);
     		addQueueConfig(rootEl, "queue1");
@@ -78,12 +109,14 @@
         	log.error(e);
         }
 	}
-
-	protected void tearDown() throws Exception {
+	
+	@After
+	public void tearDown() throws Exception {
 		notifyQueues.release();
 		MockContextFactory.revertSetAsInitial();		
 	}
-
+	
+	@Test
 	public void test_StringObj() throws Exception {
         org.jboss.soa.esb.message.Message message = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
         message.getBody().setByteArray("Hello".getBytes());
@@ -92,7 +125,8 @@
 		checkQueueTextMessage(mockQueue1, 0, "Hello");
 		checkQueueTextMessage(mockQueue2, 0, "Hello");
 	}
-	
+
+	@Test
 	public void test_NonStringObj() throws Exception {
         org.jboss.soa.esb.message.Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
         message.getBody().setByteArray(((new Integer(123).toString().getBytes())));
@@ -101,7 +135,41 @@
 		checkQueueObjectMessage(mockQueue1, 0, new Integer(123).toString().getBytes());
 		checkQueueObjectMessage(mockQueue2, 0, new Integer(123).toString().getBytes());
 	}
-
+	
+	@Test
+	public void sendNotification_without_body() 
+	{
+        org.jboss.soa.esb.message.Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
+		try
+		{
+			notifyQueues.sendNotification(message);
+		} catch (NotificationException e)
+		{
+			fail(e.getMessage());
+		}
+	}
+	
+	@Test
+	public void release() 
+	{
+		notifyQueues.release();
+	}
+	
+	@Test ( expected = JMSException.class )
+	public void sendToAll_negative() throws ConfigurationException, JMSException, ConnectionException
+	{
+		NotifyQueues notifyQueues = new MockNofityQueues(rootEl);
+		notifyQueues.sendToAll(new TextMessageImpl("junit test"));
+	}
+	
+	@Test ( expected = NotificationException.class )
+	public void sendNotification_negative() throws ConfigurationException, JMSException, ConnectionException, NotificationException 
+	{
+		NotifyQueues notifyQueues = new MockNofityQueues(rootEl);
+        org.jboss.soa.esb.message.Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
+		notifyQueues.sendNotification(message);
+	}
+	
 	private void checkQueueTextMessage(MockQueue mockQueue, int messageIdx, String expectedText) throws JMSException {
 		assertTrue(mockQueue.getMessages().size() > messageIdx);		
 		Message message = mockQueue.getMessageAt(0);
@@ -160,43 +228,134 @@
 		return mockTopic;
 	}
         
-        private static final class MockQueueConnectionFactory extends QueueConnectionFactoryImpl
+    private static final class MockQueueConnectionFactory extends QueueConnectionFactoryImpl
+    {
+        @Override
+        public QueueConnection createQueueConnection() throws JMSException
         {
-            @Override
-            public QueueConnection createQueueConnection() throws JMSException
-            {
-                return (QueueConnection)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {QueueConnection.class},
-                        new MockQueueExceptionHandlerInvocationHandler(super.createQueueConnection())) ;
-            }
+            return (QueueConnection)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {QueueConnection.class},
+                    new MockQueueExceptionHandlerInvocationHandler(super.createQueueConnection())) ;
         }
+    }
         
-        private static final class MockQueueExceptionHandlerInvocationHandler implements InvocationHandler
+    private static final class MockQueueExceptionHandlerInvocationHandler implements InvocationHandler
+    {
+        private final QueueConnection queueConnection ;
+        private ExceptionListener exceptionListener ;
+            
+        MockQueueExceptionHandlerInvocationHandler(final QueueConnection queueConnection)
         {
-            private final QueueConnection queueConnection ;
-            private ExceptionListener exceptionListener ;
+            this.queueConnection = queueConnection ;
+        }
             
-            MockQueueExceptionHandlerInvocationHandler(final QueueConnection queueConnection)
+        public Object invoke(final Object proxy, final Method method, final Object[] args)
+            throws Throwable
+        {
+            final String methodName = method.getName() ;
+            if ("setExceptionListener".equals(methodName))
             {
-                this.queueConnection = queueConnection ;
+                exceptionListener = (ExceptionListener)args[0] ;
+                return null ;
             }
-            
-            public Object invoke(final Object proxy, final Method method, final Object[] args)
-                throws Throwable
+            else if ("getExceptionListener".equals(methodName))
             {
-                final String methodName = method.getName() ;
-                if ("setExceptionListener".equals(methodName))
-                {
-                    exceptionListener = (ExceptionListener)args[0] ;
-                    return null ;
-                }
-                else if ("getExceptionListener".equals(methodName))
-                {
-                    return exceptionListener ;
-                }
-                else
-                {
-                    return method.invoke(queueConnection, args) ;
-                }
+                return exceptionListener ;
             }
+            else
+            {
+                return method.invoke(queueConnection, args) ;
+            }
         }
+    }
+        
+    private static class MockNofityQueues extends NotifyQueues
+    {
+
+		public MockNofityQueues(ConfigTree p_oP) throws ConfigurationException, JMSException, ConnectionException
+		{
+			super( p_oP );
+		}
+		
+		@Override
+		protected void setQueues (ConfigTree[] p_oaP) throws ConfigurationException, JMSException, ConnectionException
+		{
+			m_oaMssProd = new MessageProducer[1];
+            mPool = new JmsConnectionPool[0];
+            m_oSess = new Session[1];
+			m_oaMssProd[0] = new MockSender(); 
+			m_oSess[0] = new MockSession();
+			
+		}
+    }
+    private static class MockSender implements QueueSender
+    {
+		public void send( Message arg0 ) throws JMSException 
+		{ 
+			throw new JMSException( "MockJMSException");
+		}
+		public Destination getDestination() throws JMSException { return null; }
+		
+		public Queue getQueue() throws JMSException { return null; }
+		public void send( Queue arg0, Message arg1 ) throws JMSException { }
+		public void send( Message arg0, int arg1, int arg2, long arg3 ) throws JMSException { }
+		public void send( Queue arg0, Message arg1, int arg2, int arg3, long arg4 ) throws JMSException { }
+		public void close() throws JMSException { 		}
+		public int getDeliveryMode() throws JMSException { return 0; }
+		public boolean getDisableMessageID() throws JMSException { return false; }
+		public boolean getDisableMessageTimestamp() throws JMSException { return false; }
+		public int getPriority() throws JMSException { return 0; }
+		public long getTimeToLive() throws JMSException { return 0; }
+		public void send( Destination arg0, Message arg1 ) throws JMSException { 		}
+		public void send( Destination arg0, Message arg1, int arg2, int arg3, long arg4 ) throws JMSException { 		}
+		public void setDeliveryMode( int arg0 ) throws JMSException { }
+		public void setDisableMessageID( boolean arg0 ) throws JMSException { 		}
+		public void setDisableMessageTimestamp( boolean arg0 ) throws JMSException { 		}
+		public void setPriority( int arg0 ) throws JMSException { 		}
+		public void setTimeToLive( long arg0 ) throws JMSException { }
+    }
+    private static class MockSession implements Session
+    {
+		public ObjectMessage createObjectMessage() throws JMSException 
+		{ 
+			return null; 
+		}
+		public ObjectMessage createObjectMessage( Serializable arg0 ) throws JMSException 
+		{ 
+			return new ObjectMessageImpl( arg0 ); 
+		}
+		public void close() throws JMSException { }
+		public void commit() throws JMSException { }
+		public QueueBrowser createBrowser( Queue arg0 ) throws JMSException { return null; }
+		public QueueBrowser createBrowser( Queue arg0, String arg1 ) throws JMSException { return null; }
+		public BytesMessage createBytesMessage() throws JMSException { return null; }
+		public MessageConsumer createConsumer( Destination arg0 ) throws JMSException { return null; }
+		public MessageConsumer createConsumer( Destination arg0, String arg1 ) throws JMSException { return null; }
+		public MessageConsumer createConsumer( Destination arg0, String arg1, boolean arg2 ) throws JMSException { return null; }
+		public TopicSubscriber createDurableSubscriber( Topic arg0, String arg1 ) throws JMSException { return null; }
+		public TopicSubscriber createDurableSubscriber( Topic arg0, String arg1, String arg2, boolean arg3 ) throws JMSException { return null; }
+		public MapMessage createMapMessage() throws JMSException { return null; }
+		public Message createMessage() throws JMSException { return null; }
+		public MessageProducer createProducer( Destination arg0 ) throws JMSException { return null; }
+		public Queue createQueue( String arg0 ) throws JMSException { return null; }
+		public StreamMessage createStreamMessage() throws JMSException { return null; }
+		public TemporaryQueue createTemporaryQueue() throws JMSException { return null; }
+		public TemporaryTopic createTemporaryTopic() throws JMSException { return null; }
+		public TextMessage createTextMessage() throws JMSException { return null; }
+		public TextMessage createTextMessage( String arg0 ) throws JMSException { return null; }
+		public Topic createTopic( String arg0 ) throws JMSException { return null; }
+		public int getAcknowledgeMode() throws JMSException { return 0; }
+		public MessageListener getMessageListener() throws JMSException {  return null; }
+		public boolean getTransacted() throws JMSException { return false; }
+		public void recover() throws JMSException { 		}
+		public void rollback() throws JMSException { 		}
+		public void run() { 		}
+		public void setMessageListener( MessageListener arg0 ) throws JMSException { 		}
+		public void unsubscribe( String arg0 ) throws JMSException { 		}
+    	
+    }
+        
+    public static junit.framework.Test suite()
+	{
+		return new JUnit4TestAdapter(NotifyQueuesUnitTest.class);
+	}
 }

Modified: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java	2007-07-02 04:33:18 UTC (rev 12993)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/notification/NotifyTopicsUnitTest.java	2007-07-02 06:17:28 UTC (rev 12994)
@@ -93,6 +93,19 @@
 		checkTopicObjectMessage(mockTopic1, 0, new Integer(123).toString().getBytes());
 		checkTopicObjectMessage(mockTopic2, 0, new Integer(123).toString().getBytes());
 	}
+	
+	public void test_createException()
+	{
+		final String msg = "[JMSException while publishing to : /topic/SomeName";
+		try
+		{
+			String string = notifyTopics.createExceptionErrorString( msg, new JMSException("junit test dummy exception") );
+			assertNotNull( string );
+		}catch(Exception e )
+		{
+			fail(e.getMessage());
+		}
+	}
 
 	private void checkTopicTextMessage(MockTopic mockTopic, int messageIdx, String expectedText) throws JMSException {
 		assertTrue(mockTopic.getMessages().size() > messageIdx);		




More information about the jboss-svn-commits mailing list