Repository: camel
Updated Branches:
  refs/heads/master ad114f957 -> 615bc2355


CAMEL-10074: ServiceCall EIP : Support caching ServerCallServerListStrategy


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

Branch: refs/heads/master
Commit: 181e8f8bf1cc4a87b96e349f5ff375e6d26a2408
Parents: ad114f9
Author: lburgazzoli <[email protected]>
Authored: Mon Jun 20 12:28:45 2016 +0200
Committer: lburgazzoli <[email protected]>
Committed: Mon Jun 20 15:57:30 2016 +0200

----------------------------------------------------------------------
 .../CachingServiceCallServiceListStrategy.java  | 105 +++++++++++++++++++
 ...chingServiceCallServiceListStrategyTest.java |  56 ++++++++++
 2 files changed, 161 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/181e8f8b/camel-core/src/main/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategy.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategy.java
 
b/camel-core/src/main/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategy.java
new file mode 100644
index 0000000..1ad9b74
--- /dev/null
+++ 
b/camel-core/src/main/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategy.java
@@ -0,0 +1,105 @@
+/**
+ * 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.camel.impl.remote;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.spi.ServiceCallServer;
+import org.apache.camel.spi.ServiceCallServerListStrategy;
+import org.apache.camel.util.ObjectHelper;
+
+public class CachingServiceCallServiceListStrategy<T extends 
ServiceCallServer> implements ServiceCallServerListStrategy<T> {
+    private final ServiceCallServerListStrategy<T> delegate;
+    private List<T> servers;
+    private long lastUpdate;
+    private long timeout;
+
+    public 
CachingServiceCallServiceListStrategy(ServiceCallServerListStrategy<T> 
delegate) {
+        this.delegate = ObjectHelper.notNull(delegate, "delegate");
+        this.lastUpdate = 0;
+        this.servers = Collections.emptyList();
+        this.timeout = 60 * 1000; // 1 min;
+    }
+
+    public void setTimeout(long timeout) {
+        this.timeout = timeout;
+    }
+
+    public void setTimeout(long timeout, TimeUnit unit) {
+        this.timeout = unit.toMillis(timeout);
+    }
+
+    public long getTimeout() {
+        return timeout;
+    }
+
+    public CachingServiceCallServiceListStrategy<T> timeout(long timeout) {
+        setTimeout(timeout);
+        return this;
+    }
+
+    public CachingServiceCallServiceListStrategy<T> timeout(long timeout, 
TimeUnit unit) {
+        setTimeout(timeout, unit);
+        return this;
+    }
+
+    @Override
+    public List<T> getInitialListOfServers(String name) {
+        return delegate.getInitialListOfServers(name);
+    }
+
+    @Override
+    public List<T> getUpdatedListOfServers(String name) {
+        long now = System.currentTimeMillis();
+
+        if (lastUpdate == 0 || now > lastUpdate + timeout) {
+            List<T> updatedList = delegate.getUpdatedListOfServers(name);
+            if (updatedList.isEmpty()) {
+                servers = Collections.emptyList();
+            } else {
+                // List is copied as the delegated 
ServiceCallServerListStrategy
+                // may update the list
+                servers = Collections.unmodifiableList(new 
ArrayList<>(updatedList));
+            }
+
+            lastUpdate = now;
+        }
+
+        return servers;
+    }
+
+    // **********************
+    // Helpers
+    // **********************
+
+    public static <S extends ServiceCallServer> 
CachingServiceCallServiceListStrategy<S> wrap(ServiceCallServerListStrategy<S> 
delegate) {
+        return new CachingServiceCallServiceListStrategy<>(delegate);
+    }
+
+    public static <S extends ServiceCallServer> 
CachingServiceCallServiceListStrategy<S> wrap(ServiceCallServerListStrategy<S> 
delegate, long timeout) {
+        return new CachingServiceCallServiceListStrategy<>(delegate)
+            .timeout(timeout);
+    }
+
+    public static <S extends ServiceCallServer> 
CachingServiceCallServiceListStrategy<S> wrap(ServiceCallServerListStrategy<S> 
delegate, long timeout, TimeUnit unit) {
+        return new CachingServiceCallServiceListStrategy<>(delegate)
+            .timeout(timeout, unit);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/181e8f8b/camel-core/src/test/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategyTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategyTest.java
 
b/camel-core/src/test/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategyTest.java
new file mode 100644
index 0000000..0194c67
--- /dev/null
+++ 
b/camel-core/src/test/java/org/apache/camel/impl/remote/CachingServiceCallServiceListStrategyTest.java
@@ -0,0 +1,56 @@
+/**
+ * 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.camel.impl.remote;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CachingServiceCallServiceListStrategyTest {
+
+    @Test
+    public void testCachingServiceList() throws Exception {
+        MyStrategy strategy = new MyStrategy();
+        CachingServiceCallServiceListStrategy<DefaultServiceCallServer> 
caching = CachingServiceCallServiceListStrategy.wrap(strategy, 1, 
TimeUnit.SECONDS);
+
+        strategy.addServer(new DefaultServiceCallServer("localhost", 1111));
+        Assert.assertEquals(1, 
caching.getUpdatedListOfServers("noname").size());
+        strategy.addServer(new DefaultServiceCallServer("localhost", 1112));
+        Assert.assertEquals(1, 
caching.getUpdatedListOfServers("noname").size());
+
+        // Let the cache expire
+        Thread.sleep(1100);
+
+        Assert.assertEquals(2, 
caching.getUpdatedListOfServers("noname").size());
+    }
+
+    private class MyStrategy extends 
DefaultServiceCallServerListStrategy<DefaultServiceCallServer> {
+        private List<DefaultServiceCallServer> servers = new ArrayList<>();
+
+        @Override
+        public List<DefaultServiceCallServer> getUpdatedListOfServers(String 
name) {
+            return servers;
+        }
+
+        public void addServer(DefaultServiceCallServer server) {
+            servers.add(server);
+        }
+    }
+}

Reply via email to