users@glassfish.java.net

Re: JEE5 With Quartz

From: <glassfish_at_javadesktop.org>
Date: Wed, 28 May 2008 13:31:46 PDT

When enabled with the right security policy, you can set Quartz up as an easy to use JNDI-based resource injectable into your EJBs or available as a remote resource.

Example:

Assumption. You have an enterprise app that contains some EJBs, and perhaps some jars that implement specific jobs that you'd like to schedule with Quartz (or the jobs exist in their own jar somewhere in the app server space). You want your instance of Quartz to be a well-behaved (starts when needed, and goes away when you're shutting down.) You'd also like to be able to use the scheduler easily from within your EJBs.

1) Create a suitable quartz.properties file and put it in your ${com.sun.aas.instanceRoot}/config directory (the config directory of your app server
instance. This is usually ${AS_INSTALL}/domains/domain/config.

2) Create a Quartz Scheduler JNDI Factory ex:

...
public class SchedulerFactory implements javax.naming.spi.ObjectFactory {
    private static Logger log = Logger.getLogger(SchedulerFactory.class.getName());
    private static Scheduler scheduler;
    static {
        try {
            StdSchedulerFactory factory = new StdSchedulerFactory("quartz.properties");
            scheduler = factory.getScheduler();
            scheduler.start();
        } catch (SchedulerException se) {
            log.severe("Cannot initialize job scheduler : \n"+se.getMessage());
            scheduler = null;
        }
    }

    public SchedulerFactory() { ; } // Need to have public no-argument constructor

    public static Scheduler getInstance() {
        return scheduler;
    }

    public Object getObjectInstance(Object arg0, Name arg1, Context arg2, Hashtable<?, ?> arg3) throws Exception {
        return getInstance();
    }

3) Add the quartz-all-1.6.0.jar file to your ${com.sun.aas.instanceRoot}/lib directory.

4) Add the following to your ${com.sun.aas.instanceRoot}/config/server.policy file to let the quartz classes access and instantiate your job classes, also to enable clean shutdown behavior. (modified for quartz-all jar from http://javaevangelist.blogspot.com/2007/10/quartz-job-scheduler-on-glassfish.html)

///////////////////////////////////////////////////////////////////////////////////////////////
// Quartz Scheduler
///////////////////////////////////////////////////////////////////////////////////////////////
grant codeBase "file:${com.sun.aas.instanceRoot}/lib/quartz-all-1.6.0.jar" {
permission java.lang.RuntimePermission "getClassLoader";
// Required for ShutdownHookPlugin
permission java.lang.RuntimePermission "shutdownHooks";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission javax.management.MBeanPermission "com.sun.enterprise.admin.runtime.BaseRuntimeMBean#", "registerMBean";
permission javax.management.MBeanServerPermission "findMBeanServer";
};

5) Create a custom JNDI resouce using the Glassfish admin console. The type of object returned is org.quartz.Scheduler, and the factory is the class of your factory defined above. As far as a name, be clever, call it something easy to remember like "JobScheduler"

6) After a restart of GlassFish, you are ready to go.

You should now be able to access the scheduler via its JNDI reference like this:

Example 1: ------------------------------------------------------------------------------------------------------------------
From an EJB or other JEE artifact within the server:

@Resource(mappedName="JobScheduler")
Scheduler jobScheduler;

List<JobExecutionContext> jobs = jobScheduler.getCurrentlyExecutingJobs();
if (jobs != null && !jobs.isEmpty())
    for (JobExecutionContext job : jobs)
        System.out.println("Job : "+job.getName());
else
    System.out.println("No Jobs Found");

Example 2: ------------------------------------------------------------------------------------------------------------------
From a standalone application outside of the server:

Scheduler jobScheduler = null;
Properties config = getJNDIConfig(); // Load your properties from somewhere if not in jndi.properties.
InitialContext ctx = new InitialContext(config);

if (ctx != null)
    jobScheduler = (Scheduler) ctx.lookup("JobScheduler");

...use the scheduler

Hope this helps - dB
[Message sent by forum member 'dbostwick' (dbostwick)]

http://forums.java.net/jive/thread.jspa?messageID=277119