[jboss-user] [EJB 3.0] - Re: EJB 2.1 problems with EJB 3.0

ola.hamfors@diabol.se do-not-reply at jboss.com
Thu Nov 23 05:55:02 EST 2006


I have written a EJB3InvokerJob.

----------------- EJB3Invoker.java --------------------------
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;

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

import org.apache.log4j.Logger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
/**
 * 
 * @author ola.hamfors at diabol.se
 *
 */
public class EJB3InvokerJob implements StatefulJob{
	@SuppressWarnings("unused")
	private final static String RCS_ID = "$Id: EJB3InvokerJob.java,v 1.6 2006/08/18 08:04:37 STNO Exp $";

	public static final String EJB_JNDI_NAME_KEY = "ejb";

	public static final String EJB_METHOD_KEY = "method";

	public static final String EJB_INTERFACE_CLASS = "interfaceClass";

	public static final String EJB_ARG_TYPES_KEY = "argTypes";

	public static final String EJB_ARGS_KEY = "args";

	public static final String INITIAL_CONTEXT_FACTORY = "java.naming.factory.initial";

	public static final String PROVIDER_URL = "java.naming.provider.url";

	Logger logger = Logger.getLogger(getClass().getName());
	
	public EJB3InvokerJob() {
		super();
	}

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobDetail detail = context.getJobDetail();
		JobDataMap dataMap = detail.getJobDataMap();
		String ejb = dataMap.getString(EJB_JNDI_NAME_KEY);
		String method = dataMap.getString(EJB_METHOD_KEY);
		String interfaceClass = dataMap.getString(EJB_INTERFACE_CLASS);
		logger.debug("Trying to execute {ejb=" + ejb + ",method="
				+ method + ",intefaceClass=" + interfaceClass + "}");
		Object[] arguments = (Object[]) dataMap.get(EJB_ARGS_KEY);
		if (arguments == null) {
			arguments = new Object[0];
		}
		if (ejb == null) {
			throw new JobExecutionException();
		}
		InitialContext jndiContext = null;

		try {
			jndiContext = getInitialContext(dataMap);
		} catch (NamingException ne) {
			throw new JobExecutionException(ne);
		}
		Object value = null;
		try {
			value = jndiContext.lookup(ejb);
		} catch (NamingException ne) {
			throw new JobExecutionException(ne);
		}
		Class beanClass;
		try {
			beanClass = Class.forName(interfaceClass);
		} catch (ClassNotFoundException e) {
			throw new JobExecutionException(e);
		}

		Method methodExecute = null;
		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 = arguments.getClass();
			}
		}
		// get all interfaces
		Class[] interfaces = beanClass.getInterfaces();
		for (int i = 0; i < interfaces.length; i++) {
			try {
				// try to find the method and use the first interface if we do
				methodExecute = interfaces.getDeclaredMethod(method,
						argTypes);
				break;
			} catch (NoSuchMethodException nsme) {
				// do nothing
			}
			throw new JobExecutionException("No interface with method "
					+ method + " declaired. Your Bean {" + beanClass.getName()
					+ "} has to implement the Local interface");
		}

		try {
			methodExecute.invoke(value, arguments);
		} catch (IllegalAccessException iae) {
			throw new JobExecutionException(iae);
		} catch (InvocationTargetException ite) {
			throw new JobExecutionException(ite);
		}
	}

	@SuppressWarnings("unchecked")
	private InitialContext getInitialContext(JobDataMap jobDataMap)
			throws NamingException {
		Hashtable params = new Hashtable(2);
		String initialContextFactory = jobDataMap
				.getString(INITIAL_CONTEXT_FACTORY);
		if (initialContextFactory != null) {
			params.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
		}
		String providerUrl = jobDataMap.getString(PROVIDER_URL);
		if (providerUrl != null) {
			params.put(Context.PROVIDER_URL, providerUrl);
		}
		return new InitialContext(params);
	}

}
-----------------------------

---------------- CronJobListener ---------------

import java.util.Date;

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

import org.apache.log4j.Logger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

import com.omxgroup.cds.entity.batch.CronJob;
import com.omxgroup.cds.entity.util.JndiNameCreator;
import com.omxgroup.cds.server.api.CronJobApi;
import com.omxgroup.cds.server.api.CronJobApiBean;

/**
 * A CronJobListener that updates the lastexecutiondate of the cronjob
 * 
 * @author ola.hamfors at diabol.se
 * 
 */
public class CronJobListener implements JobListener {
	@SuppressWarnings("unused")
	private final static String RCS_ID = "$Id: CronJobListener.java,v 1.6 2006/11/22 18:02:25 JAJH Exp $";

	public static final String CDS_JOB_LISTENER_NAME = "CDS CronJob Listener";

	public static final String CDS_CRON_JOB_ID = "cronjobid";

	public String getName() {
		return CDS_JOB_LISTENER_NAME;
	}
	
	private Logger logger = Logger.getLogger(getClass().getName());


	public void jobToBeExecuted(JobExecutionContext ctx) {
		// do nothing
	}

	public void jobExecutionVetoed(JobExecutionContext ctx) {
		// do nothing
	}

	public void jobWasExecuted(JobExecutionContext ctx,
			JobExecutionException exception) {
		JobDetail jobDetail = ctx.getJobDetail();
		JobDataMap map = jobDetail.getJobDataMap();
		long cronJobId = map.getLong(CDS_CRON_JOB_ID);
		InitialContext jndiCtx;
		try {
			jndiCtx = new InitialContext();
			CronJobApi cronJobApi = (CronJobApi) jndiCtx
					.lookup(JndiNameCreator.getLocalJndiName(CronJobApiBean.class));
			CronJob job = cronJobApi.getCronJob(cronJobId);
			if (job != null) {
				job.setLastExecution(new Date());
				cronJobApi.update(job);
			}
		} catch (NamingException e) {
			logger.warn("Couldn't lookup CronJobApiBean", e);
		}

	}

}
-----------------------------

And this is how I use it.

public String startAllCronJobs() throws SchedulerException {

		List list = cronJobApi.getAllCronJobs();
		logger.info("Starting  " + list.size() + " "
				+ ((list.size() == 1) ? "cronjob" : "cronjobs"));
		Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
		sched.addJobListener(new CronJobListener());
		for (CronJob job : list) {

			logger.info("Starting job " + job);
			CronJobDetail jd = new CronJobDetail(job.toString(), job
					.getDescription(), EJB3InvokerJob.class);
			jd.addJobListener(CronJobListener.CDS_JOB_LISTENER_NAME);
			Class beanClass = null;
			try {
				beanClass = Class.forName(job.getQualifiedBeanClassName());
			} catch (ClassNotFoundException e) {
				logger.error("Cron Job class "
						+ job.getQualifiedBeanClassName() + "not found", e);
				continue;
			}
			String jndiName = JndiNameCreator.getLocalJndiName(beanClass);
			jd.getJobDataMap().put(EJB3InvokerJob.EJB_JNDI_NAME_KEY, jndiName);
			jd.getJobDataMap().put(EJB3InvokerJob.EJB_METHOD_KEY,
					job.getMethod());
			jd.getJobDataMap().put(EJB3InvokerJob.EJB_INTERFACE_CLASS,
					job.getQualifiedBeanClassName());
			Object[] jdArgs = new Object[0];
			jd.getJobDataMap().put(EJB3InvokerJob.EJB_ARGS_KEY, jdArgs);
			jd.getJobDataMap()
					.put(CronJobListener.CDS_CRON_JOB_ID, job.getId());

			String jobName = job.toString();
			String group = "CDS Quartz Group";

			CronTrigger cronTrigger = new CronTrigger(jobName, group);
			try {
				cronTrigger.setCronExpression(job.getCronExpression());
				// cronTrigger.addTriggerListener(CronJobTriggerListener.CRON_TIGGER_NAME);
			} catch (ParseException e) {
				logger.warn("Invalid syntax on cron expression", e);
				continue;
			}
			sched.scheduleJob(jd, cronTrigger);
			logger.info("Job " + job + " scheduled");
		}

		sched.start();
		return "Started";
	}




View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988117#3988117

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988117



More information about the jboss-user mailing list