All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.quartz.jobs.ee.ejb.EJB3InvokerJob Maven / Gradle / Ivy

There is a newer version: 2.5.0-rc1
Show newest version
package org.quartz.jobs.ee.ejb;

import java.lang.reflect.InvocationTargetException;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 

* A Job that invokes a method on an EJB3. *

* *

* Expects the properties corresponding to the following keys to be in the * JobDataMap when it executes: *

    *
  • EJB_JNDI_NAME_KEY- the JNDI name (location) of the EJB's * home interface.
  • *
  • EJB_METHOD_KEY- the name of the method to invoke on the EJB. *
  • *
  • EJB_ARGS_KEY- an Object[] of the args to pass to the method * (optional, if left out, there are no arguments).
  • *
  • EJB_ARG_TYPES_KEY- an Class[] of the types of the args to * pass to the method (optional, if left out, the types will be derived by * calling getClass() on each of the arguments).
  • *
*
* The following keys can also be used at need: *
    *
  • INITIAL_CONTEXT_FACTORY - the context factory used to build * the context.
  • *
  • PROVIDER_URL - the name of the environment property for * specifying configuration information for the service provider to use.
  • *
*

* *

* The result of the EJB method invocation will be available to * Job/TriggerListeners via * {@link org.quartz.JobExecutionContext#getResult()}. *

* * @author hhuynh * @see {@link org.quartz.jobs.ee.ejb.EJBInvokerJob} */ public class EJB3InvokerJob extends EJBInvokerJob { @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap dataMap = context.getMergedJobDataMap(); String ejb = dataMap.getString(EJB_JNDI_NAME_KEY); String method = dataMap.getString(EJB_METHOD_KEY); Object[] arguments = (Object[]) dataMap.get(EJB_ARGS_KEY); if (arguments == null) { arguments = new Object[0]; } if (ejb == null) { throw new JobExecutionException("must specify EJB_JNDI_NAME_KEY"); } if (method == null) { throw new JobExecutionException("must specify EJB_METHOD_KEY"); } InitialContext jndiContext = null; Object value = null; try { try { jndiContext = getInitialContext(dataMap); value = jndiContext.lookup(ejb); } catch (NamingException ne) { throw new JobExecutionException(ne); } Class[] argTypes = (Class[]) dataMap.get(EJB_ARG_TYPES_KEY); if (argTypes == null) { argTypes = new Class[arguments.length]; for (int i = 0; i < arguments.length; i++) { argTypes[i] = arguments[i].getClass(); } } try { Object returnValue = value.getClass() .getMethod(method, argTypes).invoke(value, arguments); context.setResult(returnValue); } catch (IllegalAccessException iae) { throw new JobExecutionException(iae); } catch (InvocationTargetException ite) { throw new JobExecutionException(ite.getTargetException()); } catch (NoSuchMethodException nsme) { throw new JobExecutionException(nsme); } } finally { if (jndiContext != null) { try { jndiContext.close(); } catch (Exception e) { // ignored } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy