001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.jmx;
018
019import java.util.List;
020
021import javax.management.openmbean.CompositeDataSupport;
022import javax.management.openmbean.CompositeType;
023import javax.management.openmbean.TabularData;
024import javax.management.openmbean.TabularDataSupport;
025import javax.management.openmbean.TabularType;
026
027import org.apache.activemq.broker.jmx.OpenTypeSupport.OpenTypeFactory;
028import org.apache.activemq.broker.scheduler.Job;
029import org.apache.activemq.broker.scheduler.JobScheduler;
030import org.apache.activemq.broker.scheduler.JobSupport;
031
032/**
033 * MBean object that can be used to manage a single instance of a JobScheduler.  The object
034 * provides methods for querying for jobs and removing some or all of the jobs that are
035 * scheduled in the managed store.
036 */
037public class JobSchedulerView implements JobSchedulerViewMBean {
038
039    private final JobScheduler jobScheduler;
040
041    /**
042     * Creates a new instance of the JobScheduler management MBean.
043     *
044     * @param jobScheduler
045     *        The scheduler instance to manage.
046     */
047    public JobSchedulerView(JobScheduler jobScheduler) {
048        this.jobScheduler = jobScheduler;
049    }
050
051    @Override
052    public TabularData getAllJobs() throws Exception {
053        OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
054        CompositeType ct = factory.getCompositeType();
055        TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
056        TabularDataSupport rc = new TabularDataSupport(tt);
057        List<Job> jobs = this.jobScheduler.getAllJobs();
058        for (Job job : jobs) {
059            rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
060        }
061        return rc;
062    }
063
064    @Override
065    public TabularData getAllJobs(String startTime, String finishTime) throws Exception {
066        OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
067        CompositeType ct = factory.getCompositeType();
068        TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
069        TabularDataSupport rc = new TabularDataSupport(tt);
070        long start = JobSupport.getDataTime(startTime);
071        long finish = JobSupport.getDataTime(finishTime);
072        List<Job> jobs = this.jobScheduler.getAllJobs(start, finish);
073        for (Job job : jobs) {
074            rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
075        }
076        return rc;
077    }
078
079    @Override
080    public TabularData getNextScheduleJobs() throws Exception {
081        OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
082        CompositeType ct = factory.getCompositeType();
083        TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
084        TabularDataSupport rc = new TabularDataSupport(tt);
085        List<Job> jobs = this.jobScheduler.getNextScheduleJobs();
086        for (Job job : jobs) {
087            rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
088        }
089        return rc;
090    }
091
092    @Override
093    public String getNextScheduleTime() throws Exception {
094        long time = this.jobScheduler.getNextScheduleTime();
095        return JobSupport.getDateTime(time);
096    }
097
098    @Override
099    public void removeAllJobs() throws Exception {
100        this.jobScheduler.removeAllJobs();
101    }
102
103    @Override
104    public void removeAllJobs(String startTime, String finishTime) throws Exception {
105        long start = JobSupport.getDataTime(startTime);
106        long finish = JobSupport.getDataTime(finishTime);
107        this.jobScheduler.removeAllJobs(start, finish);
108    }
109
110    @Override
111    public void removeAllJobsAtScheduledTime(String time) throws Exception {
112        long removeAtTime = JobSupport.getDataTime(time);
113        this.jobScheduler.remove(removeAtTime);
114    }
115
116    @Override
117    public void removeJobAtScheduledTime(String time) throws Exception {
118        removeAllJobsAtScheduledTime(time);
119    }
120
121    @Override
122    public void removeJob(String jobId) throws Exception {
123        this.jobScheduler.remove(jobId);
124    }
125
126    @Override
127    public int getExecutionCount(String jobId) throws Exception {
128        int result = 0;
129
130        List<Job> jobs = this.jobScheduler.getAllJobs();
131        for (Job job : jobs) {
132            if (job.getJobId().equals(jobId)) {
133                result = job.getExecutionCount();
134            }
135        }
136
137        return result;
138    }
139}