Package org.jbpm.jobexecutor

Source Code of org.jbpm.jobexecutor.AcquireJobs

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.jobexecutor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import org.jbpm.client.Command;
import org.jbpm.env.Environment;
import org.jbpm.env.session.Job;

/**
* @author Tom Baeyens
*/
public class AcquireJobs implements Command {

  private static final long serialVersionUID = 1L;

  private static final Logger log = Logger.getLogger(AcquireJobs.class.getName());
  private static final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss,SSS");
 
  JobExecutor jobExecutor;

  public AcquireJobs(JobExecutor jobExecutor) {
    this.jobExecutor = jobExecutor;
  }

  public Object execute(Environment environment) throws Exception {
    Collection<Long> acquiredJobDbids = new ArrayList<Long>();

    Collection<Job> acquiredJobs = new ArrayList<Job>();
   
    JobSession jobService = environment.get(JobSession.class);
    log.fine("start querying first acquirable job...");
    Job job = jobService.getFirstAcquirableJob();
    if (job!=null) {
      if (job.isExclusive()) {
        log.finest("exclusive acquirable job found ("+job+"). querying for other exclusive jobs to lock them all in one tx...");
        List<Job> otherExclusiveJobs = jobService.findExclusiveJobs(job.getProcessInstance());
        acquiredJobs.addAll(otherExclusiveJobs);
      } else {
        acquiredJobs.add(job);
      }

      for (Job acquiredJob: acquiredJobs) {
        long lockExpirationTime = System.currentTimeMillis()+jobExecutor.getLockMillis();
        log.finest("trying to obtain a lock for '"+job+"' with exp "+timeFormat.format(new Date(lockExpirationTime)));
        acquiredJob.setLockExpirationTime(new Date(lockExpirationTime));
        acquiredJob.setLockOwner(jobExecutor.getName());
        acquiredJobDbids.add(acquiredJob.getDbid());
      }

    } else {
      log.finest("no acquirable jobs in job table");
    }
   
    log.fine("locking jobs "+acquiredJobDbids);
   
    return acquiredJobDbids;
  }

}
TOP

Related Classes of org.jbpm.jobexecutor.AcquireJobs

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.