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 2008/08/21 15:20:57 UTC

svn commit: r687746 - in /servicemix/smx3/trunk: ./ core/servicemix-services/src/main/java/org/apache/servicemix/store/ core/servicemix-services/src/main/java/org/apache/servicemix/store/memory/ core/servicemix-services/src/test/java/org/apache/service...

Author: gertv
Date: Thu Aug 21 06:20:53 2008
New Revision: 687746

URL: http://svn.apache.org/viewvc?rev=687746&view=rev
Log:
SM-1518: Adding a timeout-capable MemoryStore implementation

Added:
    servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/TimeoutMemoryStore.java
    servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/
    servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/MemoryStoreFactoryTest.java
    servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/TimeoutMemoryStoreTest.java
Modified:
    servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java
    servicemix/smx3/trunk/pom.xml

Added: servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/TimeoutMemoryStore.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/TimeoutMemoryStore.java?rev=687746&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/TimeoutMemoryStore.java (added)
+++ servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/TimeoutMemoryStore.java Thu Aug 21 06:20:53 2008
@@ -0,0 +1,85 @@
+/*
+ * 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.store.memory;
+
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.id.IdGenerator;
+
+/**
+ * {@link MemoryStore} which removes entries from the store after the specified timeout
+ * to free memory.
+ */
+public class TimeoutMemoryStore extends MemoryStore {
+
+    private static final Log LOG = LogFactory.getLog(TimeoutMemoryStore.class);
+    private ConcurrentMap<String, Entry> datas = new ConcurrentHashMap<String, Entry>();
+    private final long timeout;
+
+    protected TimeoutMemoryStore(IdGenerator idGenerator, long timeout) {
+        super(idGenerator);
+        this.timeout = timeout;
+    }
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void store(String id, Object data) throws IOException {
+        LOG.debug("Storing object with id: " + id);
+        datas.put(id, new Entry(data));
+    }
+    
+    /**
+     * {@inheritDoc}
+     * 
+     * Before attempting to load the object, all data older than the specified timeout will first be 
+     * removed from the store.
+     */
+    public Object load(String id) throws IOException {
+        evict();
+        LOG.debug("Loading object with id:" + id);
+        Entry entry = datas.remove(id);
+        return entry == null ? null : entry.data;
+    }
+    
+    private void evict() {
+        long now = System.currentTimeMillis();
+        for (String key : datas.keySet()) {
+            long age = now - datas.get(key).time;
+            if (age > timeout) {
+                LOG.debug("Removing object with id " + key + " from store after " + age + " ms");
+                datas.remove(key);
+            }
+        }
+    }
+
+    /*
+     * A single store entry
+     */
+    private final class Entry {
+        private final long time = System.currentTimeMillis();
+        private final Object data;
+        
+        private Entry(Object data) {
+            this.data = data;
+        }
+    }
+}

Modified: servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java?rev=687746&r1=687745&r2=687746&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java (original)
+++ servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/store/memory/MemoryStoreFactory.java Thu Aug 21 06:20:53 2008
@@ -24,10 +24,17 @@
 import org.apache.servicemix.store.Store;
 import org.apache.servicemix.store.StoreFactory;
 
+/**
+ * {@link StoreFactory} for creating memory-based {@link Store} implementations
+ * 
+ * If a timeout has been specified, a {@link TimeoutMemoryStore} will be created,
+ * otherwise the factory will build a plain {@link MemoryStore}
+ */
 public class MemoryStoreFactory implements StoreFactory {
 
     private IdGenerator idGenerator = new IdGenerator();
     private Map<String, MemoryStore> stores = new HashMap<String, MemoryStore>();
+    private long timeout = -1;
     
     /* (non-Javadoc)
      * @see org.apache.servicemix.store.ExchangeStoreFactory#get(java.lang.String)
@@ -35,7 +42,11 @@
     public synchronized Store open(String name) throws IOException {
         MemoryStore store = stores.get(name);
         if (store == null) {
-            store = new MemoryStore(idGenerator);
+            if (timeout <= 0) {
+                store = new MemoryStore(idGenerator);
+            } else {
+                store = new TimeoutMemoryStore(idGenerator, timeout);
+            }
             stores.put(name, store);
         }
         return store;
@@ -48,4 +59,7 @@
         stores.remove(store);
     }
     
+    public void setTimeout(long timeout) {
+        this.timeout = timeout;
+    }
 }

Added: servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/MemoryStoreFactoryTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/MemoryStoreFactoryTest.java?rev=687746&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/MemoryStoreFactoryTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/MemoryStoreFactoryTest.java Thu Aug 21 06:20:53 2008
@@ -0,0 +1,41 @@
+/*
+ * 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.store.memory;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link MemoryStoreFactory}
+ */
+public class MemoryStoreFactoryTest extends TestCase {
+    
+    private MemoryStoreFactory factory;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        factory = new MemoryStoreFactory();
+    }
+    
+    public void testOpen() throws Exception {
+        assertTrue(factory.open("store1") instanceof MemoryStore);
+        factory.setTimeout(500);
+        assertTrue(factory.open("store1") instanceof MemoryStore);
+        assertTrue(factory.open("store2") instanceof TimeoutMemoryStore);
+    }
+
+}

Added: servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/TimeoutMemoryStoreTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/TimeoutMemoryStoreTest.java?rev=687746&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/TimeoutMemoryStoreTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/store/memory/TimeoutMemoryStoreTest.java Thu Aug 21 06:20:53 2008
@@ -0,0 +1,62 @@
+/*
+ * 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.store.memory;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.store.Store;
+
+/**
+ * Test case for {@link TimeoutMemoryStore} 
+ */
+public class TimeoutMemoryStoreTest extends TestCase {
+    
+    private static final long TIMEOUT = 250L; 
+    
+    private Store store;
+    private final MemoryStoreFactory factory = new MemoryStoreFactory();
+    
+    public TimeoutMemoryStoreTest() {
+        super();
+        factory.setTimeout(TIMEOUT);
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        store = factory.open("test");
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        factory.close(store);
+    }
+    
+    public void testTimeout() throws Exception {
+        String id = store.store("Any kind of data...");
+        Object data = store.load(id);
+        assertNotNull(data);
+        //now store it again and load it after the timeout
+        store.store(id, data);
+        synchronized (this) {
+            wait(TIMEOUT * 2);
+        }
+        assertNull("Data should have been removed from store after timeout", store.load(id));
+    }
+
+}

Modified: servicemix/smx3/trunk/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/pom.xml?rev=687746&r1=687745&r2=687746&view=diff
==============================================================================
--- servicemix/smx3/trunk/pom.xml (original)
+++ servicemix/smx3/trunk/pom.xml Thu Aug 21 06:20:53 2008
@@ -1845,7 +1845,7 @@
                 <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-surefire-plugin</artifactId>
-                    <version>2.4.3</version>
+                    <version>2.4.2</version>
                     <configuration>
                         <useFile>true</useFile>
                         <forkMode>once</forkMode>