karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [karaf] branch karaf-4.1.x updated: [KARAF-5307] Add SchedulerMBean
Date Tue, 28 Nov 2017 17:04:50 GMT
This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch karaf-4.1.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.1.x by this push:
     new 3f71a5e  [KARAF-5307] Add SchedulerMBean
3f71a5e is described below

commit 3f71a5e5ca7698d6b4f9a19eb90409d926b89587
Author: Jean-Baptiste Onofré <jbonofre@apache.org>
AuthorDate: Wed Nov 22 18:43:20 2017 +0100

    [KARAF-5307] Add SchedulerMBean
---
 .../org/apache/karaf/scheduler/SchedulerMBean.java | 30 +++++++
 .../org/apache/karaf/scheduler/core/Activator.java |  4 +
 .../karaf/scheduler/core/SchedulerMBeanImpl.java   | 91 ++++++++++++++++++++++
 3 files changed, 125 insertions(+)

diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/SchedulerMBean.java b/scheduler/src/main/java/org/apache/karaf/scheduler/SchedulerMBean.java
new file mode 100644
index 0000000..9248e64
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/SchedulerMBean.java
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+import javax.management.MBeanException;
+import javax.management.openmbean.TabularData;
+
+public interface SchedulerMBean {
+
+    TabularData getJobs() throws MBeanException;
+
+    void trigger(String name, boolean background) throws MBeanException;
+
+    void unschedule(String name) throws MBeanException;
+
+}
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java b/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java
index f3ef634..8271609 100644
--- a/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/core/Activator.java
@@ -36,6 +36,10 @@ public class Activator extends BaseActivator {
         scheduler = new QuartzScheduler(threadPool);
         whiteboardHandler = new WhiteboardHandler(bundleContext, scheduler);
         register(Scheduler.class, scheduler);
+
+        SchedulerMBeanImpl mBean = new SchedulerMBeanImpl();
+        mBean.setScheduler(scheduler);
+        registerMBean(mBean, "type=scheduler");
     }
 
     @Override
diff --git a/scheduler/src/main/java/org/apache/karaf/scheduler/core/SchedulerMBeanImpl.java
b/scheduler/src/main/java/org/apache/karaf/scheduler/core/SchedulerMBeanImpl.java
new file mode 100644
index 0000000..b2081d2
--- /dev/null
+++ b/scheduler/src/main/java/org/apache/karaf/scheduler/core/SchedulerMBeanImpl.java
@@ -0,0 +1,91 @@
+/*
+ * 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.apache.karaf.scheduler.ScheduleOptions;
+import org.apache.karaf.scheduler.Scheduler;
+import org.apache.karaf.scheduler.SchedulerMBean;
+import org.apache.karaf.scheduler.command.support.TriggerJob;
+
+import javax.management.MBeanException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+import javax.management.openmbean.*;
+import java.util.Map;
+
+public class SchedulerMBeanImpl extends StandardMBean implements SchedulerMBean {
+
+    private Scheduler scheduler;
+
+    public SchedulerMBeanImpl() throws NotCompliantMBeanException {
+        super(SchedulerMBean.class);
+    }
+
+    @Override
+    public TabularData getJobs() throws MBeanException {
+        try {
+            CompositeType jobType = new CompositeType("Job", "Scheduler job",
+                    new String[]{ "Job", "Schedule" },
+                    new String[]{ "Job Name", "Job Scheduling" },
+                    new OpenType[]{ SimpleType.STRING, SimpleType.STRING });
+            TabularType tableType = new TabularType("Jobs", "Tables of all jobs", jobType,
new String[]{ "Job" });
+            TabularData table = new TabularDataSupport(tableType);
+
+            Map<Object, ScheduleOptions> jobs = scheduler.getJobs();
+            for (Map.Entry<Object, ScheduleOptions> entry : jobs.entrySet()) {
+                CompositeData data = new CompositeDataSupport(jobType,
+                        new String[]{ "Job", "Schedule" },
+                        new Object[]{ entry.getValue().name(), entry.getValue().schedule()});
+                table.put(data);
+            }
+            return table;
+        } catch (Exception e) {
+            throw new MBeanException(null, e.toString());
+        }
+    }
+
+    @Override
+    public void trigger(String name, boolean background) throws MBeanException {
+        try {
+            if (background) {
+                scheduler.schedule(new TriggerJob(scheduler, name), scheduler.NOW());
+            } else {
+                scheduler.trigger(name);
+            }
+        } catch (Exception e) {
+            throw new MBeanException(null, e.toString());
+        }
+    }
+
+    @Override
+    public void unschedule(String name) throws MBeanException {
+        try {
+            scheduler.unschedule(name);
+        } catch (Exception e) {
+            throw new MBeanException(null, e.toString());
+        }
+    }
+
+    public Scheduler getScheduler() {
+        return scheduler;
+    }
+
+    public void setScheduler(Scheduler scheduler) {
+        this.scheduler = scheduler;
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@karaf.apache.org" <commits@karaf.apache.org>'].

Mime
View raw message