Example usage for com.liferay.portal.kernel.scheduler TriggerState NORMAL

List of usage examples for com.liferay.portal.kernel.scheduler TriggerState NORMAL

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.scheduler TriggerState NORMAL.

Prototype

TriggerState NORMAL

To view the source code for com.liferay.portal.kernel.scheduler TriggerState NORMAL.

Click Source Link

Usage

From source file:de.uhh.l2g.plugins.util.PortletScheduler.java

License:Open Source License

/**
 * Schedule unscheduled Scheduler (requires valid SchedulerEntry Data &&
 * Message Date)/*from   w  ww  . j  a va2 s . c om*/
 * 
 * There is no Issue in Liferay Tracker about this: Manually assembling.
 * Message, Trigger and Job from previously "working" Job, in order to use
 * 
 * SchedulerEngineHelperUtil.schedule(Trigger, StorageType, Description,
 * Destination, Message, exceptionsMaxSize);
 * 
 * Currently (6.2 GA 4 - GA 6) also fails to register the MessageListener
 * correctly for a single consumer. This might be a configuration detail
 * issue, which is not documented in liferays SchedulerEngineHelperUtil, at
 * first sight source investigation does not reveal conclusive hints.
 * 
 * Though the more complicated issue is that Quartz does persist the Job on
 * UNSCHEDULE but not the Trigger.
 * 
 * Therefore a SchedulerEntry must be available in Memory, to schedule an
 * unscheduled Job Liferay SchedulerEntry however does persit the Trigger...
 * but does not check for duplicates on deploy.
 * 
 * If we keep the SchedulerEntry in the List we have the Trigger but
 * unregister fails and duplicates the Scheduler If we remove the
 * SchedulerEntry unregister works correctly but we lose the Trigger
 * 
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws SchedulerException
 * @throws Exception
 * @throws InstantiationException
 *             *
 */
public void schedule() {
    int exceptionsMaxSize = 0;
    try {
        String messageText = GetterUtil.getString((this.getMessage().toString()));
        LOG.info("Starting :" + messageText);

        Map<String, Object> map = this.getMessage().getValues();

        String portletId = "";
        String listenerName = "";

        if (map.containsKey(SchedulerEngine.PORTLET_ID))
            portletId = map.get(SchedulerEngine.PORTLET_ID).toString();
        if (map.containsKey(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME))
            listenerName = map.get(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME).toString();
        if (map.containsKey(SchedulerEngine.EXCEPTIONS_MAX_SIZE))
            exceptionsMaxSize = Integer.valueOf(map.get(SchedulerEngine.EXCEPTIONS_MAX_SIZE).toString());

        LOG.info("Message :" + portletId + " " + listenerName + " " + this.destination);

        if (this.getTrigger() == null)
            this.setTrigger(this.generalTrigger);
        TriggerState state = SchedulerEngineHelperUtil.getJobState(this.getJobName(), this.getGroupName(),
                this.getStorageType());
        LOG.info("Trigger :" + GetterUtil.getString(this.getTrigger().toString()) + state);
        if (state != null && state.equals(TriggerState.UNSCHEDULED)) {
            this.getMessage().put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, this.schedulerClassName);
            this.getMessage().setDestinationName(this.DEST);
            this.getMessage().put(SchedulerEngine.RECEIVER_KEY,
                    this.getMessage().getValues().get(SchedulerEngine.RECEIVER_KEY).toString());
            LOG.info("Scheduling :" + this.schedulerClassName + " ");
            LOG.info("New Message " + this.getMessage().toString());
            Thread thread = Thread.currentThread();
            LOG.info("Thread :" + thread.getContextClassLoader() + thread.toString());
            if (this.schedulerEntry != null && this.getTrigger() != null)
                SchedulerEngineHelperUtil.schedule(schedulerEntry, this.getStorageType(), portletId,
                        exceptionsMaxSize);
            else
                assembleEntryFromSettings(this.schedulerClassName, TriggerState.NORMAL);
        } else {
            if (state == null)
                LOG.error("Could not find Job with Name: " + this.schedulerClassName);
            else
                LOG.info("Could not schedule Job with State: " + state);
        }

    } catch (SchedulerException e) {
        LOG.warn("", e);
    } catch (IllegalArgumentException e) {
        LOG.warn(e);
    }
}

From source file:de.uhh.l2g.plugins.util.PortletScheduler.java

License:Open Source License

/**
 * Deletes the Scheduler thoroughly until server restart!
 * // w w  w .  j a va  2s .c  om
 */
public void delete() {
    try {
        List<SchedulerResponse> scheduledJobs = SchedulerEngineHelperUtil.getScheduledJobs();
        for (SchedulerResponse resp : scheduledJobs) {
            if (resp.getJobName().equalsIgnoreCase(this.schedulerClassName)) {

                LOG.info("Stopping :" + resp.toString());
                Map<String, Object> map = resp.getMessage().getValues();

                String portletId = "";
                String listenerName = "";

                if (map.containsKey(SchedulerEngine.PORTLET_ID))
                    portletId = map.get(SchedulerEngine.PORTLET_ID).toString();
                if (map.containsKey(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME))
                    listenerName = map.get(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME).toString();

                LOG.info("Associated to :" + portletId + " " + listenerName + " " + this.destination);

                TriggerState state = SchedulerEngineHelperUtil.getJobState(resp.getJobName(),
                        resp.getGroupName(), resp.getStorageType());

                if (state.equals(TriggerState.NORMAL)) {

                    Thread thread = Thread.currentThread();
                    LOG.info("Thread :" + thread.getContextClassLoader() + thread.toString());

                    LOG.info("Unscheduling :" + this.schedulerClassName + " " + resp.getTrigger().toString());
                    SchedulerEngineHelperUtil.delete(schedulerEntry, resp.getStorageType());
                    // This removes the SchedulerEntry from Liferay's
                    // Scheduler List so it is not started aditionally on
                    // re-deploy
                    // Removing this entry will also destroy our Trigger (as
                    // Quartz unschedule(), removes the Trigger from
                    // QuartzJob
                    this.removeFromEntries();
                } else {
                    LOG.warn("Scheduler could not be unscheduled beacuse it was in state " + state);
                }
            }
        }

    } catch (SchedulerException e) {
        LOG.warn("Failed to unschedule job" + this.schedulerClassName, e);
    }
}