001: package org.jbpm.job.executor;
002:
003: import org.jbpm.job.Job;
004: import org.jbpm.JbpmConfiguration;
005: import org.jbpm.JbpmContext;
006: import org.jbpm.context.exe.ContextInstance;
007: import org.jbpm.graph.exe.ProcessInstance;
008: import org.jbpm.db.JobSession;
009: import org.outerj.daisy.workflow.serverimpl.DaisyConnectionProvider;
010: import org.outerj.daisy.workflow.jbpm_util.Mailer;
011: import org.outerj.daisy.repository.Repository;
012: import org.outerj.daisy.repository.RepositoryManager;
013:
014: import javax.sql.DataSource;
015: import java.io.StringWriter;
016: import java.io.PrintWriter;
017:
018: //
019: // Daisy note: this is a custom version of jBPM's JobExecutorThread
020: // It adds:
021: // - making the datasource available
022: // - setting the actorId on the jbpmContext
023: // - making some Daisy objects available to action through transient process variables
024: //
025: // See the sections marked with *** daisy change ***
026: //
027:
028: public class DaisyJobExecutorThread extends JobExecutorThread {
029: private DataSource dataSource;
030: private Repository repository;
031: private RepositoryManager repositoryManager;
032: private Mailer mailer;
033:
034: public DaisyJobExecutorThread(String name, JobExecutor jobExecutor,
035: JbpmConfiguration jbpmConfiguration, int idleInterval,
036: int maxIdleInterval, long maxLockTime, int maxHistory,
037: DataSource dataSource, Repository repository,
038: RepositoryManager repositoryManager, Mailer mailer) {
039: super (name, jobExecutor, jbpmConfiguration, idleInterval,
040: maxIdleInterval, maxLockTime, maxHistory);
041:
042: this .dataSource = dataSource;
043: this .repository = repository;
044: this .repositoryManager = repositoryManager;
045: this .mailer = mailer;
046: }
047:
048: public void run() {
049: // *** daisy change ***
050: DaisyConnectionProvider.DATASOURCE.set(dataSource);
051: super .run();
052: }
053:
054: protected void executeJob(Job job) {
055: JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
056: try {
057: // *** daisy change ***
058: jbpmContext.setActorId(String.valueOf(repository
059: .getUserId()));
060: JobSession jobSession = jbpmContext.getJobSession();
061: job = jobSession.loadJob(job.getId());
062:
063: // *** daisy change ***
064: ProcessInstance processInstance = job.getProcessInstance();
065: ContextInstance contextInstance = processInstance
066: .getContextInstance();
067: contextInstance.setTransientVariable("repository",
068: repository);
069: contextInstance.setTransientVariable("wfRepository",
070: repository);
071: contextInstance.setTransientVariable("repositoryManager",
072: repositoryManager);
073: contextInstance.setTransientVariable("mailer", mailer);
074:
075: try {
076: // log.debug("executing job "+job);
077: if (job.execute(jbpmContext)) {
078: jobSession.deleteJob(job);
079: }
080:
081: } catch (Exception e) {
082: // log.debug("exception while executing '"+job+"'", e);
083: StringWriter sw = new StringWriter();
084: e.printStackTrace(new PrintWriter(sw));
085: job.setException(sw.toString());
086: job.setRetries(job.getRetries() - 1);
087: }
088:
089: // if this job is locked too long
090: long totalLockTimeInMillis = System.currentTimeMillis()
091: - job.getLockTime().getTime();
092: if (totalLockTimeInMillis > maxLockTime) {
093: jbpmContext.setRollbackOnly();
094: }
095:
096: } finally {
097: try {
098: jbpmContext.close();
099: } catch (RuntimeException e) {
100: // log.error("problem committing job execution transaction", e);
101: throw e;
102: }
103: }
104: }
105:
106: }
|