1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.bam.processor; |
19 |
|
|
20 |
|
import org.apache.camel.bam.model.ActivityState; |
21 |
|
import org.apache.camel.bam.rules.ProcessRules; |
22 |
|
import org.apache.camel.impl.ServiceSupport; |
23 |
|
import org.apache.commons.logging.Log; |
24 |
|
import org.apache.commons.logging.LogFactory; |
25 |
|
import org.springframework.orm.jpa.JpaCallback; |
26 |
|
import org.springframework.orm.jpa.JpaTemplate; |
27 |
|
import org.springframework.transaction.TransactionStatus; |
28 |
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult; |
29 |
|
import org.springframework.transaction.support.TransactionTemplate; |
30 |
|
|
31 |
|
import javax.persistence.EntityManager; |
32 |
|
import javax.persistence.LockModeType; |
33 |
|
import javax.persistence.PersistenceException; |
34 |
|
import java.util.Date; |
35 |
|
import java.util.List; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
10 |
public class ActivityMonitorEngine extends ServiceSupport implements Runnable { |
43 |
1 |
private static final Log log = LogFactory.getLog(ActivityMonitorEngine.class); |
44 |
|
private JpaTemplate template; |
45 |
|
private TransactionTemplate transactionTemplate; |
46 |
|
private ProcessRules rules; |
47 |
1 |
private int escalateLevel = 0; |
48 |
1 |
private long windowMillis = 1000L; |
49 |
|
private Thread thread; |
50 |
1 |
private boolean useLocking = false; |
51 |
|
|
52 |
1 |
public ActivityMonitorEngine(JpaTemplate template, TransactionTemplate transactionTemplate, ProcessRules rules) { |
53 |
1 |
this.template = template; |
54 |
1 |
this.transactionTemplate = transactionTemplate; |
55 |
1 |
this.rules = rules; |
56 |
1 |
} |
57 |
|
|
58 |
|
public boolean isUseLocking() { |
59 |
1 |
return useLocking; |
60 |
|
} |
61 |
|
|
62 |
|
public void setUseLocking(boolean useLocking) { |
63 |
0 |
this.useLocking = useLocking; |
64 |
0 |
} |
65 |
|
|
66 |
|
public void run() { |
67 |
1 |
log.debug("Starting to poll for timeout events"); |
68 |
|
|
69 |
4 |
while (!isStopped()) { |
70 |
|
try { |
71 |
4 |
long now = System.currentTimeMillis(); |
72 |
4 |
long nextPoll = now + windowMillis; |
73 |
4 |
final Date timeNow = new Date(now); |
74 |
|
|
75 |
4 |
transactionTemplate.execute(new TransactionCallbackWithoutResult() { |
76 |
4 |
protected void doInTransactionWithoutResult(TransactionStatus status) { |
77 |
4 |
List<ActivityState> list = template.find("select x from " + ActivityState.class.getName() + " x where x.escalationLevel = ?1 and x.timeOverdue < ?2", escalateLevel, timeNow); |
78 |
4 |
for (ActivityState activityState : list) { |
79 |
1 |
fireExpiredEvent(activityState); |
80 |
1 |
} |
81 |
4 |
} |
82 |
|
}); |
83 |
|
|
84 |
4 |
long timeToSleep = nextPoll - System.currentTimeMillis(); |
85 |
4 |
if (timeToSleep > 0) { |
86 |
4 |
if (log.isDebugEnabled()) { |
87 |
0 |
log.debug("Sleeping for " + timeToSleep + " millis"); |
88 |
|
} |
89 |
|
try { |
90 |
4 |
Thread.sleep(timeToSleep); |
91 |
|
} |
92 |
0 |
catch (InterruptedException e) { |
93 |
0 |
log.debug("Caught: " + e, e); |
94 |
3 |
} |
95 |
|
} |
96 |
|
} |
97 |
0 |
catch (Exception e) { |
98 |
0 |
log.error("Caught: " + e, e); |
99 |
3 |
} |
100 |
0 |
} |
101 |
0 |
} |
102 |
|
|
103 |
|
protected void fireExpiredEvent(final ActivityState activityState) { |
104 |
1 |
if (log.isDebugEnabled()) { |
105 |
0 |
log.debug("Trying to fire expiration of: " + activityState); |
106 |
|
} |
107 |
|
|
108 |
1 |
template.execute(new JpaCallback() { |
109 |
1 |
public Object doInJpa(EntityManager entityManager) throws PersistenceException { |
110 |
|
|
111 |
1 |
if (isUseLocking()) { |
112 |
0 |
log.info("Attempting to lock: " + activityState); |
113 |
0 |
entityManager.lock(activityState, LockModeType.WRITE); |
114 |
0 |
log.info("Grabbed lock: " + activityState); |
115 |
|
} |
116 |
|
|
117 |
|
try { |
118 |
1 |
rules.processExpired(activityState); |
119 |
|
} |
120 |
0 |
catch (Exception e) { |
121 |
0 |
log.error("Failed to process expiration of: " + activityState + ". Reason: " + e, e); |
122 |
1 |
} |
123 |
1 |
activityState.setEscalationLevel(escalateLevel + 1); |
124 |
1 |
return null; |
125 |
|
} |
126 |
|
}); |
127 |
1 |
} |
128 |
|
|
129 |
|
protected void doStart() throws Exception { |
130 |
1 |
rules.start(); |
131 |
1 |
thread = new Thread(this, "ActivityMonitorEngine"); |
132 |
1 |
thread.start(); |
133 |
1 |
} |
134 |
|
|
135 |
|
protected void doStop() throws Exception { |
136 |
0 |
if (thread != null) { |
137 |
0 |
thread = null; |
138 |
|
} |
139 |
0 |
rules.stop(); |
140 |
0 |
} |
141 |
|
} |