001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.quartz.inflow;
023:
024: import org.jboss.logging.Logger;
025: import org.quartz.*;
026: import org.quartz.impl.StdSchedulerFactory;
027:
028: import javax.resource.spi.ResourceAdapter;
029: import javax.resource.spi.BootstrapContext;
030: import javax.resource.spi.ResourceAdapterInternalException;
031: import javax.resource.spi.ActivationSpec;
032: import javax.resource.spi.endpoint.MessageEndpointFactory;
033: import javax.resource.spi.endpoint.MessageEndpoint;
034: import javax.resource.ResourceException;
035: import javax.transaction.xa.XAResource;
036:
037: /**
038: * The QuartzResourceAdapter.
039: *
040: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
041: * @version $Revision: 57189 $
042: */
043: public class QuartzResourceAdapter implements ResourceAdapter {
044: private static Logger log = Logger
045: .getLogger(QuartzResourceAdapter.class);
046:
047: private Scheduler sched;
048:
049: public void start(BootstrapContext ctx)
050: throws ResourceAdapterInternalException {
051:
052: log.info("start quartz!!!");
053:
054: SchedulerFactory sf = new StdSchedulerFactory();
055: try {
056: sched = sf.getScheduler();
057: sched.start();
058: } catch (SchedulerException e) {
059: throw new ResourceAdapterInternalException(e);
060: }
061: }
062:
063: public void stop() {
064: log.debug("stop");
065: try {
066: sched.shutdown(true);
067: } catch (SchedulerException e) {
068: throw new RuntimeException(e);
069: }
070: }
071:
072: public void endpointActivation(
073: MessageEndpointFactory endpointFactory, ActivationSpec spec)
074: throws ResourceException {
075: log.debug("endpointActivation, spec=" + spec);
076: QuartzActivationSpec quartzSpec = (QuartzActivationSpec) spec;
077:
078: // allocate instance of endpoint to figure out its endpoint interface
079: Class clazz = QuartzJob.class;
080: MessageEndpoint tmpMe = endpointFactory.createEndpoint(null);
081: if (tmpMe instanceof StatefulJob)
082: clazz = StatefulQuartzJob.class;
083: tmpMe.release();
084:
085: try {
086: JobDetail jobDetail = new JobDetail(
087: quartzSpec.getJobName(), quartzSpec.getJobGroup(),
088: clazz, quartzSpec.getVolatility(), quartzSpec
089: .getDurable(), quartzSpec.getRecoverable());
090: jobDetail.getJobDataMap().setAllowsTransientData(true);
091: jobDetail.getJobDataMap().put("endpointFactory",
092: endpointFactory);
093: log.debug("adding job: " + quartzSpec);
094: CronTrigger trigger = new CronTrigger(quartzSpec
095: .getTriggerName(), quartzSpec.getTriggerGroup(),
096: quartzSpec.getCronTrigger());
097: sched.scheduleJob(jobDetail, trigger);
098: } catch (Exception e) {
099: log.error(e);
100: throw new ResourceException(e);
101: }
102: }
103:
104: public void endpointDeactivation(
105: MessageEndpointFactory endpointFactory, ActivationSpec spec) {
106: QuartzActivationSpec quartzSpec = (QuartzActivationSpec) spec;
107: try {
108: log.debug("****endpointDeactivation: " + quartzSpec);
109: sched.deleteJob(quartzSpec.getJobName(), quartzSpec
110: .getJobGroup());
111: } catch (SchedulerException e) {
112: throw new RuntimeException(e);
113: }
114: }
115:
116: public XAResource[] getXAResources(ActivationSpec[] specs)
117: throws ResourceException {
118: return new XAResource[0];
119: }
120:
121: }
|