Home » synapse-1.2-src » org.apache.synapse.startup.quartz » [javadoc | source]

    1   package org.apache.synapse.startup.quartz;
    2   
    3   import java.util.Iterator;
    4   import java.util.Set;
    5   
    6   import org.apache.axiom.om.OMElement;
    7   import org.apache.commons.logging.Log;
    8   import org.apache.commons.logging.LogFactory;
    9   import org.apache.synapse.ManagedLifecycle;
   10   import org.apache.synapse.startup.Task;
   11   import org.apache.synapse.config.xml.PropertyHelper;
   12   import org.apache.synapse.core.SynapseEnvironment;
   13   import org.quartz.Job;
   14   import org.quartz.JobDataMap;
   15   import org.quartz.JobExecutionContext;
   16   import org.quartz.JobExecutionException;
   17   
   18   public class SimpleQuartzJob implements Job {
   19       public static final String
   20           SYNAPSE_ENVIRONMENT = "SynapseEnvironment",
   21           CLASSNAME = "ClassName",
   22           PROPERTIES = "Properties";
   23       private static final Log log = LogFactory.getLog(SimpleQuartzJob.class);
   24   
   25       public void execute(JobExecutionContext ctx) throws JobExecutionException {
   26   
   27           log.debug("Executing task : " + ctx.getJobDetail().getFullName());
   28           JobDataMap jdm = ctx.getMergedJobDataMap();
   29           String jobClassName = (String) jdm.get(CLASSNAME);
   30           if (jobClassName == null) {
   31               handleException("No " + CLASSNAME + " in JobDetails");
   32           }
   33   
   34           Task task = null;
   35           try {
   36               task = (Task) getClass().getClassLoader().loadClass(jobClassName).newInstance();
   37           } catch (Exception e) {
   38               handleException("Cannot instantiate task : " + jobClassName, e);
   39           }
   40   
   41           Set properties = (Set) jdm.get(PROPERTIES);
   42           Iterator it = properties.iterator();
   43           while (it.hasNext()) {
   44               OMElement prop = (OMElement) it.next();
   45               log.debug("Found Property : " + prop.toString());
   46               PropertyHelper.setStaticProperty(prop, task);
   47           }
   48   
   49           SynapseEnvironment se = (SynapseEnvironment) jdm.get("SynapseEnvironment");
   50           if (task instanceof ManagedLifecycle) {
   51               if (se != null) {
   52                   ((ManagedLifecycle) task).init(se);
   53               }
   54           }
   55   
   56           if (se.isInitialized()) {
   57               task.execute();
   58           }
   59       }
   60   
   61       private void handleException(String msg) throws JobExecutionException {
   62           log.error(msg);
   63           throw new JobExecutionException(msg);
   64       }
   65   
   66       private void handleException(String msg, Exception e) throws JobExecutionException {
   67           log.error(msg, e);
   68           throw new JobExecutionException(msg, e);
   69       }
   70   
   71   }

Home » synapse-1.2-src » org.apache.synapse.startup.quartz » [javadoc | source]