JobExecutor.java 7.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.activiti5.engine.impl.jobexecutor;

import java.util.Date;
import java.util.List;
import java.util.UUID;

20
import org.activiti.engine.runtime.ClockReader;
21 22 23 24 25 26 27 28 29
import org.activiti5.engine.impl.cmd.AcquireJobsCmd;
import org.activiti5.engine.impl.interceptor.Command;
import org.activiti5.engine.impl.interceptor.CommandExecutor;
import org.activiti5.engine.impl.persistence.entity.JobEntity;
import org.activiti5.engine.runtime.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
30
 * <p>Interface to the work management component of activiti.</p>
31
 * 
32 33
 * <p>This component is responsible for performing all background work 
 * ({@link Job Jobs}) scheduled by activiti.</p>
34
 * 
35 36 37 38
 * <p>You should generally only have one of these per Activiti instance (process 
 * engine) in a JVM.
 * In clustered situations, you can have multiple of these running against the
 * same queue + pending job list.</p>
39 40 41 42 43
 * 
 * @author Daniel Meyer
 * @author Joram Barrez
 */
public abstract class JobExecutor {
44
  
45 46
  private static Logger log = LoggerFactory.getLogger(JobExecutor.class);

47
  protected String name = "JobExecutor["+getClass().getName()+"]";
48 49 50 51 52
  protected CommandExecutor commandExecutor;
  protected Command<AcquiredJobs> acquireJobsCmd;
  protected AcquireJobsRunnable acquireJobsRunnable;
  protected RejectedJobsHandler rejectedJobsHandler;
  protected Thread jobAcquisitionThread;
53
  
54 55 56 57
  protected boolean isAutoActivate = false;
  protected boolean isActive = false;

  /**
58 59 60
   * To avoid deadlocks, the default for this is one.
   * This way, in a clustered setup, multiple job executors can acquire jobs
   * without creating a deadlock due to fetching multiple jobs at once and
61 62
   * trying to lock them all at once.
   * 
63 64
   * In a non-clustered setup, this setting can be changed to any value > 0
   * without problems.
65
   * 
J
Joram Barrez 已提交
66
   * See https://activiti.atlassian.net/browse/ACT-1879 for more information.
67 68 69 70 71 72
   */
  protected int maxJobsPerAcquisition = 1;
  protected long waitTimeInMillis = 5000L;
  protected String lockOwner = UUID.randomUUID().toString();
  protected int lockTimeInMillis = 5 * 60 * 1000;
  protected ClockReader clockReader;
73
  
74 75 76 77 78 79
  /** Starts the job executor */
  public void start() {
    if (isActive) {
      return;
    }
    log.info("Starting up the JobExecutor[{}].", getClass().getName());
80
    ensureInitialization();    
81 82 83
    startExecutingJobs();
    isActive = true;
  }
84
  
85 86 87 88 89 90 91 92
  /** Shuts down the whole job executor */
  public synchronized void shutdown() {
    if (!isActive) {
      return;
    }
    log.info("Shutting down the JobExecutor[{}].", getClass().getName());
    acquireJobsRunnable.stop();
    stopExecutingJobs();
93
    ensureCleanup();   
94 95
    isActive = false;
  }
96 97 98 99 100 101 102 103 104 105 106
  
  /** Possibility to ensure everything is nicely initialized before starting the threads */
  protected void ensureInitialization() { 
  	if (acquireJobsCmd == null) {
  		acquireJobsCmd = new AcquireJobsCmd(this);
  	}
  	if (acquireJobsRunnable == null) {
  		acquireJobsRunnable = new AcquireJobsRunnableImpl(this);
  	}
  }
  
107
  /** Possibility to clean up resources */
108
  protected void ensureCleanup() {  
109
    acquireJobsCmd = null;
110
    acquireJobsRunnable = null;  
111
  }
112 113 114 115 116
  
  /** 
   * Called when a new job was added by the process engine to which
   * this job executor belongs. This is a hint, that for example
   * the acquiring needs to start again when it would be sleeping.
117 118
   */
  public void jobWasAdded() {
119
    if(isActive) {
120 121 122
      acquireJobsRunnable.jobWasAdded();
    }
  }
123
  
124 125
  /** Starts the acquisition thread */
  protected void startJobAcquisitionThread() {
126 127 128 129 130 131
		if (jobAcquisitionThread == null) {
			jobAcquisitionThread = new Thread(acquireJobsRunnable);
		}
		jobAcquisitionThread.start();
	}
	
132
  /** Stops the acquisition thread */
133 134 135 136 137 138 139 140 141
	protected void stopJobAcquisitionThread() {
		try {
			jobAcquisitionThread.join();
		} catch (InterruptedException e) {
			log.warn("Interrupted while waiting for the job Acquisition thread to terminate", e);
		}	
		jobAcquisitionThread = null;
	}
  
142
  /* Need to be implemented by concrete subclasses */
143 144
  
	public abstract void executeJobs(List<String> jobIds);
145
  protected abstract void startExecutingJobs();
146 147
  protected abstract void stopExecutingJobs(); 
  
148 149
  /* Can be overridden by subclasses if wanted */
  public void jobDone(JobEntity job) {
150
  	// Default: do nothing
151
  }
152
  
153 154
  /* Can be overridden by subclasses if wanted */
  public void jobDone(String jobId) {
155
  	// Default: do nothing
156
  }
157 158
  
  // getters and setters //////////////////////////////////////////////////////
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

  public CommandExecutor getCommandExecutor() {
    return commandExecutor;
  }

  public long getWaitTimeInMillis() {
    return waitTimeInMillis;
  }

  public void setWaitTimeInMillis(int waitTimeInMillis) {
    this.waitTimeInMillis = waitTimeInMillis;
  }

  public int getLockTimeInMillis() {
    return lockTimeInMillis;
  }

  public void setLockTimeInMillis(int lockTimeInMillis) {
    this.lockTimeInMillis = lockTimeInMillis;
  }

  public String getLockOwner() {
    return lockOwner;
  }

  public void setLockOwner(String lockOwner) {
    this.lockOwner = lockOwner;
  }

  public boolean isAutoActivate() {
    return isAutoActivate;
  }
T
Tijs Rademakers 已提交
191

192 193 194
  public void setCommandExecutor(CommandExecutor commandExecutor) {
    this.commandExecutor = commandExecutor;
  }
T
Tijs Rademakers 已提交
195

196 197 198
  public void setAutoActivate(boolean isAutoActivate) {
    this.isAutoActivate = isAutoActivate;
  }
T
Tijs Rademakers 已提交
199

200 201 202
  public int getMaxJobsPerAcquisition() {
    return maxJobsPerAcquisition;
  }
203
  
204 205 206
  public void setMaxJobsPerAcquisition(int maxJobsPerAcquisition) {
    this.maxJobsPerAcquisition = maxJobsPerAcquisition;
  }
T
Tijs Rademakers 已提交
207

208 209 210
  public String getName() {
    return name;
  }
211
  
212 213 214
  public Command<AcquiredJobs> getAcquireJobsCmd() {
    return acquireJobsCmd;
  }
215
  
216 217 218
  public void setAcquireJobsCmd(Command<AcquiredJobs> acquireJobsCmd) {
    this.acquireJobsCmd = acquireJobsCmd;
  }
219
    
220
  public AcquireJobsRunnable getAcquireJobsRunnable() {
221 222
		return acquireJobsRunnable;
	}
T
Tijs Rademakers 已提交
223

224 225 226
	public void setAcquireJobsRunnable(AcquireJobsRunnable acquireJobsRunnable) {
		this.acquireJobsRunnable = acquireJobsRunnable;
	}
T
Tijs Rademakers 已提交
227

228
	public boolean isActive() {
229 230
    return isActive;
  }
231
  
232 233 234
  public RejectedJobsHandler getRejectedJobsHandler() {
    return rejectedJobsHandler;
  }
235
    
236 237 238
  public void setRejectedJobsHandler(RejectedJobsHandler rejectedJobsHandler) {
    this.rejectedJobsHandler = rejectedJobsHandler;
  }
239
  
240 241 242
  public Date getCurrentTime() {
    return clockReader.getCurrentTime();
  }
T
Tijs Rademakers 已提交
243

244 245 246
  public void setClockReader(ClockReader clockReader) {
    this.clockReader = clockReader;
  }
247
}