ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
191
192
193
194
195
196
197
package org.jbpm.pvm.internal.job;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
 
import org.jbpm.api.JbpmException;
import org.jbpm.api.cmd.Command;
import org.jbpm.api.job.Job;
import org.jbpm.pvm.internal.lob.Lob;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.wire.Descriptor;
 
public abstract class JobImpl<T> implements Command<T>, Serializable, Job {
 
  private static final long serialVersionUID = 1L;
  // private static final DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS");
 
  public static final String STATE_WAITING = "waiting";
  public static final String STATE_ACQUIRED = "acquired";
  public static final String STATE_ERROR = "error";
  public static final String STATE_SUSPENDED = "suspended";
 
  protected long dbid;
  protected int dbversion;
 
  /** date until which the command should not be executed
   * for async messages, this duedate should be set to null. */
  protected Date duedate = null;
  
  /** job state. */
  protected String state = STATE_WAITING;
 
  /** the execution (if any) for this job */  
  protected ExecutionImpl execution;
 
  /** the process instance */  
  protected ExecutionImpl processInstance;
  
  // execution members
 
  /** specifies if this jobImpl can be executed concurrently with other jobs for the 
   * same execution. */
  protected boolean isExclusive;
 
  /** name of the jobImpl executor name that has locked this jobImpl. */
  protected String lockOwner;
 
  /** the time the lock on this jobImpl expires. */
  protected Date lockExpirationTime;
 
  /** stack trace of the exception that occurred during command execution. */
  protected String exception;
  
  /** number of attempts left to try.  Should be decremented each time an exception 
   * occurs during command execution. */
  protected int retries = 3;
  
  protected Lob configurationBytes;
 
  protected Object configuration;
  
  /** a command that can be used as the behaviour of this job */ 
  protected Descriptor commandDescriptor;
 
  public JobImpl() {
  }
 
  public void setExecution(ExecutionImpl execution) {
    this.execution = execution;
    this.processInstance = execution.getProcessInstance();
  }
  
  public void acquire(String lockOwner, Date lockExpirationTime) {
    this.state = STATE_ACQUIRED;
    this.lockOwner = lockOwner;
    this.lockExpirationTime = lockExpirationTime;
  }
  public void release() {
    this.state = STATE_WAITING;
    this.lockOwner = null;
    this.lockExpirationTime = null;
  }
  public void setRetries(int retries) {
    this.retries = retries;
    if (this.retries==0) {
      this.state = STATE_ERROR;
    }
  }
  public void suspend() {
    this.state = STATE_SUSPENDED;
  }
  public void resume() {
    if (retries==0) {
      this.state = STATE_ERROR;
    } else {
      this.state = STATE_WAITING;
    }
  }
  
  public Object getConfiguration() {
    if ( (configuration==null)
         && (configurationBytes!=null)
       ) {
      try {
        byte[] bytes = configurationBytes.extractBytes();
        ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
        ObjectInputStream objectStream = new ObjectInputStream(byteStream);
        configuration = objectStream.readObject();
      } catch (Exception e) {
        throw new JbpmException("couldn't deserialize configuration object for "+this, e);
      }
    }
    return configuration;
  }
  
  public void setConfiguration(Object configuration) {
    this.configuration = configuration;
    
    try {
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
      ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
      objectStream.writeObject(configuration);
      byte[] bytes = byteStream.toByteArray();
      configurationBytes = new Lob(bytes, true);
    } catch (Exception e) {
      throw new JbpmException("couldn't serialize configuration object for "+this, e);
    }
  }
 
  // customized getters and setters ///////////////////////////////////////////
  
  public String getId() {
    return Long.toString(dbid);
  }
  
  // getters and setters //////////////////////////////////////////////////////
  
  public long getDbid() {
    return dbid;
  }
  public void setDbid(long id) {
      dbid = id;
  }
  public String getLockOwner() {
    return lockOwner;
  }
  /** @deprecated */
  public Date getDueDate() {
    return getDuedate();
  }
  public Date getDuedate() {
    return duedate;
  }
  public void setDuedate(Date duedate) {
    this.duedate = duedate;
  }
  public String getException() {
    return exception;
  }
  public int getRetries() {
    return retries;
  }
  public boolean isExclusive() {
    return isExclusive;
  }
  public ExecutionImpl getExecution() {
    return execution;
  }
  public void setExclusive(boolean isExclusive) {
    this.isExclusive = isExclusive;
  }
  public void setLockOwner(String jobExecutorName) {
    this.lockOwner = jobExecutorName;
  }
  public ExecutionImpl getProcessInstance() {
    return processInstance;
  }
  public void setException(String exception) {
    this.exception = exception;
  }
  public Date getLockExpirationTime() {
    return lockExpirationTime;
  }
  public void setLockExpirationTime(Date lockExpirationTime) {
    this.lockExpirationTime = lockExpirationTime;
  }
  public Descriptor getCommandDescriptor() {
    return commandDescriptor;
  }
  public void setCommandDescriptor(Descriptor commandDescriptor) {
    this.commandDescriptor = commandDescriptor;
  }
}