1 17 package org.apache.servicemix.components.quartz; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.MessageExchangeListener; 22 import org.apache.servicemix.components.util.ComponentSupport; 23 import org.quartz.JobDetail; 24 import org.quartz.JobExecutionContext; 25 import org.quartz.JobExecutionException; 26 import org.quartz.Scheduler; 27 import org.quartz.SchedulerException; 28 import org.quartz.SchedulerFactory; 29 import org.quartz.Trigger; 30 import org.quartz.impl.StdSchedulerFactory; 31 32 import javax.jbi.JBIException; 33 import javax.jbi.messaging.InOnly; 34 import javax.jbi.messaging.MessageExchange; 35 import javax.jbi.messaging.MessagingException; 36 import javax.jbi.messaging.NormalizedMessage; 37 import java.util.Date ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 41 46 public class QuartzComponent extends ComponentSupport implements MessageExchangeListener { 47 private static final transient Log log = LogFactory.getLog(QuartzComponent.class); 48 49 public static final String COMPONENT_KEY = "org.apache.servicemix.component"; 50 51 private SchedulerFactory factory; 52 private Scheduler scheduler; 53 private Map triggers; 54 private QuartzMarshaler marshaler = new DefaultQuartzMarshaler(); 55 56 public void start() throws JBIException { 57 try { 58 scheduler.start(); 59 super.start(); 60 } catch (SchedulerException e) { 61 throw new JBIException(e); 62 } 63 } 64 65 public void stop() throws JBIException { 66 try { 67 super.stop(); 68 scheduler.standby(); 69 } catch (SchedulerException e) { 70 throw new JBIException(e); 71 } 72 } 73 74 public void shutDown() throws JBIException { 75 try { 76 scheduler.shutdown(); 77 } catch (SchedulerException e) { 78 throw new JBIException(e); 79 } finally { 80 super.shutDown(); 81 } 82 } 83 84 public void addTrigger(Trigger trigger, JobDetail detail) throws JBIException { 85 try { 86 if (trigger.getName() == null) { 88 trigger.setName(detail.getName()); 89 } 90 if (trigger.getGroup() == null) { 92 trigger.setGroup(detail.getGroup()); 93 } 94 if (trigger.getStartTime() == null) { 96 trigger.setStartTime(new Date ()); 97 } 98 detail.getJobDataMap().put(COMPONENT_KEY, this); 99 Class jobClass = detail.getJobClass(); 100 if (jobClass == null) { 101 detail.setJobClass(ServiceMixJob.class); 102 } 103 scheduler.scheduleJob(detail, trigger); 104 } 105 catch (SchedulerException e) { 106 throw new JBIException("Failed to add trigger: " + trigger + " with detail: " + detail + ". Reason: " + e, e); 107 } 108 } 109 110 111 116 public void onJobExecute(JobExecutionContext context) throws JobExecutionException { 117 if (log.isDebugEnabled()) { 118 log.debug("Firing Quartz Job with context: " + context); 119 } 120 try { 121 InOnly exchange = getExchangeFactory().createInOnlyExchange(); 122 NormalizedMessage message = exchange.createMessage(); 123 getMarshaler().populateNormalizedMessage(message, context); 124 exchange.setInMessage(message); 125 send(exchange); 126 } 127 catch (MessagingException e) { 128 throw new JobExecutionException(e); 129 } 130 } 131 132 133 public SchedulerFactory getFactory() { 136 return factory; 137 } 138 139 public void setFactory(SchedulerFactory factory) { 140 this.factory = factory; 141 } 142 143 public Scheduler getScheduler() { 144 return scheduler; 145 } 146 147 public void setScheduler(Scheduler scheduler) { 148 this.scheduler = scheduler; 149 } 150 151 public Map getTriggers() { 152 return triggers; 153 } 154 155 public void setTriggers(Map triggers) { 156 this.triggers = triggers; 157 } 158 159 public QuartzMarshaler getMarshaler() { 160 return marshaler; 161 } 162 163 public void setMarshaler(QuartzMarshaler marshaler) { 164 this.marshaler = marshaler; 165 } 166 167 protected void init() throws JBIException { 170 super.init(); 171 try { 172 if (scheduler == null) { 173 if (factory == null) { 174 factory = new StdSchedulerFactory(); 175 } 176 scheduler = factory.getScheduler(); 177 } 178 } 179 catch (SchedulerException e) { 180 throw new JBIException(e); 181 } 182 183 if (triggers != null) { 184 for (Iterator iter = triggers.entrySet().iterator(); iter.hasNext();) { 185 Map.Entry entry = (Map.Entry ) iter.next(); 186 Object key = entry.getKey(); 187 Object value = entry.getValue(); 188 if (key == null) { 189 throw new IllegalArgumentException ("Key of the map cannot be null"); 190 } 191 if (value == null) { 192 throw new IllegalArgumentException ("Key of the map cannot be null"); 193 } 194 if (!(key instanceof Trigger)) { 195 throw new IllegalArgumentException ("Key of the map must be a Trigger but was: " + key.getClass().getName()); 196 } 197 if (!(value instanceof JobDetail)) { 198 throw new IllegalArgumentException ("Key of the map must be a JobDetail but was: " + value.getClass().getName()); 199 } 200 addTrigger((Trigger) key, (JobDetail) value); 201 } 202 } 203 } 204 205 public void onMessageExchange(MessageExchange exchange) throws MessagingException { 206 } 209 210 } 211 | Popular Tags |