001: /*
002: * $Id: QuartzMessageReceiver.java 10961 2008-02-22 19:01:02Z 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.endpoint.EndpointException;
015: import org.mule.api.endpoint.InboundEndpoint;
016: import org.mule.api.lifecycle.CreateException;
017: import org.mule.api.service.Service;
018: import org.mule.api.transport.Connector;
019: import org.mule.config.i18n.CoreMessages;
020: import org.mule.transport.AbstractMessageReceiver;
021: import org.mule.transport.quartz.i18n.QuartzMessages;
022: import org.mule.transport.quartz.jobs.MuleReceiverJob;
023:
024: import java.util.Date;
025:
026: import org.quartz.CronTrigger;
027: import org.quartz.JobDataMap;
028: import org.quartz.JobDetail;
029: import org.quartz.ObjectAlreadyExistsException;
030: import org.quartz.Scheduler;
031: import org.quartz.SimpleTrigger;
032: import org.quartz.Trigger;
033:
034: /**
035: * Listens for Quartz sheduled events using the Receiver Job and fires events to the
036: * service associated with this receiver.
037: */
038: public class QuartzMessageReceiver extends AbstractMessageReceiver {
039:
040: public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver";
041: public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector";
042:
043: private final QuartzConnector connector;
044:
045: public QuartzMessageReceiver(Connector connector, Service service,
046: InboundEndpoint endpoint) throws CreateException {
047: super (connector, service, endpoint);
048: this .connector = (QuartzConnector) connector;
049: }
050:
051: protected void doDispose() {
052: // template method
053: }
054:
055: protected void doStart() throws MuleException {
056: try {
057: Scheduler scheduler = connector.getQuartzScheduler();
058:
059: JobDetail jobDetail = new JobDetail();
060: jobDetail.setName(endpoint.getEndpointURI().toString());
061: jobDetail.setJobClass(MuleReceiverJob.class);
062: JobDataMap jobDataMap = new JobDataMap();
063: jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this
064: .getReceiverKey());
065: jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this .connector
066: .getName());
067: jobDataMap.putAll(endpoint.getProperties());
068: jobDetail.setJobDataMap(jobDataMap);
069:
070: Trigger trigger = null;
071: String cronExpression = jobDataMap
072: .getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
073: String repeatInterval = jobDataMap
074: .getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
075: String repeatCount = jobDataMap
076: .getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
077: String startDelay = jobDataMap
078: .getString(QuartzConnector.PROPERTY_START_DELAY);
079: String groupName = jobDataMap
080: .getString(QuartzConnector.PROPERTY_GROUP_NAME);
081: String jobGroupName = jobDataMap
082: .getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME);
083:
084: if (groupName == null) {
085: groupName = QuartzConnector.DEFAULT_GROUP_NAME;
086: }
087: if (jobGroupName == null) {
088: jobGroupName = groupName;
089: }
090:
091: jobDetail.setGroup(groupName);
092:
093: if (cronExpression != null) {
094: CronTrigger ctrigger = new CronTrigger();
095: ctrigger.setCronExpression(cronExpression);
096: trigger = ctrigger;
097: } else if (repeatInterval != null) {
098: SimpleTrigger strigger = new SimpleTrigger();
099: strigger.setRepeatInterval(Long
100: .parseLong(repeatInterval));
101: if (repeatCount != null) {
102: strigger.setRepeatCount(Integer
103: .parseInt(repeatCount));
104: } else {
105: strigger.setRepeatCount(-1);
106: }
107: trigger = strigger;
108: } else {
109: throw new IllegalArgumentException(QuartzMessages
110: .cronExpressionOrIntervalMustBeSet()
111: .getMessage());
112: }
113: long start = System.currentTimeMillis();
114: if (startDelay != null) {
115: start += Long.parseLong(startDelay);
116: }
117: trigger.setStartTime(new Date(start));
118: trigger.setName(endpoint.getEndpointURI().toString());
119: trigger.setGroup(groupName);
120: trigger.setJobName(endpoint.getEndpointURI().toString());
121: trigger.setJobGroup(jobGroupName);
122:
123: // We need to handle cases when the job has already been
124: // persisted
125: try {
126: scheduler.scheduleJob(jobDetail, trigger);
127: } catch (ObjectAlreadyExistsException oaee) {
128: // Do anything here?
129: }
130:
131: scheduler.start();
132: } catch (Exception e) {
133: throw new EndpointException(CoreMessages
134: .failedToStart("Quartz receiver"), e);
135: }
136: }
137:
138: protected void doStop() throws MuleException {
139: // nothing to do
140: }
141:
142: protected void doConnect() throws Exception {
143: // nothing to do
144: }
145:
146: protected void doDisconnect() throws Exception {
147: // nothing to do
148: }
149:
150: }
|