1 22 package org.jboss.resource.adapter.quartz.inflow; 23 24 import org.jboss.logging.Logger; 25 import org.quartz.*; 26 import org.quartz.impl.StdSchedulerFactory; 27 28 import javax.resource.spi.ResourceAdapter ; 29 import javax.resource.spi.BootstrapContext ; 30 import javax.resource.spi.ResourceAdapterInternalException ; 31 import javax.resource.spi.ActivationSpec ; 32 import javax.resource.spi.work.WorkManager ; 33 import javax.resource.spi.endpoint.MessageEndpointFactory ; 34 import javax.resource.spi.endpoint.MessageEndpoint ; 35 import javax.resource.ResourceException ; 36 import javax.transaction.xa.XAResource ; 37 38 44 public class QuartzResourceAdapter implements ResourceAdapter 45 { 46 private static Logger log = Logger.getLogger(QuartzResourceAdapter.class); 47 48 private static final ThreadLocal holder = new ThreadLocal (); 49 50 private Scheduler sched; 51 52 public static WorkManager getConfigTimeWorkManager() { 53 return (WorkManager ) holder.get(); 54 } 55 56 public void start(BootstrapContext ctx) throws ResourceAdapterInternalException 57 { 58 log.debug("start quartz!!!"); 59 SchedulerFactory sf = new StdSchedulerFactory(); 61 try 62 { 63 holder.set(ctx.getWorkManager()); 64 sched = sf.getScheduler(); 65 sched.start(); 66 } 67 catch (SchedulerException e) 68 { 69 throw new ResourceAdapterInternalException (e); 70 } 71 finally 72 { 73 holder.set(null); 74 } 75 } 76 77 public void stop() 78 { 79 log.debug("stop"); 80 try 81 { 82 sched.shutdown(true); 83 } 84 catch (SchedulerException e) 85 { 86 throw new RuntimeException (e); 87 } 88 } 89 90 public void endpointActivation(MessageEndpointFactory endpointFactory, 91 ActivationSpec spec) 92 throws ResourceException 93 { 94 log.debug("endpointActivation, spec="+spec); 95 QuartzActivationSpec quartzSpec = (QuartzActivationSpec) spec; 96 97 Class clazz = QuartzJob.class; 99 MessageEndpoint tmpMe = endpointFactory.createEndpoint(null); 100 if (tmpMe instanceof StatefulJob) clazz = StatefulQuartzJob.class; 101 tmpMe.release(); 102 103 try 104 { 105 JobDetail jobDetail = new JobDetail(quartzSpec.getJobName(), quartzSpec.getJobGroup(), clazz, true, false, false); 106 jobDetail.getJobDataMap().setAllowsTransientData(true); 107 jobDetail.getJobDataMap().put("endpointFactory", endpointFactory); 108 log.debug("adding job: " + quartzSpec); 109 CronTrigger trigger = new CronTrigger(quartzSpec.getTriggerName(), quartzSpec.getTriggerGroup(), quartzSpec.getCronTrigger()); 110 sched.scheduleJob(jobDetail, trigger); 111 } 112 catch (Exception e) 113 { 114 log.error(e); 115 throw new ResourceException (e); 116 } 117 } 118 119 public void endpointDeactivation(MessageEndpointFactory endpointFactory, 120 ActivationSpec spec) 121 { 122 QuartzActivationSpec quartzSpec = (QuartzActivationSpec) spec; 123 try 124 { 125 log.debug("****endpointDeactivation: " + quartzSpec); 126 sched.deleteJob(quartzSpec.getJobName(), quartzSpec.getJobGroup()); 127 } 128 catch (SchedulerException e) 129 { 130 throw new RuntimeException (e); 131 } 132 } 133 134 public XAResource [] getXAResources(ActivationSpec [] specs) throws ResourceException 135 { 136 return new XAResource [0]; 137 } 138 139 } 140 | Popular Tags |