Search in sources :

Example 6 with QuartzScheduler

use of org.pentaho.platform.scheduler2.quartz.QuartzScheduler in project pentaho-platform by pentaho.

the class BlockoutManagerUtil method getFireTimes.

public static List<Date> getFireTimes(IJobTrigger jobTrigger, IScheduler scheduler) {
    // Determines the maximum amount of fire times allowed to be calculated
    int n = 1000;
    Date startDate = new Date(System.currentTimeMillis());
    Date endDate = new Date(startDate.getTime() + 4 * TIME.YEAR.time);
    // Quartz Triggers
    if (scheduler instanceof QuartzScheduler) {
        try {
            List<Date> dates = new ArrayList<Date>();
            boolean endDateIsNull = jobTrigger.getEndTime() == null;
            // $NON-NLS-1$ //$NON-NLS-2$
            Trigger trigger = QuartzScheduler.createQuartzTrigger(jobTrigger, new QuartzJobKey("test", "test"));
            // add previous trigger (it might be currently active)
            // $NON-NLS-1$;
            IBlockoutManager manager = PentahoSystem.get(IBlockoutManager.class, "IBlockoutManager", null);
            if (manager != null) {
                List<Job> blockouts = manager.getBlockOutJobs();
                for (Job blockout : blockouts) {
                    if (blockout.getLastRun() != null) {
                        dates.add(blockout.getLastRun());
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                Date nextFireTime = trigger.getFireTimeAfter(startDate);
                if ((nextFireTime == null) || (nextFireTime.after(endDate) || (!endDateIsNull && nextFireTime.after(jobTrigger.getEndTime())))) {
                    break;
                }
                dates.add(nextFireTime);
                startDate = nextFireTime;
            }
            return dates;
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
    throw new RuntimeException(// $NON-NLS-1$
    "Can not calculate fire times for unsupported Scheduler Type: " + scheduler.getClass().getSimpleName());
}
Also used : SchedulerException(org.pentaho.platform.api.scheduler2.SchedulerException) QuartzJobKey(org.pentaho.platform.scheduler2.quartz.QuartzJobKey) ArrayList(java.util.ArrayList) QuartzScheduler(org.pentaho.platform.scheduler2.quartz.QuartzScheduler) Date(java.util.Date) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) Trigger(org.quartz.Trigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Job(org.pentaho.platform.api.scheduler2.Job) IBlockoutManager(org.pentaho.platform.api.scheduler2.IBlockoutManager)

Example 7 with QuartzScheduler

use of org.pentaho.platform.scheduler2.quartz.QuartzScheduler in project pentaho-platform by pentaho.

the class QuartzThreadsIT method init.

@Before
public void init() throws SchedulerException, PlatformInitializationException, org.quartz.SchedulerException, IOException {
    scheduler = new QuartzScheduler();
    Properties props = new Properties();
    props.put("org.quartz.scheduler.makeSchedulerThreadDaemon", "true");
    props.put("org.quartz.threadPool.makeThreadsDaemons", "true");
    props.put("org.quartz.scheduler.instanceName", SCHEDULER_NAME);
    props.put("org.quartz.scheduler.instanceId", "1");
    props.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
    props.put("org.quartz.threadPool.threadCount", new Integer(WORKER_THREAD_COUNT).toString());
    props.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");
    System.err.println("Quartz initialized with properties:");
    Assert.assertTrue("Properties object is empty! Something went wrong!", props.size() > 0);
    // BEWARE JUnit does not allow exceptions to fail a @Before, so this
    props.store(System.out, "QuartzThreadsTest");
    // must be visually inspected
    // org.quartz.scheduler.rmi.export = false
    // org.quartz.scheduler.rmi.proxy = false
    scheduler.setQuartzSchedulerFactory(new org.quartz.impl.StdSchedulerFactory(props));
    scheduler.start();
}
Also used : QuartzScheduler(org.pentaho.platform.scheduler2.quartz.QuartzScheduler) Properties(java.util.Properties) Before(org.junit.Before)

Example 8 with QuartzScheduler

use of org.pentaho.platform.scheduler2.quartz.QuartzScheduler in project pentaho-platform by pentaho.

the class SchedulerResourceUtil method convertScheduleRequestToJobTrigger.

public static IJobTrigger convertScheduleRequestToJobTrigger(JobScheduleRequest scheduleRequest, IScheduler scheduler) throws SchedulerException, UnifiedRepositoryException {
    // Used to determine if created by a RunInBackgroundCommand
    boolean runInBackground = scheduleRequest.getSimpleJobTrigger() == null && scheduleRequest.getComplexJobTrigger() == null && scheduleRequest.getCronJobTrigger() == null;
    // add 10 seconds to the RIB to ensure execution (see PPP-3264)
    IJobTrigger jobTrigger = runInBackground ? new SimpleJobTrigger(new Date(System.currentTimeMillis() + 10000), null, 0, 0) : scheduleRequest.getSimpleJobTrigger();
    if (scheduleRequest.getSimpleJobTrigger() != null) {
        SimpleJobTrigger simpleJobTrigger = scheduleRequest.getSimpleJobTrigger();
        if (simpleJobTrigger.getStartTime() == null) {
            simpleJobTrigger.setStartTime(new Date());
        }
        jobTrigger = simpleJobTrigger;
    } else if (scheduleRequest.getComplexJobTrigger() != null) {
        ComplexJobTriggerProxy proxyTrigger = scheduleRequest.getComplexJobTrigger();
        ComplexJobTrigger complexJobTrigger = new ComplexJobTrigger();
        complexJobTrigger.setStartTime(proxyTrigger.getStartTime());
        complexJobTrigger.setEndTime(proxyTrigger.getEndTime());
        complexJobTrigger.setDuration(scheduleRequest.getDuration());
        if (proxyTrigger.getDaysOfWeek().length > 0) {
            if (proxyTrigger.getWeeksOfMonth().length > 0) {
                for (int dayOfWeek : proxyTrigger.getDaysOfWeek()) {
                    for (int weekOfMonth : proxyTrigger.getWeeksOfMonth()) {
                        QualifiedDayOfWeek qualifiedDayOfWeek = new QualifiedDayOfWeek();
                        qualifiedDayOfWeek.setDayOfWeek(DayOfWeek.values()[dayOfWeek]);
                        if (weekOfMonth == JobScheduleRequest.LAST_WEEK_OF_MONTH) {
                            qualifiedDayOfWeek.setQualifier(DayOfWeekQualifier.LAST);
                        } else {
                            qualifiedDayOfWeek.setQualifier(DayOfWeekQualifier.values()[weekOfMonth]);
                        }
                        complexJobTrigger.addDayOfWeekRecurrence(qualifiedDayOfWeek);
                    }
                }
            } else {
                for (int dayOfWeek : proxyTrigger.getDaysOfWeek()) {
                    complexJobTrigger.addDayOfWeekRecurrence(dayOfWeek + 1);
                }
            }
        } else if (proxyTrigger.getDaysOfMonth().length > 0) {
            for (int dayOfMonth : proxyTrigger.getDaysOfMonth()) {
                complexJobTrigger.addDayOfMonthRecurrence(dayOfMonth);
            }
        }
        for (int month : proxyTrigger.getMonthsOfYear()) {
            complexJobTrigger.addMonthlyRecurrence(month + 1);
        }
        for (int year : proxyTrigger.getYears()) {
            complexJobTrigger.addYearlyRecurrence(year);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(complexJobTrigger.getStartTime());
        complexJobTrigger.setHourlyRecurrence(calendar.get(Calendar.HOUR_OF_DAY));
        complexJobTrigger.setMinuteRecurrence(calendar.get(Calendar.MINUTE));
        complexJobTrigger.setUiPassParam(scheduleRequest.getComplexJobTrigger().getUiPassParam());
        jobTrigger = complexJobTrigger;
    } else if (scheduleRequest.getCronJobTrigger() != null) {
        if (scheduler instanceof QuartzScheduler) {
            String cronString = scheduleRequest.getCronJobTrigger().getCronString();
            // $NON-NLS-1$
            String delims = "[ ]+";
            String[] tokens = cronString.split(delims);
            if (tokens.length < 7) {
                cronString += " *";
            }
            ComplexJobTrigger complexJobTrigger = QuartzScheduler.createComplexTrigger(cronString);
            complexJobTrigger.setStartTime(scheduleRequest.getCronJobTrigger().getStartTime());
            complexJobTrigger.setEndTime(scheduleRequest.getCronJobTrigger().getEndTime());
            complexJobTrigger.setDuration(scheduleRequest.getCronJobTrigger().getDuration());
            complexJobTrigger.setUiPassParam(scheduleRequest.getCronJobTrigger().getUiPassParam());
            jobTrigger = complexJobTrigger;
        } else {
            throw new IllegalArgumentException();
        }
    }
    return jobTrigger;
}
Also used : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) QuartzScheduler(org.pentaho.platform.scheduler2.quartz.QuartzScheduler) Date(java.util.Date) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) QualifiedDayOfWeek(org.pentaho.platform.scheduler2.recur.QualifiedDayOfWeek) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) IJobTrigger(org.pentaho.platform.api.scheduler2.IJobTrigger)

Aggregations

QuartzScheduler (org.pentaho.platform.scheduler2.quartz.QuartzScheduler)7 Before (org.junit.Before)4 Date (java.util.Date)3 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)3 Job (org.pentaho.platform.api.scheduler2.Job)3 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)3 File (java.io.File)2 Calendar (java.util.Calendar)2 Test (org.junit.Test)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 FileSystemBackedUnifiedRepository (org.pentaho.platform.repository2.unified.fs.FileSystemBackedUnifiedRepository)2 TestQuartzScheduler (org.pentaho.platform.scheduler2.ws.test.TestQuartzScheduler)2 OutputStream (java.io.OutputStream)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1