You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/07/04 16:08:55 UTC

[2/2] git commit: CAMEL-7563: Hazelcast instance as an endpoint url param with thanks to Alexander Lomov

 CAMEL-7563: Hazelcast instance as an endpoint url param with thanks to Alexander Lomov


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b505dd81
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b505dd81
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b505dd81

Branch: refs/heads/master
Commit: b505dd8182e5f4dc0ebf2e89c1a9824045189bec
Parents: 9c2ea18
Author: Willem Jiang <wi...@gmail.com>
Authored: Fri Jul 4 20:40:09 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Fri Jul 4 22:08:24 2014 +0800

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastComponent.java | 29 +++++++++---
 ...astComponentInstanceReferenceSpringTest.java | 33 +++++++++++++
 ...mel-context-hazelcast-instance-reference.xml | 49 ++++++++++++++++++++
 3 files changed, 104 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b505dd81/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
index 4731bab..c77d4a6 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
@@ -51,6 +51,21 @@ public class HazelcastComponent extends DefaultComponent {
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
 
+        // Query param named 'hazelcastInstance' (if exists) overrides the instance that was set
+        // programmatically and cancels local instance creation as well.
+        HazelcastInstance hzInstance = resolveAndRemoveReferenceParameter(parameters, "hazelcastInstance",
+                HazelcastInstance.class);
+        if (hzInstance != null) {
+            hazelcastInstance = hzInstance;
+            createOwnInstance = false;
+        }
+
+        // Instance was neither set programmtically nor provided as a bean reference.
+        if (hazelcastInstance == null) {
+            createOwnInstance = true;
+            createOwnInstance();
+        }
+
         HazelcastDefaultEndpoint endpoint = null;
 
         // check type of endpoint
@@ -110,13 +125,6 @@ public class HazelcastComponent extends DefaultComponent {
     @Override
     public void doStart() throws Exception {
         super.doStart();
-        if (hazelcastInstance == null) {
-            createOwnInstance = true;
-            Config config = new XmlConfigBuilder().build();
-            // Disable the version check
-            config.getProperties().setProperty("hazelcast.version.check.enabled", "false");
-            hazelcastInstance = Hazelcast.newHazelcastInstance(config);
-        }
     }
 
     @Override
@@ -134,4 +142,11 @@ public class HazelcastComponent extends DefaultComponent {
     public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
         this.hazelcastInstance = hazelcastInstance;
     }
+
+    private void createOwnInstance() {
+        Config config = new XmlConfigBuilder().build();
+        // Disable the version check
+        config.getProperties().setProperty("hazelcast.version.check.enabled", "false");
+        hazelcastInstance = Hazelcast.newHazelcastInstance(config);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b505dd81/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceSpringTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceSpringTest.java b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceSpringTest.java
new file mode 100644
index 0000000..9e84047
--- /dev/null
+++ b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceSpringTest.java
@@ -0,0 +1,33 @@
+package org.apache.camel.component.hazelcast;
+
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+public class HazelcastComponentInstanceReferenceSpringTest extends HazelcastCamelSpringTestSupport {
+
+    private static final String TEST_VALUE = "TestValue";
+    private static final String TEST_KEY = "TestKey";
+
+
+    @Test
+    public void testComparePutAndGet() {
+        template.sendBodyAndHeader("direct:testHazelcastInstanceBeanRefPut", TEST_VALUE,
+                HazelcastConstants.OBJECT_ID, TEST_KEY);
+
+        template.sendBodyAndHeader("direct:testHazelcastInstanceBeanRefGet", null,
+                HazelcastConstants.OBJECT_ID, TEST_KEY);
+        final Object testValueReturn = consumer.receiveBody("seda:out");
+
+        assertEquals(TEST_VALUE, testValueReturn);
+    }
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext(
+                "/META-INF/spring/test-camel-context-hazelcast-instance-reference.xml"
+        );
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/b505dd81/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-reference.xml
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-reference.xml b/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-reference.xml
new file mode 100644
index 0000000..d255013
--- /dev/null
+++ b/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-reference.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
+       xsi:schemaLocation="
+	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+    <bean id="hazelcastInstance" class="com.hazelcast.core.Hazelcast"
+          factory-method="newHazelcastInstance" />
+    <bean id="hazelcastLifecycle" class="com.hazelcast.core.LifecycleService"
+          factory-bean="hazelcastInstance" factory-method="getLifecycleService"
+          destroy-method="shutdown" />
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+        <route id="testHazelcastInstanceBeanRefPut">
+            <from uri="direct:testHazelcastInstanceBeanRefPut"/>
+            <setHeader headerName="CamelHazelcastOperationType">
+                <constant>put</constant>
+            </setHeader>
+            <to uri="hazelcast:map:testmap?hazelcastInstance=#hazelcastInstance"/>
+        </route>
+
+        <route id="testHazelcastInstanceBeanRefGet">
+            <from uri="direct:testHazelcastInstanceBeanRefGet" />
+            <setHeader headerName="CamelHazelcastOperationType">
+                <constant>get</constant>
+            </setHeader>
+            <to uri="hazelcast:map:testmap?hazelcastInstance=#hazelcastInstance"/>
+            <to uri="seda:out" />
+        </route>
+    </camelContext>
+
+</beans>