KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > quartz > QuartzMessageReceiver


1 /*
2  * $Id: QuartzMessageReceiver.java 3982 2006-11-22 14:28:01Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.quartz;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.providers.AbstractMessageReceiver;
16 import org.mule.providers.quartz.jobs.MuleReceiverJob;
17 import org.mule.umo.UMOComponent;
18 import org.mule.umo.UMOException;
19 import org.mule.umo.endpoint.EndpointException;
20 import org.mule.umo.endpoint.UMOEndpoint;
21 import org.mule.umo.lifecycle.InitialisationException;
22 import org.mule.umo.provider.UMOConnector;
23 import org.quartz.CronTrigger;
24 import org.quartz.JobDataMap;
25 import org.quartz.JobDetail;
26 import org.quartz.Scheduler;
27 import org.quartz.SimpleTrigger;
28 import org.quartz.Trigger;
29
30 import java.util.Date JavaDoc;
31
32 /**
33  * Listens for Quartz sheduled events using the Receiver Job and fires events to the
34  * component associated with this receiver.
35  */

36 public class QuartzMessageReceiver extends AbstractMessageReceiver
37 {
38
39     public static final String JavaDoc QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver";
40
41     private final QuartzConnector connector;
42
43     public QuartzMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
44         throws InitialisationException
45     {
46         super(connector, component, endpoint);
47         this.connector = (QuartzConnector)connector;
48     }
49
50     protected void doDispose()
51     {
52         // template method
53
}
54
55     public void doStart() throws UMOException
56     {
57         try
58         {
59             Scheduler scheduler = connector.getScheduler();
60
61             JobDetail jobDetail = new JobDetail();
62             jobDetail.setName(endpoint.getEndpointURI().toString());
63             jobDetail.setJobClass(MuleReceiverJob.class);
64             JobDataMap jobDataMap = new JobDataMap();
65             jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this);
66             jobDataMap.putAll(endpoint.getProperties());
67             jobDetail.setJobDataMap(jobDataMap);
68
69             Trigger trigger = null;
70             String JavaDoc cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
71             String JavaDoc repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
72             String JavaDoc repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
73             String JavaDoc startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY);
74             String JavaDoc groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME);
75             String JavaDoc jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME);
76
77             if (groupName == null)
78             {
79                 groupName = QuartzConnector.DEFAULT_GROUP_NAME;
80             }
81             if (jobGroupName == null)
82             {
83                 jobGroupName = groupName;
84             }
85
86             jobDetail.setGroup(groupName);
87
88             if (cronExpression != null)
89             {
90                 CronTrigger ctrigger = new CronTrigger();
91                 ctrigger.setCronExpression(cronExpression);
92                 trigger = ctrigger;
93             }
94             else if (repeatInterval != null)
95             {
96                 SimpleTrigger strigger = new SimpleTrigger();
97                 strigger.setRepeatInterval(Long.parseLong(repeatInterval));
98                 if (repeatCount != null)
99                 {
100                     strigger.setRepeatCount(Integer.parseInt(repeatCount));
101                 }
102                 else
103                 {
104                     strigger.setRepeatCount(-1);
105                 }
106                 trigger = strigger;
107             }
108             else
109             {
110                 throw new IllegalArgumentException JavaDoc(new Message("quartz", 1).getMessage());
111             }
112             long start = System.currentTimeMillis();
113             if (startDelay != null)
114             {
115                 start += Long.parseLong(startDelay);
116             }
117             trigger.setStartTime(new Date JavaDoc(start));
118             trigger.setName(endpoint.getEndpointURI().toString());
119             trigger.setGroup(groupName);
120             trigger.setJobName(endpoint.getEndpointURI().toString());
121             trigger.setJobGroup(jobGroupName);
122
123             scheduler.scheduleJob(jobDetail, trigger);
124             scheduler.start();
125         }
126         catch (Exception JavaDoc e)
127         {
128             throw new EndpointException(new Message(Messages.FAILED_TO_START_X, "Quartz receiver"), e);
129         }
130     }
131
132     public void doConnect() throws Exception JavaDoc
133     {
134         // template method
135
}
136
137     public void doDisconnect() throws Exception JavaDoc
138     {
139         // template method
140
}
141
142 }
143
Popular Tags