servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r752229 - in /servicemix/smx4/nmr/trunk/nmr: api/src/main/java/org/apache/servicemix/nmr/api/ core/src/main/java/org/apache/servicemix/nmr/core/ core/src/test/java/org/apache/servicemix/nmr/core/
Date Tue, 10 Mar 2009 19:50:25 GMT
Author: gertv
Date: Tue Mar 10 19:50:24 2009
New Revision: 752229

URL: http://svn.apache.org/viewvc?rev=752229&view=rev
Log:
SMX4NMR-118: Allow canceling an Exchange that has been sent synchronously and is waiting for
a response

Modified:
    servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Exchange.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
    servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/ExchangeImplTest.java

Modified: servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Exchange.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Exchange.java?rev=752229&r1=752228&r2=752229&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Exchange.java
(original)
+++ servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Exchange.java
Tue Mar 10 19:50:24 2009
@@ -322,6 +322,13 @@
      * @return the string representation
      */
     String display(boolean displayContent);
+    
+    /**
+     * Cancels a pending synchronous exchange.
+     * 
+     * The canceled exchange's status will be set to {@link Status#Error}   
+     */
+    void cancel();
 
 
 }

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java?rev=752229&r1=752228&r2=752229&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
(original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
Tue Mar 10 19:50:24 2009
@@ -474,5 +474,16 @@
         }
         return converter;
     }
-
+    
+    public void cancel() {
+        cancel(consumerLock);
+        cancel(providerLock);
+    }
+    
+    private void cancel(Semaphore lock) {
+        if (lock != null) {
+            setStatus(Status.Error);
+            lock.release();
+        }
+    }
 }

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/ExchangeImplTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/ExchangeImplTest.java?rev=752229&r1=752228&r2=752229&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/ExchangeImplTest.java
(original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/ExchangeImplTest.java
Tue Mar 10 19:50:24 2009
@@ -22,6 +22,10 @@
 import java.io.ObjectOutputStream;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.net.URLClassLoader;
 
 import javax.xml.namespace.QName;
@@ -31,6 +35,8 @@
 import org.apache.servicemix.nmr.api.Pattern;
 import org.apache.servicemix.nmr.api.Status;
 import org.apache.servicemix.nmr.api.Type;
+import org.apache.servicemix.nmr.api.internal.InternalExchange;
+
 import junit.framework.TestCase;
 
 
@@ -204,6 +210,28 @@
 		assertNotNull(e.getFault());
 	}
 
+	public void testCancel() throws InterruptedException {
+	    final InternalExchange e = new ExchangeImpl(Pattern.InOnly);
+	    final CountDownLatch latch = new CountDownLatch(1);
+	    Thread thread = new Thread() {
+	        @Override
+	        public void run() {
+	            try {
+	                e.getConsumerLock(true).acquire();
+	                latch.countDown();
+	            } catch (InterruptedException e) {
+	                fail(e.getMessage());
+	            }
+	        }
+	    };
+	    thread.start();
+	    //let's sleep for a moment to make sure the thread has acquired the lock
+	    Thread.sleep(150);
+	    e.cancel();
+	    assertTrue("Exchange should have been cancelled", latch.await(1, TimeUnit.SECONDS));
+	    assertEquals(Status.Error, e.getStatus());
+	}
+
     public static Map<String, Object> createMap(String... data) {
         Map<String, Object> props = new HashMap<String, Object>();
         for (int i = 0; i < data.length / 2; i++) {
@@ -211,5 +239,4 @@
         }
         return props;
     }
-
 }



Mime
View raw message