servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r1103659 - in /servicemix/smx4/features/trunk/camel/servicemix-camel/src: main/java/org/apache/servicemix/camel/nmr/ test/java/org/apache/servicemix/camel/nmr/
Date Mon, 16 May 2011 09:28:37 GMT
Author: gertv
Date: Mon May 16 09:28:36 2011
New Revision: 1103659

URL: http://svn.apache.org/viewvc?rev=1103659&view=rev
Log:
SMX4-825: Support timeout property on Camel NMR endpoints

Added:
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java
Modified:
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixEndpoint.java
    servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixProducer.java

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixEndpoint.java?rev=1103659&r1=1103658&r2=1103659&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixEndpoint.java
(original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixEndpoint.java
Mon May 16 09:28:36 2011
@@ -34,10 +34,13 @@ public class ServiceMixEndpoint extends 
 
     private static final String SYNCHRONOUS = "synchronous";
     public static final String RUN_AS_SUBJECT = "runAsSubject";
+    private static final String TIMEOUT = "timeout";
+    private static final Long DEFAULT_TIMEOUT = new Long(0);
 
     private String endpointName;
     private boolean synchronous;
     private boolean runAsSubject;
+    private Long timeOut = DEFAULT_TIMEOUT;
 
     public ServiceMixEndpoint(ServiceMixComponent component, String uri, String endpointName)
{
         super(uri, component);
@@ -48,6 +51,15 @@ public class ServiceMixEndpoint extends 
     public void configureProperties(Map<String, Object> options) {
         synchronous = Boolean.valueOf((String) options.remove(SYNCHRONOUS));
         runAsSubject = Boolean.valueOf((String) options.remove(RUN_AS_SUBJECT));
+        timeOut = parseLongOption(options, TIMEOUT);
+    }
+
+    private Long parseLongOption(Map<String, Object> options, String timeout) {
+        String value = (String) options.remove(TIMEOUT);
+        if (value != null) {
+            return Long.parseLong(value);
+        }
+        return 0l;
     }
 
     public ServiceMixComponent getComponent() {
@@ -65,6 +77,11 @@ public class ServiceMixEndpoint extends 
     public boolean isRunAsSubject() {
         return runAsSubject;
     }
+
+    public Long getTimeOut() {
+        return timeOut;
+    }
+
     
     public Producer createProducer() throws Exception {
         return new ServiceMixProducer(this, getComponent().getNmr());

Modified: servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixProducer.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixProducer.java?rev=1103659&r1=1103658&r2=1103659&view=diff
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixProducer.java
(original)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/main/java/org/apache/servicemix/camel/nmr/ServiceMixProducer.java
Mon May 16 09:28:36 2011
@@ -62,7 +62,7 @@ public class ServiceMixProducer extends 
             ex.printStackTrace();
         }
 
-        channel.sendSync(e);
+        channel.sendSync(e, getEndpoint().getTimeOut());
 
         handleResponse(exchange, channel, e);
     }
@@ -82,10 +82,15 @@ public class ServiceMixProducer extends 
                     ServiceHelper.createMap(org.apache.servicemix.nmr.api.Endpoint.NAME,
                             getEndpoint().getEndpointName())));
 
-            continuations.put(e.getId(), new Continuation(exchange, asyncCallback));
-            channel.send(e);
-
-            return false;
+            if (isSendSyncRequired()) {
+                process(exchange);
+                asyncCallback.done(true);
+                return true;
+            } else {
+                continuations.put(e.getId(), new Continuation(exchange, asyncCallback));
+                channel.send(e);
+                return false;
+            }
         } catch (Exception ex) {
             log.warn("Error occured while sending NMR exchange", ex);
 
@@ -198,4 +203,12 @@ public class ServiceMixProducer extends 
             this.callback = callback;
         }
     }
+
+    /*
+     * Check if sendSync is required for interacting with the NMR.
+     * Currently, sendSync is required only if a timeout has been configured on the endpoint.
+     */
+    private boolean isSendSyncRequired() {
+        return getEndpoint().getTimeOut() > 0;
+    }
 }

Added: servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java?rev=1103659&view=auto
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java
(added)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java
Mon May 16 09:28:36 2011
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.camel.nmr;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.servicemix.nmr.api.AbortedException;
+import org.junit.Test;
+
+/**
+ * Test cases for {@link ServiceMixComponent} and the correct handling of the nmr: URIs
+ */
+public class ServiceMixComponentTest extends AbstractComponentTest {
+
+    private final CamelContext context = new DefaultCamelContext();
+
+    @Test
+    public void testSimpleUri() {
+        ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test");
+        assertNotNull(endpoint);
+    }
+
+    @Test
+    public void testUriRunAsSubject() {
+        ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test");
+        assertNotNull(endpoint);
+        assertFalse(endpoint.isRunAsSubject());
+
+        endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?runAsSubject=false");
+        assertNotNull(endpoint);
+        assertFalse(endpoint.isRunAsSubject());
+
+        endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?runAsSubject=true");
+        assertNotNull(endpoint);
+        assertTrue(endpoint.isRunAsSubject());
+    }
+
+    @Test
+    public void testUriTimeOut() {
+        ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test");
+        assertNotNull(endpoint);
+        assertEquals(new Long(0), endpoint.getTimeOut());
+        endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?timeout=3000");
+        assertNotNull(endpoint);
+        assertEquals(new Long(3000), endpoint.getTimeOut());
+    }
+
+}

Added: servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java?rev=1103659&view=auto
==============================================================================
--- servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java
(added)
+++ servicemix/smx4/features/trunk/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java
Mon May 16 09:28:36 2011
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.camel.nmr;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.servicemix.nmr.api.AbortedException;
+import org.junit.Test;
+
+/**
+ * A basic test to ensure that the 'timeout' property on the endpoint works fine.
+ */
+public class TimeoutTest extends AbstractComponentTest {
+
+    private static final String SLOW_MESSAGE = "Take the slow route, please!";
+    private static final String FAST_MESSAGE = "Get me there as quickly as you can!";
+    private static final String RESPONSE_MESSAGE = "You've arrived at your destination!";
+
+    private static final Long TIMEOUT = 1000l;
+
+    @Test
+    public void testFastInOutWithTimeout() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:timeout");
+        mock.expectedMessageCount(1);
+
+        Exchange result = template.request("direct:timeout", new Processor() {
+
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(FAST_MESSAGE);
+            }
+
+        });
+
+        assertMockEndpointsSatisfied();
+        assertFalse("Exchange got finished successfully", result.isFailed());
+        assertEquals("Response message got set", RESPONSE_MESSAGE, result.getOut().getBody());
+    }
+
+    @Test
+    public void testSlowInOutWithTimeout() throws InterruptedException {
+        MockEndpoint mock = getMockEndpoint("mock:timeout");
+        mock.expectedMessageCount(0);
+
+        Exchange result = template.request("direct:timeout", new Processor() {
+
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(SLOW_MESSAGE);
+            }
+
+        });
+
+        assertTrue("Exchange got finished successfully", result.isFailed());
+        assertFalse("Response message not set", result.hasOut());
+        assertTrue("TimeoutException was thrown", result.getException() instanceof AbortedException);
+    }
+
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                from("direct:timeout")
+                    .choice()
+                        .when(simple("${body} contains slow")).to("nmr:slow-route?timeout="
+ TIMEOUT)
+                        .otherwise().to("nmr:fast-route?timeout=" + TIMEOUT)
+                     .end()
+                     .to("mock:timeout");
+
+                from("nmr:fast-route").process(new ResponseProcessor());
+                from("nmr:slow-route").delay(2 * TIMEOUT).process(new ResponseProcessor());
+            }
+        };
+    }
+
+    private final class ResponseProcessor implements Processor {
+
+        public void process(Exchange exchange) throws Exception {
+            exchange.getOut().setBody(RESPONSE_MESSAGE);
+        }
+    }
+}



Mime
View raw message