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.Message;
028import org.apache.activemq.ScheduledMessage;
029import org.apache.activemq.broker.jmx.OpenTypeSupport.OpenTypeFactory;
030import org.apache.activemq.broker.scheduler.Job;
031import org.apache.activemq.broker.scheduler.JobScheduler;
032import org.apache.activemq.broker.scheduler.JobSupport;
033import org.apache.activemq.openwire.OpenWireFormat;
034import org.apache.activemq.util.ByteSequence;
035
036/**
037 * MBean object that can be used to manage a single instance of a JobScheduler.  The object
038 * provides methods for querying for jobs and removing some or all of the jobs that are
039 * scheduled in the managed store.
040 */
041public class JobSchedulerView implements JobSchedulerViewMBean {
042
043    private final JobScheduler jobScheduler;
044
045    /**
046     * Creates a new instance of the JobScheduler management MBean.
047     *
048     * @param jobScheduler
049     *        The scheduler instance to manage.
050     */
051    public JobSchedulerView(JobScheduler jobScheduler) {
052        this.jobScheduler = jobScheduler;
053    }
054
055    @Override
056    public TabularData getAllJobs() throws Exception {
057        OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
058        CompositeType ct = factory.getCompositeType();
059        TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
060        TabularDataSupport rc = new TabularDataSupport(tt);
061        List<Job> jobs = this.jobScheduler.getAllJobs();
062        for (Job job : jobs) {
063            rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
064        }
065        return rc;
066    }
067
068    @Override
069    public TabularData getAllJobs(String startTime, String finishTime) throws Exception {
070        OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
071        CompositeType ct = factory.getCompositeType();
072        TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
073        TabularDataSupport rc = new TabularDataSupport(tt);
074        long start = JobSupport.getDataTime(startTime);
075        long finish = JobSupport.getDataTime(finishTime);
076        List<Job> jobs = this.jobScheduler.getAllJobs(start, finish);
077        for (Job job : jobs) {
078            rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
079        }
080        return rc;
081    }
082
083    @Override
084    public int getDelayedMessageCount() throws Exception {
085        int counter = 0;
086        OpenWireFormat wireFormat = new OpenWireFormat();
087        for (Job job : jobScheduler.getAllJobs()) {
088            Message msg = (Message) wireFormat.unmarshal(new ByteSequence(job.getPayload()));
089            if (msg.getLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY) > 0) {
090                counter++;
091            }
092        }
093        return counter;
094    }
095
096    @Override
097    public int getScheduledMessageCount() throws Exception {
098        return this.jobScheduler.getAllJobs().size();
099    }
100
101    @Override
102    public TabularData getNextScheduleJobs() throws Exception {
103        OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
104        CompositeType ct = factory.getCompositeType();
105        TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
106        TabularDataSupport rc = new TabularDataSupport(tt);
107        List<Job> jobs = this.jobScheduler.getNextScheduleJobs();
108        for (Job job : jobs) {
109            rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
110        }
111        return rc;
112    }
113
114    @Override
115    public String getNextScheduleTime() throws Exception {
116        long time = this.jobScheduler.getNextScheduleTime();
117        return JobSupport.getDateTime(time);
118    }
119
120    @Override
121    public void removeAllJobs() throws Exception {
122        this.jobScheduler.removeAllJobs();
123    }
124
125    @Override
126    public void removeAllJobs(String startTime, String finishTime) throws Exception {
127        long start = JobSupport.getDataTime(startTime);
128        long finish = JobSupport.getDataTime(finishTime);
129        this.jobScheduler.removeAllJobs(start, finish);
130    }
131
132    @Override
133    public void removeAllJobsAtScheduledTime(String time) throws Exception {
134        long removeAtTime = JobSupport.getDataTime(time);
135        this.jobScheduler.remove(removeAtTime);
136    }
137
138    @Override
139    public void removeJob(String jobId) throws Exception {
140        this.jobScheduler.remove(jobId);
141    }
142
143    @Override
144    public int getExecutionCount(String jobId) throws Exception {
145        int result = 0;
146
147        List<Job> jobs = this.jobScheduler.getAllJobs();
148        for (Job job : jobs) {
149            if (job.getJobId().equals(jobId)) {
150                result = job.getExecutionCount();
151            }
152        }
153
154        return result;
155    }
156}