activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rajdav...@apache.org
Subject svn commit: r521788 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo: PojoComponent.java PollingPojoComponent.java PollingPojoEndpoint.java PollingSchedule.java
Date Fri, 23 Mar 2007 16:12:17 GMT
Author: rajdavies
Date: Fri Mar 23 09:12:16 2007
New Revision: 521788

URL: http://svn.apache.org/viewvc?view=rev&rev=521788
Log:
Add a polling component 

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoEndpoint.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoComponent.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoComponent.java?view=diff&rev=521788&r1=521787&r2=521788
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoComponent.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoComponent.java
Fri Mar 23 09:12:16 2007
@@ -29,8 +29,8 @@
  */
 public class PojoComponent implements Component<PojoExchange> {
 	
-    private final HashMap<String, Object> registry = new HashMap<String, Object>();
-    private final HashMap<String, PojoEndpoint> activatedEndpoints = new HashMap<String,
PojoEndpoint>();
+    protected final HashMap<String, Object> registry = new HashMap<String, Object>();
+    protected final HashMap<String, PojoEndpoint> activatedEndpoints = new HashMap<String,
PojoEndpoint>();
     
 	private CamelContext container;
 
@@ -40,6 +40,11 @@
 	public Object lookupRegisteredPojo(String uri) {
 		return registry.get(uri);
 	}
+    
+    public void unregisterPojo(String uri) {
+        registry.remove(uri);
+        unregisterActivation(uri);
+    }
 	
 	public void registerActivation(String uri, PojoEndpoint endpoint) {
 		activatedEndpoints.put(uri, endpoint);

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java?view=auto&rev=521788
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java
Fri Mar 23 09:12:16 2007
@@ -0,0 +1,202 @@
+/*
+ * 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.component.pojo;
+
+import java.util.Date;
+import java.util.HashMap;
+
+/**
+* Represents the polling component that manages {@link PojoEndpoint}.  It holds the 
+* list of named pojos that queue endpoints reference.
+*
+* @version $Revision: 519973 $
+*/
+public class PollingPojoComponent extends PojoComponent{
+
+    
+       
+       protected final HashMap<String,PollingSchedule> schedules=new HashMap<String,PollingSchedule>();
+
+    /**
+     * Registers a pojo and schedules the pojo for execution at the specified time. If the
time is in the past, the task
+     * is scheduled for immediate execution.
+     * 
+     * @param uri
+     * @param pojo
+     * @param time time at which task is to be executed.
+     * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
+     * @throws IllegalStateException if task was already scheduled or cancelled, timer was
cancelled, or timer thread
+     *             terminated.
+     */
+    public void registerAndSchedulePojo(String uri,Object pojo,Date time){
+        super.registerPojo(uri,pojo);
+        
+        PollingSchedule schedule = new PollingSchedule(uri,time);
+        schedules.put(uri,schedule);
+    }
+    
+    /**
+     * Registers a pojo and cchedules the pojo for execution at the specified time. If the
time is in the past, the task
+     * is scheduled for immediate execution.
+     * 
+     * @param uri
+     * @param pojo
+     * @param firstTime 
+     * @param period 
+     * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
+     * @throws IllegalStateException if task was already scheduled or cancelled, timer was
cancelled, or timer thread
+     *             terminated.
+     */
+    
+    public void registerAndScheulePojo(String uri,Object pojo,Date firstTime,long period){
+        super.registerPojo(uri,pojo);
+        PollingSchedule schedule=new PollingSchedule(uri,firstTime,period);
+        schedules.put(uri,schedule);
+    }
+    
+    /**
+     * Registers a pojo and schedules the specified task for repeated <i>fixed-delay
execution</i>,
+     * beginning after the specified delay.  Subsequent executions take place
+     * at approximately regular intervals separated by the specified period.
+     *
+     * <p>In fixed-delay execution, each execution is scheduled relative to
+     * the actual execution time of the previous execution.  If an execution
+     * is delayed for any reason (such as garbage collection or other
+     * background activity), subsequent executions will be delayed as well.
+     * In the long run, the frequency of execution will generally be slightly
+     * lower than the reciprocal of the specified period (assuming the system
+     * clock underlying <tt>Object.wait(long)</tt> is accurate).
+     *
+     * <p>Fixed-delay execution is appropriate for recurring activities
+     * that require "smoothness."  In other words, it is appropriate for
+     * activities where it is more important to keep the frequency accurate
+     * in the short run than in the long run.  This includes most animation
+     * tasks, such as blinking a cursor at regular intervals.  It also includes
+     * tasks wherein regular activity is performed in response to human
+     * input, such as automatically repeating a character as long as a key
+     * is held down.
+     * 
+     * @param uri 
+     * @param pojo 
+     * @param delay  delay in milliseconds before task is to be executed.
+     * @param period time in milliseconds between successive task executions.
+     * @throws IllegalArgumentException if <tt>delay</tt> is negative, or
+     *         <tt>delay + System.currentTimeMillis()</tt> is negative.
+     * @throws IllegalStateException if task was already scheduled or
+     *         cancelled, timer was cancelled, or timer thread terminated.
+     */
+    
+    public void registerAndScheulePojo(String uri,Object pojo,long delay,long period){
+        super.registerPojo(uri,pojo);
+        PollingSchedule schedule=new PollingSchedule(uri,delay,period);
+        schedules.put(uri,schedule);
+    }
+    
+    
+    /**
+     * Register pojo and schedules the specified task for repeated <i>fixed-rate execution</i>,
+     * beginning at the specified time. Subsequent executions take place at
+     * approximately regular intervals, separated by the specified period.
+     *
+     * <p>In fixed-rate execution, each execution is scheduled relative to the
+     * scheduled execution time of the initial execution.  If an execution is
+     * delayed for any reason (such as garbage collection or other background
+     * activity), two or more executions will occur in rapid succession to
+     * "catch up."  In the long run, the frequency of execution will be
+     * exactly the reciprocal of the specified period (assuming the system
+     * clock underlying <tt>Object.wait(long)</tt> is accurate).
+     *
+     * <p>Fixed-rate execution is appropriate for recurring activities that
+     * are sensitive to <i>absolute</i> time, such as ringing a chime every
+     * hour on the hour, or running scheduled maintenance every day at a
+     * particular time.  It is also appropriate for recurring activities
+     * where the total time to perform a fixed number of executions is
+     * important, such as a countdown timer that ticks once every second for
+     * ten seconds.  Finally, fixed-rate execution is appropriate for
+     * scheduling multiple repeating timer tasks that must remain synchronized
+     * with respect to one another.
+     * 
+     * @param uri 
+     * @param pojo 
+     * @param firstTime First time at which task is to be executed.
+     * @param period time in milliseconds between successive task executions.
+     * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
+     * @throws IllegalStateException if task was already scheduled or
+     *         cancelled, timer was cancelled, or timer thread terminated.
+     */
+    
+    public void registerAndScheulePojoAtFixedRate(String uri,Object pojo,Date firstTime,
long period){
+        super.registerPojo(uri,pojo);
+        PollingSchedule schedule=new PollingSchedule(uri,firstTime,-1,period,true);
+        schedules.put(uri,schedule);
+    }
+    
+    /**
+     * Register pojo and schedules the specified task for repeated <i>fixed-rate execution</i>,
+     * beginning after the specified delay.  Subsequent executions take place
+     * at approximately regular intervals, separated by the specified period.
+     *
+     * <p>In fixed-rate execution, each execution is scheduled relative to the
+     * scheduled execution time of the initial execution.  If an execution is
+     * delayed for any reason (such as garbage collection or other background
+     * activity), two or more executions will occur in rapid succession to
+     * "catch up."  In the long run, the frequency of execution will be
+     * exactly the reciprocal of the specified period (assuming the system
+     * clock underlying <tt>Object.wait(long)</tt> is accurate).
+     *
+     * <p>Fixed-rate execution is appropriate for recurring activities that
+     * are sensitive to <i>absolute</i> time, such as ringing a chime every
+     * hour on the hour, or running scheduled maintenance every day at a
+     * particular time.  It is also appropriate for recurring activities
+     * where the total time to perform a fixed number of executions is
+     * important, such as a countdown timer that ticks once every second for
+     * ten seconds.  Finally, fixed-rate execution is appropriate for
+     * scheduling multiple repeating timer tasks that must remain synchronized
+     * with respect to one another.
+     * 
+     * @param uri 
+     * @param pojo 
+     * @param delay  delay in milliseconds before task is to be executed.
+     * @param period time in milliseconds between successive task executions.
+     * @throws IllegalArgumentException if <tt>delay</tt> is negative, or
+     *         <tt>delay + System.currentTimeMillis()</tt> is negative.
+     * @throws IllegalStateException if task was already scheduled or
+     *         cancelled, timer was cancelled, or timer thread terminated.
+     */
+    
+    public void registerAndScheulePojoAtFixedRate(String uri,Object pojo,long delay, long
period){
+        super.registerPojo(uri,pojo);
+        PollingSchedule schedule=new PollingSchedule(uri,null,delay,period,true);
+        schedules.put(uri,schedule);
+    }
+    
+    
+    public void registerActivation(String uri, PojoEndpoint endpoint) {
+        super.registerActivation(uri,endpoint);
+        if (endpoint instanceof PollingPojoEndpoint) {
+            PollingSchedule schedule = schedules.get(uri);
+            if(schedule != null) {
+                schedule.activate((Runnable)endpoint);
+            }
+        }
+    }
+    
+    public void unregisterActivation(String uri) {
+        super.unregisterActivation(uri);
+        PollingSchedule schedule = schedules.remove(uri);
+        if(schedule != null) {
+            schedule.deactivate();
+        }
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoEndpoint.java?view=auto&rev=521788
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoEndpoint.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoEndpoint.java
Fri Mar 23 09:12:16 2007
@@ -0,0 +1,32 @@
+/*
+ * 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.component.pojo;
+
+import org.apache.camel.CamelContext;
+
+/**
+ * A polling pojo endpoint - the run() method will be called periodically
+ *
+ * @version $Revision: 519973 $
+ */
+public abstract class PollingPojoEndpoint extends PojoEndpoint implements Runnable{
+
+    public PollingPojoEndpoint(String uri,CamelContext container,PojoComponent component,Object
pojo){
+        super(uri,container,component,pojo);
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java?view=auto&rev=521788
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java
Fri Mar 23 09:12:16 2007
@@ -0,0 +1,103 @@
+/*
+ * 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.component.pojo;
+
+import java.util.Date;
+import java.util.Timer;
+import java.util.TimerTask;
+
+
+/**
+ * Schedule used for polling an endpoint
+ *
+ */
+class PollingSchedule{
+
+    private String name;
+    private Timer timer;
+    private Date time;
+    private long period=-1;
+    private long delay=-1;
+    private boolean fixedRate;
+    private boolean daemon=true;
+
+    PollingSchedule(String name,Date time){
+        this(name,time,-1,-1,false);
+    }
+
+    PollingSchedule(String name,Date time,long period){
+        this(name,time,-1,period,false);
+    }
+
+    PollingSchedule(String name,long delay){
+        this(name,null,delay,-1,false);
+    }
+    
+    PollingSchedule(String name,long delay,long period){
+        this(name,null,delay,period,false);
+    }
+
+    PollingSchedule(String name,Date time,long delay,long period,boolean fixedRate){
+        this.name=name;
+        this.time=time;
+        this.delay=delay;
+        this.period=period;
+        this.fixedRate=fixedRate;
+    }
+
+    void activate(Runnable run){
+        timer=createTimerAndTask(name,run);
+    }
+
+    void deactivate(){
+        if(timer!=null){
+            timer.cancel();
+        }
+    }
+
+    private Timer createTimerAndTask(String name,final Runnable run){
+        TimerTask task=new TimerTask(){
+
+            @Override public void run(){
+                run.run();
+            }
+        };
+        Timer result=new Timer(name,daemon);
+        if(fixedRate){
+            if(time!=null){
+                result.scheduleAtFixedRate(task,time,period);
+            }else{
+                result.scheduleAtFixedRate(task,delay,period);
+            }
+        }else{
+            if(time!=null){
+                if(period>=0){
+                    result.schedule(task,time,period);
+                }else{
+                    result.schedule(task,time);
+                }
+            }else{
+                if(period>=0){
+                    result.schedule(task,delay,period);
+                }else{
+                    result.schedule(task,delay);
+                }
+            }
+        }
+        return result;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message