>
> Could anyone post some code showing how to implement this?
yes, of course:
first, the usage example:
package ch.bmw.jobscheduler.test;
__________________________________________________________________________
import java.util.Date;
import junit.framework.TestCase;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import com.javaforge.honeycomb.RegistryAccessor;
public class JobSchedulerServiceTest extends TestCase {
private Registry reg;
private SchedulerFactory sf;
protected void setUp() throws Exception {
reg = RegistryBuilder.constructDefaultRegistry();
((RegistryAccessor)reg.getService(RegistryAccessor.class))
.setRegistry(reg);
}
public void testSimple() throws SchedulerException,
InterruptedException {
Scheduler s = (Scheduler) reg.getService( Scheduler.class );
JobDetail jobDetail = new JobDetail("myJob",
null,
Job.class, true, false, false );
Trigger trigger = TriggerUtils.makeSecondlyTrigger(3);
trigger.setName( "testTrigger" );
trigger.setStartTime( new Date() );
trigger.setVolatility( true );
s.scheduleJob( jobDetail, trigger );
Thread.sleep(10000);
}
}
____________________________________________________________________________
_____________________________
the hivemodule looks like this (autowiring used):
____________________________________________________________________________
___
<module id="scheduler" version="1.0.0" package="ch.bmw.jobscheduler">
Defines services for the JobScheduler-Application.
<service-point interface="org.quartz.Scheduler" id="Scheduler">
<invoke-factory service-id="SchedulerFactoryForHivemind"
model="singleton"/>
</service-point>
<service-point id="SchedulerFactoryForHivemind"
parameters-occurs="0..1"
interface="org.apache.hivemind.ServiceImplementationFactory">
A wrapper around the quartz standard SchedulerFactory (see below).
Provides schedulers with a hivemind-aware JobFactory and starts
them.
<invoke-factory model="singleton">
<construct class="SchedulerFactoryForHivemind"/>
</invoke-factory>
<interceptor service-id="hivemind.LoggingInterceptor"/>
</service-point>
<service-point id="SchedulerFactory"
interface="org.quartz.SchedulerFactory">
A raw quartz SchedulerFactory as configured in the config-file
given.
Usually wrapped by a SchedulerFactoryForHiveMind - see above.
<invoke-factory>
<construct class="org.quartz.impl.StdSchedulerFactory" >
<string>${quartz.config.file}</string>
</construct>
</invoke-factory>
<interceptor service-id="hivemind.LoggingInterceptor"/>
</service-point>
<contribution configuration-id="hivemind.FactoryDefaults">
<default symbol="quartz.config.file" value="quartz.properties"/>
</contribution>
</module>
____________________________________________________________________________
___________
package ch.bmw.jobscheduler;
import java.util.Iterator;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Registry;
import org.apache.hivemind.ServiceImplementationFactory;
import org.apache.hivemind.ServiceImplementationFactoryParameters;
import org.apache.hivemind.events.RegistryShutdownListener;
import org.apache.hivemind.internal.RegistryInfrastructure;
import org.apache.hivemind.internal.ser.ServiceSerializationHelper;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import com.javaforge.honeycomb.RegistryAccessor;
public class SchedulerFactoryForHivemind implements
ServiceImplementationFactory, RegistryShutdownListener{
private SchedulerFactory schedulerFactory;
public SchedulerFactoryForHivemind( SchedulerFactory sf ) {
schedulerFactory = sf;
}
public Object createCoreServiceImplementation(
ServiceImplementationFactoryParameters
factoryParameters) {
try {
if ( !
factoryParameters.getServiceInterface().equals( Scheduler.class ) )
throw new ApplicationRuntimeException("this
factory can't make "+ factoryParameters.getServiceInterface() );
String name = (String)
factoryParameters.getFirstParameter();
Scheduler s = name == null ?
schedulerFactory.getScheduler()
: schedulerFactory.getScheduler(name);
RegistryInfrastructure reg =
(RegistryInfrastructure)
ServiceSerializationHelper.getServiceSerializationSupport();
s.setJobFactory( new HivemindServiceJobFactory( reg
) );
s.start();
return s;
} catch (SchedulerException e) {
throw new ApplicationRuntimeException( e );
}
}
public void registryDidShutdown() {
try {
for (Iterator it =
schedulerFactory.getAllSchedulers().iterator(); it.hasNext();) {
Scheduler s = (Scheduler) it.next();
s.shutdown();
}
} catch (SchedulerException e) {
throw new ApplicationRuntimeException(e);
}
}
}
____________________________________________________________________________
_______________________________
package ch.bmw.jobscheduler;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryInfrastructureImpl;
import org.apache.hivemind.internal.RegistryInfrastructure;
import org.apache.hivemind.internal.ser.ServiceSerializationHelper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
/**
* A job Factory producing jobs that wrap/are HiveMind services.
*
* @author Marcus Schulte
*
*/
public class HivemindServiceJobFactory implements JobFactory {
public static final String
SERVICE_NAME_KEY="org.apache.hivemind.ServiceName";
private RegistryInfrastructure reg;
private class HivemindServiceInvocationJobWrapper implements Job {
private Job wrappedJob;
public HivemindServiceInvocationJobWrapper( Job j ) {
wrappedJob = j;
}
public HivemindServiceInvocationJobWrapper( Runnable r ) {
wrappedJob = new RunnableAdapter(r);
}
public void execute(JobExecutionContext context) throws
JobExecutionException {
try {
reg.setupThread();
wrappedJob.execute( context );
} catch ( JobExecutionException e ) {
throw e;
} catch ( RuntimeException e ) {
throw new JobExecutionException( e );
} finally {
reg.cleanupThread();
}
}
}
// adapts a Runnable to a Job
private static class RunnableAdapter implements Job {
private Runnable runnable;
public RunnableAdapter(Runnable runnable) {
this.runnable = runnable;
}
public void execute(JobExecutionContext context) throws
JobExecutionException {
runnable.run();
}
}
public HivemindServiceJobFactory( RegistryInfrastructure reg ) {
this.reg = reg;
}
public Job newJob(TriggerFiredBundle bundle) throws
SchedulerException {
Class serviceIf = bundle.getJobDetail().getJobClass();
String serviceName = (String)
bundle.getJobDetail().getJobDataMap().get( SERVICE_NAME_KEY );
Object svc;
svc = (serviceName != null ?
reg.getService(
serviceName, serviceIf, null )
: reg.getService( serviceIf,
null ) );
if ( svc instanceof Job )
return new HivemindServiceInvocationJobWrapper(
(Job) svc );
else if ( svc instanceof Runnable )
return new HivemindServiceInvocationJobWrapper(
(Runnable)svc );
else
throw new SchedulerException("Service
"+serviceName+" does not implement" +
" one of org.quartz.Job, java.lang.Runnable:
"+serviceIf.getCanonicalName() );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]