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     */
017    package org.apache.activemq.broker.jmx;
018    
019    import org.apache.activemq.broker.jmx.OpenTypeSupport.OpenTypeFactory;
020    import org.apache.activemq.broker.scheduler.Job;
021    import org.apache.activemq.broker.scheduler.JobScheduler;
022    import org.apache.activemq.broker.scheduler.JobSupport;
023    
024    import javax.management.openmbean.*;
025    import java.io.IOException;
026    import java.util.List;
027    
028    public class JobSchedulerView implements JobSchedulerViewMBean {
029    
030        private final JobScheduler jobScheduler;
031    
032        public JobSchedulerView(JobScheduler jobScheduler) {
033            this.jobScheduler = jobScheduler;
034        }
035    
036        public TabularData getAllJobs() throws Exception {
037            OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
038            CompositeType ct = factory.getCompositeType();
039            TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
040            TabularDataSupport rc = new TabularDataSupport(tt);
041            List<Job> jobs = this.jobScheduler.getAllJobs();
042            for (Job job : jobs) {
043                rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
044            }
045            return rc;
046        }
047    
048        public TabularData getAllJobs(String startTime, String finishTime) throws Exception {
049            OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
050            CompositeType ct = factory.getCompositeType();
051            TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
052            TabularDataSupport rc = new TabularDataSupport(tt);
053            long start = JobSupport.getDataTime(startTime);
054            long finish = JobSupport.getDataTime(finishTime);
055            List<Job> jobs = this.jobScheduler.getAllJobs(start, finish);
056            for (Job job : jobs) {
057                rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
058            }
059            return rc;
060        }
061    
062        public TabularData getNextScheduleJobs() throws Exception {
063            OpenTypeFactory factory = OpenTypeSupport.getFactory(Job.class);
064            CompositeType ct = factory.getCompositeType();
065            TabularType tt = new TabularType("Scheduled Jobs", "Scheduled Jobs", ct, new String[] { "jobId" });
066            TabularDataSupport rc = new TabularDataSupport(tt);
067            List<Job> jobs = this.jobScheduler.getNextScheduleJobs();
068            for (Job job : jobs) {
069                rc.put(new CompositeDataSupport(ct, factory.getFields(job)));
070            }
071            return rc;
072        }
073    
074        public String getNextScheduleTime() throws Exception {
075            long time = this.jobScheduler.getNextScheduleTime();
076            return JobSupport.getDateTime(time);
077        }
078    
079        public void removeAllJobs() throws Exception {
080            this.jobScheduler.removeAllJobs();
081    
082        }
083    
084        public void removeAllJobs(String startTime, String finishTime) throws Exception {
085            long start = JobSupport.getDataTime(startTime);
086            long finish = JobSupport.getDataTime(finishTime);
087            this.jobScheduler.removeAllJobs(start, finish);
088    
089        }
090    
091        public void removeJob(String jobId) throws Exception {
092            this.jobScheduler.remove(jobId);
093    
094        }
095    
096        public void removeJobAtScheduledTime(String time) throws IOException {
097            // TODO Auto-generated method stub
098    
099        }
100    
101    }