You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by io...@apache.org on 2011/07/20 14:28:19 UTC
svn commit: r1148723 - in /karaf/trunk: ./
assemblies/features/standard/src/main/feature/ scheduler/ scheduler/core/
scheduler/core/src/ scheduler/core/src/main/ scheduler/core/src/main/java/
scheduler/core/src/main/java/org/ scheduler/core/src/main/ja...
Author: iocanel
Date: Wed Jul 20 12:28:13 2011
New Revision: 1148723
URL: http://svn.apache.org/viewvc?rev=1148723&view=rev
Log:
[KARAF-751] Added a scheduler which allows scheduling of Runnable services via java Timer.
Added:
karaf/trunk/scheduler/
karaf/trunk/scheduler/core/
karaf/trunk/scheduler/core/pom.xml
karaf/trunk/scheduler/core/src/
karaf/trunk/scheduler/core/src/main/
karaf/trunk/scheduler/core/src/main/java/
karaf/trunk/scheduler/core/src/main/java/org/
karaf/trunk/scheduler/core/src/main/java/org/apache/
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/Activator.java
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/KarafTimerTask.java
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/RunableServiceListener.java
karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/TaskScheduler.java
karaf/trunk/scheduler/core/src/main/resources/
karaf/trunk/scheduler/core/src/main/resources/OSGI-INF/
karaf/trunk/scheduler/core/src/main/resources/OSGI-INF/bundle.info
karaf/trunk/scheduler/core/src/test/
karaf/trunk/scheduler/core/src/test/java/
karaf/trunk/scheduler/pom.xml
Modified:
karaf/trunk/assemblies/features/standard/src/main/feature/feature.xml
karaf/trunk/pom.xml
Modified: karaf/trunk/assemblies/features/standard/src/main/feature/feature.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/assemblies/features/standard/src/main/feature/feature.xml?rev=1148723&r1=1148722&r2=1148723&view=diff
==============================================================================
--- karaf/trunk/assemblies/features/standard/src/main/feature/feature.xml (original)
+++ karaf/trunk/assemblies/features/standard/src/main/feature/feature.xml Wed Jul 20 12:28:13 2011
@@ -146,6 +146,9 @@
<bundle>mvn:org.apache.aries.jmx/org.apache.aries.jmx/${aries.jmx.version}</bundle>
<bundle>mvn:org.apache.aries.jmx/org.apache.aries.jmx.blueprint/${aries.jmx.version}</bundle>
</feature>
+ <feature name="scheduler" version="${project.version}" start-level='30'>
+ <bundle>mvn:org.apache.karaf.scheduler/org.apache.karaf.scheduler.core/${project.version}</bundle>
+ </feature>
<feature name="eventadmin" version="${project.version}" start-level='30'>
<bundle start-level='30'>mvn:org.apache.felix/org.apache.felix.eventadmin/${felix.eventadmin.version}</bundle>
</feature>
Modified: karaf/trunk/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/pom.xml?rev=1148723&r1=1148722&r2=1148723&view=diff
==============================================================================
--- karaf/trunk/pom.xml (original)
+++ karaf/trunk/pom.xml Wed Jul 20 12:28:13 2011
@@ -46,6 +46,7 @@
<module>management</module>
<module>webconsole</module>
<module>exception</module>
+ <module>scheduler</module>
<module>assemblies</module>
<module>demos</module>
<module>itests</module>
Added: karaf/trunk/scheduler/core/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/core/pom.xml?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/core/pom.xml (added)
+++ karaf/trunk/scheduler/core/pom.xml Wed Jul 20 12:28:13 2011
@@ -0,0 +1,83 @@
+<?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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>scheduler</artifactId>
+ <groupId>org.apache.karaf</groupId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.karaf.scheduler</groupId>
+ <artifactId>org.apache.karaf.scheduler.core</artifactId>
+ <packaging>bundle</packaging>
+ <name>Apache Karaf :: Scheduler :: Core</name>
+ <description>This bundle provides a Service Listener which listnes for Runnable Services and schedules their execution, based on the service properties</description>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>${project.basedir}/src/main/resources</directory>
+ <includes>
+ <include>**/*</include>
+ </includes>
+ </resource>
+ <resource>
+ <directory>${project.basedir}/src/main/resources</directory>
+ <filtering>true</filtering>
+ <includes>
+ <include>**/*.info</include>
+ </includes>
+ </resource>
+ </resources>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <configuration>
+ <instructions>
+ <Bundle-Activator>org.apache.karaf.scheduler.core.Activator</Bundle-Activator>
+ <Import-Package>
+ !${project.artifactId}*,
+ *
+ </Import-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
\ No newline at end of file
Added: karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/Activator.java
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/Activator.java?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/Activator.java (added)
+++ karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/Activator.java Wed Jul 20 12:28:13 2011
@@ -0,0 +1,51 @@
+/*
+ * 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.karaf.scheduler.core;
+
+import org.osgi.framework.*;
+
+import java.util.Properties;
+
+/**
+ * @author: iocanel
+ */
+public class Activator implements BundleActivator {
+
+ private TaskScheduler scheduler = new TaskScheduler();
+ private RunableServiceListener listener;
+
+ private static final String filter = "(&(objectclass=%s)(&(%s >= 0)(%s >= 0)))";
+
+
+ @Override
+ public void start(BundleContext context) throws Exception {
+ listener = new RunableServiceListener(context, scheduler);
+
+ //register scheduler service
+ context.registerService(scheduler.getClass().getName(), scheduler, new Properties());
+
+ //register service listener
+ context.addServiceListener(listener, String.format(filter, Runnable.class.getName(), KarafTimerTask.ID_PROPERTY, KarafTimerTask.PERIOD_PROPERTY));
+
+ }
+
+ @Override
+ public void stop(BundleContext context) throws Exception {
+
+ }
+}
Added: karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/KarafTimerTask.java
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/KarafTimerTask.java?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/KarafTimerTask.java (added)
+++ karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/KarafTimerTask.java Wed Jul 20 12:28:13 2011
@@ -0,0 +1,84 @@
+/*
+ * 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.karaf.scheduler.core;
+
+import java.util.TimerTask;
+
+/**
+ * @author: iocanel
+ */
+public class KarafTimerTask<R extends Runnable> extends TimerTask {
+
+ public static final String ID_PROPERTY = "org.apache.karaf.scheduler.task.id";
+ public static final String PERIOD_PROPERTY = "org.apache.karaf.scheduler.task.period";
+
+ protected String id;
+ protected R task;
+ protected Long schedulePeriod = 0L;
+
+ /**
+ * Constructor
+ * @param id the id of the task. Used for reference.
+ * @param task the task to be scheduled
+ */
+ public KarafTimerTask(String id, R task) {
+ this.id = id;
+ this.task = task;
+ }
+
+ /**
+ * Constructor
+ * @param id the id of the task. Used for reference.
+ * @param task the task to be scheduled
+ * @param schedulePeriod the schedule period.
+ */
+ public KarafTimerTask(String id, R task, Long schedulePeriod) {
+ this.id = id;
+ this.task = task;
+ this.schedulePeriod = schedulePeriod;
+ }
+
+ @Override
+ public void run() {
+ task.run();
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public R getTask() {
+ return task;
+ }
+
+ public void setTask(R task) {
+ this.task = task;
+ }
+
+ public Long getSchedulePeriod() {
+ return schedulePeriod;
+ }
+
+ public void setSchedulePeriod(Long schedulePeriod) {
+ this.schedulePeriod = schedulePeriod;
+ }
+}
Added: karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/RunableServiceListener.java
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/RunableServiceListener.java?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/RunableServiceListener.java (added)
+++ karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/RunableServiceListener.java Wed Jul 20 12:28:13 2011
@@ -0,0 +1,87 @@
+/*
+ * 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.karaf.scheduler.core;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * @author: iocanel
+ */
+public class RunableServiceListener implements ServiceListener {
+
+ private final BundleContext bundleContext;
+ private final TaskScheduler scheduler;
+
+ /**
+ * Constructor
+ *
+ * @param bundleContext
+ * @param scheduler
+ */
+ public RunableServiceListener(BundleContext bundleContext, TaskScheduler scheduler) {
+ this.bundleContext = bundleContext;
+ this.scheduler = scheduler;
+ }
+
+ @Override
+ public void serviceChanged(ServiceEvent event) {
+ switch (event.getType()) {
+ case ServiceEvent.REGISTERED:
+ scheduleRunnableService(event);
+ break;
+ case ServiceEvent.UNREGISTERING: {
+ unscheduleRunnableService(event);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ /**
+ * Schedules the execution of the Runnable Service of the {@link ServiceEvent}.
+ *
+ * @param event
+ */
+ protected void scheduleRunnableService(ServiceEvent event) {
+ ServiceReference reference = event.getServiceReference();
+ Runnable service = (Runnable) bundleContext.getService(reference);
+ String id = (String) reference.getProperty(KarafTimerTask.ID_PROPERTY);
+ String periodValue = (String) reference.getProperty(KarafTimerTask.PERIOD_PROPERTY);
+
+ if (periodValue != null) {
+ Long period = Long.parseLong(periodValue);
+ KarafTimerTask task = new KarafTimerTask(id, service, period);
+ scheduler.schedule(task);
+ }
+ }
+
+ /**
+ * Unschedules the execution of the Runnable Service of the {@link ServiceEvent}.
+ *
+ * @param event
+ */
+ protected void unscheduleRunnableService(ServiceEvent event) {
+ ServiceReference reference = event.getServiceReference();
+ String id = (String) reference.getProperty(KarafTimerTask.ID_PROPERTY);
+ scheduler.unschedule(id);
+ }
+}
Added: karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/TaskScheduler.java
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/TaskScheduler.java?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/TaskScheduler.java (added)
+++ karaf/trunk/scheduler/core/src/main/java/org/apache/karaf/scheduler/core/TaskScheduler.java Wed Jul 20 12:28:13 2011
@@ -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.karaf.scheduler.core;
+
+import java.util.Map;
+import java.util.Timer;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author: iocanel
+ */
+public class TaskScheduler {
+
+ private final Timer timer = new Timer();
+ private final Map<String,KarafTimerTask> tasks = new ConcurrentHashMap<String, KarafTimerTask>();
+
+ /**
+ * Schedule a {@link KarafTimerTask}
+ * @param task
+ */
+ public void schedule(KarafTimerTask task) {
+ if(task != null) {
+ tasks.put(task.getId(),task);
+ timer.schedule(task,0,task.getSchedulePeriod());
+ }
+
+ }
+
+ /**
+ * Unschedule a {@link KarafTimerTask}
+ * @param id The id of the task to unschedule.
+ */
+ public void unschedule(String id) {
+ if(id != null) {
+ KarafTimerTask task = tasks.remove(id);
+ if(task != null) {
+ task.cancel();
+ }
+ }
+ }
+}
Added: karaf/trunk/scheduler/core/src/main/resources/OSGI-INF/bundle.info
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/core/src/main/resources/OSGI-INF/bundle.info?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/core/src/main/resources/OSGI-INF/bundle.info (added)
+++ karaf/trunk/scheduler/core/src/main/resources/OSGI-INF/bundle.info Wed Jul 20 12:28:13 2011
@@ -0,0 +1,20 @@
+h1. Synopsis
+
+${project.name}
+
+${project.description}
+
+Maven URL:
+[mvn:${project.groupId}/${project.artifactId}/${project.version}]
+
+h1. Description
+
+This bundle register a service listener, which listens from service events related to java.lang.Runnable interface.
+Each serivce with such interface may be added the following properties:
+
+org.apache.karaf.scheduler.task.id
+org.apache.karaf.scheduler.task.period
+
+If both properties are found, then the Runnable is decorated by a TimerTask and scheduled for repeated execution using the period property.
+
+
Added: karaf/trunk/scheduler/pom.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/scheduler/pom.xml?rev=1148723&view=auto
==============================================================================
--- karaf/trunk/scheduler/pom.xml (added)
+++ karaf/trunk/scheduler/pom.xml Wed Jul 20 12:28:13 2011
@@ -0,0 +1,38 @@
+<?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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>karaf</artifactId>
+ <groupId>org.apache.karaf</groupId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.karaf</groupId>
+ <artifactId>scheduler</artifactId>
+ <name>Apache Karaf :: Scheduler</name>
+ <packaging>pom</packaging>
+
+ <modules>
+ <module>core</module>
+ </modules>
+
+
+</project>
\ No newline at end of file