1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
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 /**
39 * Comment
40 *
41 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
42 * @version $Revision: 71554 $
43 */
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 // First we must get a reference to a scheduler
60 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 // allocate instance of endpoint to figure out its endpoint interface
98 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 }