1 10 11 package org.mule.providers.quartz; 12 13 import org.mule.MuleManager; 14 import org.mule.config.i18n.Message; 15 import org.mule.providers.AbstractMessageDispatcher; 16 import org.mule.providers.quartz.jobs.DelegatingJob; 17 import org.mule.umo.UMOEvent; 18 import org.mule.umo.UMOException; 19 import org.mule.umo.UMOMessage; 20 import org.mule.umo.endpoint.UMOImmutableEndpoint; 21 import org.mule.umo.provider.DispatchException; 22 import org.mule.util.ClassUtils; 23 import org.quartz.CronTrigger; 24 import org.quartz.Job; 25 import org.quartz.JobDataMap; 26 import org.quartz.JobDetail; 27 import org.quartz.Scheduler; 28 import org.quartz.SimpleTrigger; 29 import org.quartz.Trigger; 30 31 import java.util.Date ; 32 import java.util.Iterator ; 33 34 39 public class QuartzMessageDispatcher extends AbstractMessageDispatcher 40 { 41 42 public QuartzMessageDispatcher(UMOImmutableEndpoint endpoint) 43 { 44 super(endpoint); 45 } 46 47 protected void doDispose() 48 { 49 } 51 52 protected void doDispatch(UMOEvent event) throws Exception 53 { 54 JobDetail jobDetail = new JobDetail(); 55 jobDetail.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId()); 57 58 JobDataMap jobDataMap = new JobDataMap(); 59 UMOMessage msg = event.getMessage(); 60 for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) 61 { 62 String propertyKey = (String )iterator.next(); 63 jobDataMap.put(propertyKey, msg.getProperty(propertyKey)); 64 } 65 jobDetail.setJobDataMap(jobDataMap); 66 67 Job job; 68 Object payload = event.getTransformedMessage(); 70 71 String jobClass = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_CLASS); 72 if (payload instanceof Job) 73 { 74 job = (Job)payload; 75 jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job); 76 jobDetail.setJobClass(DelegatingJob.class); 77 } 78 else if (jobClass != null) 79 { 80 jobDetail.setJobClass(ClassUtils.loadClass(jobClass, getClass())); 81 } 82 else 83 { 84 Object tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_OBJECT); 85 if (tempJob == null) 86 { 87 tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_REF); 88 if (tempJob == null) 89 { 90 throw new DispatchException(new Message("quartz", 2), event.getMessage(), 91 event.getEndpoint()); 92 } 93 else 94 { 95 tempJob = MuleManager.getInstance().getContainerContext().getComponent(tempJob); 96 if (!(tempJob instanceof Job)) 97 { 98 throw new DispatchException(new Message("quartz", 3), event.getMessage(), 99 event.getEndpoint()); 100 } 101 } 102 } 103 else if (!(tempJob instanceof Job)) 104 { 105 throw new DispatchException(new Message("quartz", 3), event.getMessage(), event.getEndpoint()); 106 } 107 jobDetail.setJobClass(DelegatingJob.class); 108 } 109 110 jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload); 111 112 Trigger trigger = null; 113 String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION); 114 String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL); 115 String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT); 116 String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY); 117 String groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME); 118 String jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME); 119 120 if (groupName == null) 121 { 122 groupName = QuartzConnector.DEFAULT_GROUP_NAME; 123 } 124 if (jobGroupName == null) 125 { 126 jobGroupName = groupName; 127 } 128 129 jobDetail.setGroup(groupName); 130 131 if (cronExpression != null) 132 { 133 CronTrigger ctrigger = new CronTrigger(); 134 ctrigger.setCronExpression(cronExpression); 135 trigger = ctrigger; 136 } 137 else if (repeatInterval != null) 138 { 139 SimpleTrigger strigger = new SimpleTrigger(); 140 strigger.setRepeatInterval(Long.parseLong(repeatInterval)); 141 if (repeatCount != null) 142 { 143 strigger.setRepeatCount(Integer.parseInt(repeatCount)); 144 } 145 else 146 { 147 strigger.setRepeatCount(-1); 148 } 149 trigger = strigger; 150 } 151 else 152 { 153 throw new IllegalArgumentException (new Message("quartz", 1).getMessage()); 154 } 155 long start = System.currentTimeMillis(); 156 if (startDelay != null) 157 { 158 start += Long.parseLong(startDelay); 159 } 160 trigger.setStartTime(new Date (start)); 161 trigger.setName(event.getEndpoint().getEndpointURI().toString()); 162 trigger.setGroup(groupName); 163 trigger.setJobName(jobDetail.getName()); 164 trigger.setJobGroup(jobGroupName); 165 166 Scheduler scheduler = ((QuartzConnector)this.getConnector()).getScheduler(); 167 scheduler.scheduleJob(jobDetail, trigger); 168 } 169 170 protected UMOMessage doSend(UMOEvent event) throws Exception 171 { 172 doDispatch(event); 173 return null; 174 } 175 176 protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception 177 { 178 } 180 181 protected void doDisconnect() throws Exception 182 { 183 } 185 186 198 protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 199 { 200 throw new UnsupportedOperationException ("doReceive"); 201 } 202 203 public Object getDelegateSession() throws UMOException 204 { 205 return null; 206 } 207 } 208 | Popular Tags |