001: /*
002: * $Id: QuartzConnector.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.quartz;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.lifecycle.InitialisationException;
015: import org.mule.api.transport.ConnectorException;
016: import org.mule.config.i18n.CoreMessages;
017: import org.mule.transport.AbstractConnector;
018:
019: import java.util.Properties;
020:
021: import org.quartz.Scheduler;
022: import org.quartz.SchedulerFactory;
023: import org.quartz.impl.StdSchedulerFactory;
024:
025: /**
026: * Creates a connection to a Quartz sheduler. This allows events to be sheduled at
027: * specific times, with repeated occurences.
028: */
029: public class QuartzConnector extends AbstractConnector {
030:
031: public static final String QUARTZ = "quartz";
032:
033: public static final String PROPERTY_CRON_EXPRESSION = "cronExpression";
034: public static final String PROPERTY_REPEAT_INTERVAL = "repeatInterval";
035: public static final String PROPERTY_REPEAT_COUNT = "repeatCount";
036: public static final String PROPERTY_START_DELAY = "startDelay";
037: public static final String PROPERTY_PAYLOAD = "payload";
038: public static final String PROPERTY_JOB_DISPATCH_ENDPOINT = "jobDispatchEndpoint";
039: public static final String PROPERTY_JOB_RECEIVE_ENDPOINT = "jobReceiveEndpoint";
040: public static final String PROPERTY_JOB_RECEIVE_TIMEOUT = "jobReceiveTimeout";
041: /** deprecated: use PROPERTY_PAYLOAD_REFERENCE */
042: public static final String PROPERTY_PAYLOAD_CLASS_NAME = "payloadClassName";
043: public static final String PROPERTY_PAYLOAD_REFERENCE = "payloadRef";
044: public static final String PROPERTY_GROUP_NAME = "groupName";
045: public static final String PROPERTY_JOB_GROUP_NAME = "jobGroupName";
046: public static final String PROPERTY_JOB_REF = "jobRef";
047: public static final String PROPERTY_JOB_CLASS = "jobClass";
048: public static final String PROPERTY_JOB_OBJECT = "jobObject";
049:
050: public static final String DEFAULT_GROUP_NAME = "mule";
051:
052: /**
053: * Properties to be used for creating the scheduler. If no properties are given, the
054: * scheduler will be created by StdSchedulerFactory.getDefaultScheduler()
055: */
056: private Properties factoryProperties = null;
057:
058: /**
059: * The scheduler instance. This can be configured by the user and injected as a bean
060: * or if not, it will be created by Mule upon initialization.
061: */
062: private Scheduler quartzScheduler = null;
063:
064: protected void doInitialise() throws InitialisationException {
065: try {
066: if (quartzScheduler == null) {
067: if (factoryProperties != null) {
068: SchedulerFactory factory = new StdSchedulerFactory(
069: factoryProperties);
070: quartzScheduler = factory.getScheduler();
071: } else {
072: quartzScheduler = StdSchedulerFactory
073: .getDefaultScheduler();
074: }
075: }
076: } catch (Exception e) {
077: throw new InitialisationException(CoreMessages
078: .initialisationFailure("Quartz provider"), e, this );
079: }
080: }
081:
082: protected void doDispose() {
083: // template method
084: }
085:
086: protected void doConnect() throws Exception {
087: // template method
088: }
089:
090: protected void doDisconnect() throws Exception {
091: // template method
092: }
093:
094: protected void doStart() throws MuleException {
095: try {
096: quartzScheduler.start();
097: } catch (Exception e) {
098: throw new ConnectorException(CoreMessages
099: .failedToStart("Quartz provider"), this , e);
100: }
101: }
102:
103: protected void doStop() throws MuleException {
104: try {
105: if (quartzScheduler != null) {
106: quartzScheduler.shutdown();
107: }
108: } catch (Exception e) {
109: throw new ConnectorException(CoreMessages
110: .failedToStop("Quartz provider"), this , e);
111: }
112: }
113:
114: public String getProtocol() {
115: return QUARTZ;
116: }
117:
118: public Scheduler getQuartzScheduler() {
119: return quartzScheduler;
120: }
121:
122: public void setQuartzScheduler(Scheduler scheduler) {
123: this .quartzScheduler = scheduler;
124: }
125:
126: public Properties getFactoryProperties() {
127: return factoryProperties;
128: }
129:
130: public void setFactoryProperties(Properties factoryProperties) {
131: this.factoryProperties = factoryProperties;
132: }
133:
134: }
|