You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2010/03/19 15:10:59 UTC

svn commit: r925228 - in /servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src: main/java/org/apache/servicemix/camel/ test/java/org/apache/servicemix/camel/

Author: gertv
Date: Fri Mar 19 14:10:59 2010
New Revision: 925228

URL: http://svn.apache.org/viewvc?rev=925228&view=rev
Log:
SMXCOMP-687: Ensure CamelContext lifecycle remains in sync with JBI SU lifecycle

Added:
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java
Removed:
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelControlBus.java
Modified:
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java

Added: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java?rev=925228&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelContextEndpoint.java Fri Mar 19 14:10:59 2010
@@ -0,0 +1,91 @@
+/*
+ * 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;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessageExchange.Role;
+import javax.jbi.messaging.MessagingException;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.ServiceSupport;
+import org.apache.servicemix.common.endpoints.ProviderEndpoint;
+
+/**
+ * JBI endpoint that provides access to an underlying {@link org.apache.camel.CamelContext} and
+ * manages the lifecycle for that endpoint
+ */
+public class CamelContextEndpoint extends ProviderEndpoint {
+
+    /**
+     * The default {@link org.apache.servicemix.camel.CamelContextEndpoint} service name
+     */
+    public static final QName SERVICE_NAME = new QName("http://activemq.apache.org/camel/schema/jbi", "camelcontext");
+
+    private final CamelContext camelContext;
+
+    public CamelContextEndpoint(CamelContext camelContext, String su) {
+        this.camelContext = camelContext;
+        setService(SERVICE_NAME);
+        setEndpoint(su + "-controlbus");
+    }
+
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void process(MessageExchange exchange) throws Exception {
+        if (exchange.getRole() == Role.PROVIDER) {
+            if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+                //TODO: actually implement some control bus operations
+                fail(exchange, new UnsupportedOperationException("No control bus operations available"));
+            }
+        } else {
+            throw new MessagingException(
+                    "Unexpected exchange role: CamelContextEndpoint is not capable of handling " + exchange.getRole());
+        }
+
+    }
+
+    @Override
+    public void start() throws Exception {
+        super.start();
+
+        if (isStartRequired()) {
+            camelContext.start();
+        }
+    }
+
+    @Override
+    public void stop() throws Exception {
+        super.stop();
+
+        if (isStopRequired()) {
+            camelContext.stop();
+        }
+    }
+
+    private boolean isStartRequired() {
+        return ((ServiceSupport) camelContext).isStopping() || ((ServiceSupport) camelContext).isStopped();
+    }
+
+    private boolean isStopRequired() {
+        return ((ServiceSupport) camelContext).isStarting() || ((ServiceSupport) camelContext).isStarted();
+    }
+}

Modified: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java?rev=925228&r1=925227&r2=925228&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/main/java/org/apache/servicemix/camel/CamelSpringDeployer.java Fri Mar 19 14:10:59 2010
@@ -23,7 +23,6 @@ import java.util.List;
 import javax.jbi.management.DeploymentException;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.component.bean.BeanComponent;
 import org.apache.camel.spring.SpringCamelContext;
 import org.apache.servicemix.common.ServiceUnit;
 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer;
@@ -118,12 +117,10 @@ public class CamelSpringDeployer extends
                         services.add(jbiComponent.createJbiEndpointFromCamel(endpoint));
                     }
                 }
-                // lets add a control bus endpoint to ensure we have at least
-                // one endpoint to deploy
-                BeanComponent beanComponent = camelContext.getComponent("bean", BeanComponent.class);
-                Endpoint endpoint = beanComponent.createEndpoint(new CamelControlBus(camelContext),
-                                                                 "camel:" + serviceUnitName + "-controlBus");
-                services.add(jbiComponent.createJbiEndpointFromCamel(endpoint));
+                
+                // Here we just add a CamelContextEndpoint to delegate the servicemix start and stop lifecycle call
+                CamelContextEndpoint camelContextEndpoint = new CamelContextEndpoint(camelContext, serviceUnitName);
+                services.add(camelContextEndpoint);
             }
             return services;
         } catch (Exception e) {

Added: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java?rev=925228&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/CamelContextEndpointLifeCycleTest.java Fri Mar 19 14:10:59 2010
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.net.URI;
+import java.net.URL;
+import java.util.Collection;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.ServiceSupport;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.common.Endpoint;
+import org.apache.servicemix.common.ServiceUnit;
+
+/**
+ * Test cases to ensure that {@link org.apache.servicemix.camel.CamelContextEndpoint} correctly
+ * manages the lifecycle of the underlying {@link org.apache.camel.CamelContext}
+ */
+public class CamelContextEndpointLifeCycleTest extends NonJbiCamelEndpointsIntegrationTest {
+    private static final transient Log LOG = LogFactory.getLog(CamelContextEndpointLifeCycleTest.class);
+
+    public void testComponentInstallation() throws Exception {
+        String serviceUnitConfiguration = suName + "-src/camel-context.xml";
+
+        CamelJbiComponent component = new CamelJbiComponent();
+        container.activateComponent(component, "#ServiceMixComponent#");
+        URL url = getClass().getResource(serviceUnitConfiguration);
+        File path = new File(new URI(url.toString()));
+        path = path.getParentFile();
+        try {
+            // Deploy and start su
+            component.getServiceUnitManager().deploy(suName, path.getAbsolutePath());
+            component.getServiceUnitManager().init(suName, path.getAbsolutePath());
+            component.getServiceUnitManager().start(suName);
+
+            ServiceUnit su = component.getRegistry().getServiceUnit(suName);
+            Collection<Endpoint> endpoints = su.getEndpoints();
+            assertEquals("There should have one Endpoint", 1, endpoints.size());
+            Endpoint endpoint = endpoints.iterator().next();
+            assertTrue("It should be CamelContextEndpoint", endpoint instanceof CamelContextEndpoint);
+            CamelContext camelContext = ((CamelContextEndpoint)endpoint).getCamelContext();
+            // check the CamelContextEndpoint status
+            assertTrue("The CamelContext should be started", ((ServiceSupport)camelContext).isStarted());
+            // Stop
+            component.getServiceUnitManager().stop(suName);
+            // check the CamelContextEndpoint status
+            assertTrue("The CamelContext should be stopped", ((ServiceSupport)camelContext).isStopped());
+            // reStart
+            component.getServiceUnitManager().start(suName);
+            assertTrue("The CamelContext should be started", ((ServiceSupport)camelContext).isStarted());
+
+            component.getServiceUnitManager().stop(suName);
+            component.getServiceUnitManager().shutDown(suName);
+            component.getServiceUnitManager().undeploy(suName, path.getAbsolutePath());
+        } catch (Exception e) {
+            LOG.error("Caught: " + e, e);
+            throw e;
+        }
+    }
+}

Modified: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java?rev=925228&r1=925227&r2=925228&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-camel/src/test/java/org/apache/servicemix/camel/NonJbiCamelEndpointsIntegrationTest.java Fri Mar 19 14:10:59 2010
@@ -153,7 +153,7 @@ public class NonJbiCamelEndpointsIntegra
     protected void configureExchange(ServiceMixClient client,
             MessageExchange exchange) {
         ServiceEndpoint endpoint = client.getContext().getEndpoint(
-                CamelProviderEndpoint.SERVICE_NAME, "camel:su1-controlBus");
+                CamelContextEndpoint.SERVICE_NAME, "su1-controlbus");
         assertNotNull("Should have a Camel endpoint exposed in JBI!", endpoint);
         exchange.setEndpoint(endpoint);
     }