JobEntity.java 8.3 KB
Newer Older
D
dsyer 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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.
 */
T
tombaeyens 已提交
13
package org.activiti.engine.impl.persistence.entity;
D
dsyer 已提交
14 15

import java.io.Serializable;
16
import java.io.UnsupportedEncodingException;
D
dsyer 已提交
17 18 19 20
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

21
import org.activiti.engine.ActivitiException;
22 23
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
24
import org.activiti.engine.impl.context.Context;
25
import org.activiti.engine.impl.db.HasRevision;
T
tombaeyens 已提交
26
import org.activiti.engine.impl.db.PersistentObject;
27
import org.activiti.engine.impl.interceptor.CommandContext;
28
import org.activiti.engine.impl.jobexecutor.JobHandler;
29
import org.activiti.engine.runtime.Job;
30
import org.apache.commons.lang3.StringUtils;
D
dsyer 已提交
31 32

/**
D
dsyer 已提交
33
 * Stub of the common parts of a Job. You will normally work with a subclass of
34
 * JobEntity, such as {@link TimerEntity} or {@link MessageEntity}.
D
dsyer 已提交
35
 *
D
dsyer 已提交
36 37
 * @author Tom Baeyens
 * @author Nick Burch
D
dsyer 已提交
38
 * @author Dave Syer
J
jbarrez 已提交
39
 * @author Frederik Heremans
D
dsyer 已提交
40
 */
41
public abstract class JobEntity implements Serializable, Job, PersistentObject, HasRevision {
D
dsyer 已提交
42

M
meyerd 已提交
43
  public static final boolean DEFAULT_EXCLUSIVE = true;
D
dsyer 已提交
44
  public static final int DEFAULT_RETRIES = 3;
45
  private static final int MAX_EXCEPTION_MESSAGE_LENGTH = 255;
D
dsyer 已提交
46 47 48

  private static final long serialVersionUID = 1L;

T
tombaeyens 已提交
49
  protected String id;
50
  protected int revision;
D
dsyer 已提交
51

T
tombaeyens 已提交
52
  protected Date duedate;
D
dsyer 已提交
53

T
tombaeyens 已提交
54 55
  protected String lockOwner = null;
  protected Date lockExpirationTime = null;
D
dsyer 已提交
56

57
  protected String executionId = null;
T
tombaeyens 已提交
58
  protected String processInstanceId = null;
59
  protected String processDefinitionId = null;
D
dsyer 已提交
60

T
tombaeyens 已提交
61
  protected boolean isExclusive = DEFAULT_EXCLUSIVE;
D
dsyer 已提交
62

T
tombaeyens 已提交
63
  protected int retries = DEFAULT_RETRIES;
D
dsyer 已提交
64

T
tombaeyens 已提交
65 66
  protected String jobHandlerType = null;
  protected String jobHandlerConfiguration = null;
67
  
M
Marcus Klimstra 已提交
68
  protected final ByteArrayRef exceptionByteArrayRef = new ByteArrayRef();
69 70
  
  protected String exceptionMessage;
71 72
  
  protected String tenantId;
T
tombaeyens 已提交
73

74
  public void execute(CommandContext commandContext) {
75 76
    ExecutionEntity execution = null;
    if (executionId != null) {
77
      execution = commandContext.getExecutionEntityManager().findExecutionById(executionId);
T
tombaeyens 已提交
78
    }
79

80
    Map<String, JobHandler> jobHandlers = Context.getProcessEngineConfiguration().getJobHandlers();
81
    JobHandler jobHandler = jobHandlers.get(jobHandlerType);
82

83
    jobHandler.execute(this, jobHandlerConfiguration, execution, commandContext);
T
tombaeyens 已提交
84
  }
85
  
86
  public void insert() {
87
    Context.getCommandContext()
M
Marcus Klimstra 已提交
88 89
      .getDbSqlSession()
      .insert(this);
90 91
    
    // add link to execution
92
    if (executionId != null) {
93
      ExecutionEntity execution = Context.getCommandContext()
94
        .getExecutionEntityManager()
95 96
        .findExecutionById(executionId);
      execution.addJob(this);
97 98
      
      // Inherit tenant if (if applicable)
99
      if (execution != null && execution.getTenantId() != null) {
100 101
      	setTenantId(execution.getTenantId());
      }
102
    }
103 104 105 106 107
    
    if(Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
    	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
    			ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, this));
    }
108 109
  }
  
110
  public void delete() {
111 112 113
    Context.getCommandContext()
      .getDbSqlSession()
      .delete(this);
114 115

    // Also delete the job's exception byte array
M
Marcus Klimstra 已提交
116
    exceptionByteArrayRef.delete();
117 118
    
    // remove link to execution
119
    if (executionId != null) {
120
      ExecutionEntity execution = Context.getCommandContext()
121
        .getExecutionEntityManager()
122 123 124
        .findExecutionById(executionId);
      execution.removeJob(this);
    }
125 126 127 128 129
    
    if(Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
    	Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
    			ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_DELETED, this));
    }
130
  }
D
dsyer 已提交
131

132 133 134
  public void setExecution(ExecutionEntity execution) {
    executionId = execution.getId();
    processInstanceId = execution.getProcessInstanceId();
135
    processDefinitionId = execution.getProcessDefinitionId();
136 137 138
    execution.addJob(this);
  }

139
  public String getExceptionStacktrace() {
M
Marcus Klimstra 已提交
140 141
    byte[] bytes = exceptionByteArrayRef.getBytes();
    if (bytes == null) {
142 143 144
      return null;
    }
    try {
M
Marcus Klimstra 已提交
145
      return new String(bytes, "UTF-8");
146 147 148
    } catch (UnsupportedEncodingException e) {
      throw new ActivitiException("UTF-8 is not a supported encoding");
    }
149 150
  }
  
151
  public void setExceptionStacktrace(String exception) {
M
Marcus Klimstra 已提交
152
    exceptionByteArrayRef.setValue("stacktrace", getUtf8Bytes(exception));
153 154
  }

155 156 157 158 159 160 161 162
  private byte[] getUtf8Bytes(String str) {
    if (str == null) {
      return null;
    }
    try {
      return str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new ActivitiException("UTF-8 is not a supported encoding");
163 164 165
    }
  }
  
D
dsyer 已提交
166
  public Object getPersistentState() {
D
dsyer 已提交
167
    Map<String, Object> persistentState = new HashMap<String, Object>();
D
dsyer 已提交
168 169 170
    persistentState.put("lockOwner", lockOwner);
    persistentState.put("lockExpirationTime", lockExpirationTime);
    persistentState.put("retries", retries);
171
    persistentState.put("duedate", duedate);
172
    persistentState.put("exceptionMessage", exceptionMessage);
M
Marcus Klimstra 已提交
173
    persistentState.put("exceptionByteArrayId", exceptionByteArrayRef.getId());      
D
dsyer 已提交
174 175
    return persistentState;
  }
176 177 178 179
  
  public int getRevisionNext() {
    return revision+1;
  }
D
dsyer 已提交
180

T
tombaeyens 已提交
181
  // getters and setters //////////////////////////////////////////////////////
D
dsyer 已提交
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public int getRevision() {
    return revision;
  }
  public void setRevision(int revision) {
    this.revision = revision;
  }
  public Date getDuedate() {
    return duedate;
  }
  public void setDuedate(Date duedate) {
    this.duedate = duedate;
  }
201 202
  public String getExecutionId() {
    return executionId;
D
dsyer 已提交
203
  }
204 205
  public void setExecutionId(String executionId) {
    this.executionId = executionId;
D
dsyer 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  }
  public int getRetries() {
    return retries;
  }
  public void setRetries(int retries) {
    this.retries = retries;
  }
  public String getLockOwner() {
    return lockOwner;
  }
  public void setLockOwner(String claimedBy) {
    this.lockOwner = claimedBy;
  }
  public Date getLockExpirationTime() {
    return lockExpirationTime;
  }
  public void setLockExpirationTime(Date claimedUntil) {
    this.lockExpirationTime = claimedUntil;
  }
  public String getProcessInstanceId() {
    return processInstanceId;
  }
  public void setProcessInstanceId(String processInstanceId) {
    this.processInstanceId = processInstanceId;
  }
  public boolean isExclusive() {
T
tombaeyens 已提交
232
    return isExclusive;
D
dsyer 已提交
233
  }
T
tombaeyens 已提交
234 235
  public void setExclusive(boolean isExclusive) {
    this.isExclusive = isExclusive;
D
dsyer 已提交
236
  }
237 238 239 240 241 242
  public String getProcessDefinitionId() {
    return processDefinitionId;
  }
  public void setProcessDefinitionId(String processDefinitionId) {
    this.processDefinitionId = processDefinitionId;
  }
D
dsyer 已提交
243 244 245 246 247 248 249 250 251 252 253 254
  public String getJobHandlerType() {
    return jobHandlerType;
  }
  public void setJobHandlerType(String jobHandlerType) {
    this.jobHandlerType = jobHandlerType;
  }
  public String getJobHandlerConfiguration() {
    return jobHandlerConfiguration;
  }
  public void setJobHandlerConfiguration(String jobHandlerConfiguration) {
    this.jobHandlerConfiguration = jobHandlerConfiguration;
  }
255 256 257 258
  public String getExceptionMessage() {
    return exceptionMessage;
  }
  public void setExceptionMessage(String exceptionMessage) {
259
    this.exceptionMessage = StringUtils.abbreviate(exceptionMessage, MAX_EXCEPTION_MESSAGE_LENGTH);
260
  }
261 262 263 264 265 266 267
  public String getTenantId() {
		return tenantId;
	}
	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}
	
268
  // common methods  //////////////////////////////////////////////////////////
269

270
	@Override
271 272
  public String toString() {
    return "JobEntity [id=" + id + "]";
273
  }
274
  
D
dsyer 已提交
275
}