You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/09/20 15:01:21 UTC

svn commit: r1173127 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/TimerListener.java main/java/org/apache/camel/support/TimerListenerManager.java test/java/org/apache/camel/support/TimerListenerManagerTest.java

Author: davsclaus
Date: Tue Sep 20 13:01:20 2011
New Revision: 1173127

URL: http://svn.apache.org/viewvc?rev=1173127&view=rev
Log:
CAMEL-4469: Added TimerListener and TimerListenerManager to make it easier to register scheduled background tasks

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/TimerListener.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/TimerListener.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/TimerListener.java?rev=1173127&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/TimerListener.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/TimerListener.java Tue Sep 20 13:01:20 2011
@@ -0,0 +1,33 @@
+/**
+ * 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;
+
+/**
+ * Listener for receiving timer events.
+ * <p/>
+ * For example to periodically to update internal state.
+ *
+ * @see org.apache.camel.support.TimerListenerManager
+ */
+public interface TimerListener {
+
+    /**
+     * Notification invoked.
+     */
+    void onTimer();
+    
+}

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java?rev=1173127&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/support/TimerListenerManager.java Tue Sep 20 13:01:20 2011
@@ -0,0 +1,118 @@
+/**
+ * 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.support;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.TimerListener;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link TimerListener} manager which triggers the
+ * {@link org.apache.camel.TimerListener} listeners once every second.
+ * <p/>
+ * The {@link #setExecutorService(java.util.concurrent.ScheduledExecutorService)} method
+ * must be invoked prior to starting this manager using the {@link #start()} method.
+ *
+ * @see TimerListener
+ */
+public class TimerListenerManager extends ServiceSupport implements Runnable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(TimerListenerManager.class);
+    private final Set<TimerListener> listeners = new LinkedHashSet<TimerListener>();
+    private ScheduledExecutorService executorService;
+    private volatile ScheduledFuture task;
+    private long interval = 1000L;
+
+    public TimerListenerManager() {
+    }
+
+    public void setExecutorService(ScheduledExecutorService executorService) {
+        this.executorService = executorService;
+    }
+
+    /**
+     * Gets the interval in millis.
+     * <p/>
+     * The default interval is 1000 millis.
+     *
+     * @return interval in millis.
+     */
+    public long getInterval() {
+        return interval;
+    }
+
+    /**
+     * Sets the interval in millis.
+     *
+     * @param interval interval in millis.
+     */
+    public void setInterval(long interval) {
+        this.interval = interval;
+    }
+
+    @Override
+    public void run() {
+        LOG.trace("Running scheduled TimerListener task");
+
+        if (!isRunAllowed()) {
+            LOG.debug("TimerListener task cannot run as its not allowed");
+            return;
+        }
+
+        for (TimerListener listener : listeners) {
+            try {
+                LOG.trace("Invoking onTimer on {}", listener);
+                listener.onTimer();
+            } catch (Throwable e) {
+                // ignore
+                LOG.debug("Error occurred during onTimer for TimerListener: " + listener + ". This exception will be ignored.", e);
+            }
+        }
+    }
+
+    public void addTimerListener(TimerListener listener) {
+        listeners.add(listener);
+        LOG.debug("Added TimerListener: {}", listener);
+    }
+
+    public void removeTimerListener(TimerListener listener) {
+        listeners.remove(listener);
+        LOG.debug("Removed TimerListener: {}", listener);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        ObjectHelper.notNull(executorService, "executorService", this);
+        task = executorService.scheduleAtFixedRate(this, 1000L, interval, TimeUnit.MILLISECONDS);
+        LOG.debug("Started scheduled TimerListener task to run with interval {} ms", interval);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        // executor service will be shutdown by CamelContext
+        task.cancel(true);
+    }
+
+}
+

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java?rev=1173127&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/support/TimerListenerManagerTest.java Tue Sep 20 13:01:20 2011
@@ -0,0 +1,61 @@
+/**
+ * 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.support;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+import org.apache.camel.TimerListener;
+
+/**
+ *
+ */
+public class TimerListenerManagerTest extends TestCase {
+
+    private final MyTask task = new MyTask();
+
+    public void testTimerListenerManager() throws Exception {
+        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
+        TimerListenerManager manager = new TimerListenerManager();
+        manager.setExecutorService(executor);
+        manager.addTimerListener(task);
+        manager.start();
+
+        assertTrue("Should be invoked", task.await());
+
+        manager.stop();
+        manager.removeTimerListener(task);
+        executor.shutdown();
+    }
+
+    private class MyTask implements TimerListener {
+
+        private CountDownLatch latch = new CountDownLatch(1);
+
+        @Override
+        public void onTimer() {
+            latch.countDown();
+        }
+
+        public boolean await() throws InterruptedException {
+            return latch.await(5, TimeUnit.SECONDS);
+        }
+    }
+}