1 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 ; 31 32 36 public class QuartzMessageReceiver extends AbstractMessageReceiver 37 { 38 39 public static final String 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 } 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 cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION); 71 String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL); 72 String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT); 73 String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY); 74 String groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME); 75 String 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 (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 (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 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 133 { 134 } 136 137 public void doDisconnect() throws Exception 138 { 139 } 141 142 } 143 | Popular Tags |