KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > quartz > inflow > QuartzResourceAdapter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * 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 JavaDoc;
29 import javax.resource.spi.BootstrapContext JavaDoc;
30 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
31 import javax.resource.spi.ActivationSpec JavaDoc;
32 import javax.resource.spi.work.WorkManager JavaDoc;
33 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
34 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
35 import javax.resource.ResourceException JavaDoc;
36 import javax.transaction.xa.XAResource JavaDoc;
37
38 /**
39  * Comment
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 56863 $
43  */

44 public class QuartzResourceAdapter implements ResourceAdapter JavaDoc
45 {
46    private static Logger log = Logger.getLogger(QuartzResourceAdapter.class);
47
48    private static final ThreadLocal JavaDoc holder = new ThreadLocal JavaDoc();
49
50    private Scheduler sched;
51
52    public static WorkManager JavaDoc getConfigTimeWorkManager() {
53       return (WorkManager JavaDoc) holder.get();
54    }
55
56    public void start(BootstrapContext JavaDoc ctx) throws ResourceAdapterInternalException JavaDoc
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 JavaDoc(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 JavaDoc(e);
87       }
88    }
89
90    public void endpointActivation(MessageEndpointFactory JavaDoc endpointFactory,
91                                   ActivationSpec JavaDoc spec)
92       throws ResourceException JavaDoc
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 JavaDoc clazz = QuartzJob.class;
99       MessageEndpoint JavaDoc 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 JavaDoc e)
113       {
114          log.error(e);
115          throw new ResourceException JavaDoc(e);
116       }
117    }
118
119    public void endpointDeactivation(MessageEndpointFactory JavaDoc endpointFactory,
120                                     ActivationSpec JavaDoc 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 JavaDoc(e);
131       }
132    }
133
134    public XAResource JavaDoc[] getXAResources(ActivationSpec JavaDoc[] specs) throws ResourceException JavaDoc
135    {
136       return new XAResource JavaDoc[0];
137    }
138
139 }
140
Popular Tags