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