| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.bam; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Exchange; |
| 20 |
|
import org.apache.camel.Processor; |
| 21 |
|
import org.apache.camel.bam.model.ActivityState; |
| 22 |
|
import org.apache.camel.bam.model.ProcessInstance; |
| 23 |
|
import org.apache.camel.builder.FromBuilder; |
| 24 |
|
import org.apache.camel.builder.ProcessorFactory; |
| 25 |
|
import org.apache.camel.impl.DefaultExchange; |
| 26 |
|
import org.apache.camel.util.Time; |
| 27 |
|
import org.apache.commons.logging.Log; |
| 28 |
|
import org.apache.commons.logging.LogFactory; |
| 29 |
|
|
| 30 |
|
import java.util.Date; |
| 31 |
0 |
|
| 32 |
0 |
|
| 33 |
0 |
|
| 34 |
0 |
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
public class TemporalRule { |
| 38 |
0 |
private static final transient Log log = LogFactory.getLog(TemporalRule.class); |
| 39 |
|
private TimeExpression first; |
| 40 |
|
private TimeExpression second; |
| 41 |
|
private long expectedMillis; |
| 42 |
|
private long overdueMillis; |
| 43 |
|
private Processor overdueAction; |
| 44 |
|
private ProcessorFactory overdueProcessorFactory; |
| 45 |
|
|
| 46 |
0 |
public TemporalRule(TimeExpression left, TimeExpression right) { |
| 47 |
0 |
this.first = left; |
| 48 |
0 |
this.second = right; |
| 49 |
0 |
} |
| 50 |
|
|
| 51 |
|
public TemporalRule expectWithin(Time builder) { |
| 52 |
0 |
return expectWithin(builder.toMillis()); |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
public TemporalRule expectWithin(long millis) { |
| 56 |
0 |
expectedMillis = millis; |
| 57 |
0 |
return this; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
public FromBuilder errorIfOver(Time builder) { |
| 61 |
0 |
return errorIfOver(builder.toMillis()); |
| 62 |
0 |
} |
| 63 |
|
|
| 64 |
|
public FromBuilder errorIfOver(long millis) { |
| 65 |
0 |
overdueMillis = millis; |
| 66 |
|
|
| 67 |
0 |
FromBuilder builder = new FromBuilder(second.getBuilder().getProcessBuilder(), null); |
| 68 |
0 |
overdueProcessorFactory = builder; |
| 69 |
0 |
return builder; |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
public TimeExpression getFirst() { |
| 73 |
0 |
return first; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
public TimeExpression getSecond() { |
| 77 |
0 |
return second; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
public void evaluate(ProcessContext context, ActivityState activityState) { |
| 81 |
0 |
ProcessInstance instance = context.getProcessInstance(); |
| 82 |
|
|
| 83 |
0 |
Date firstTime = first.evaluateState(instance); |
| 84 |
0 |
if (firstTime == null) { |
| 85 |
|
|
| 86 |
0 |
return; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
0 |
ActivityState secondState = second.getActivityState(instance); |
| 94 |
0 |
if (expectedMillis > 0L) { |
| 95 |
0 |
Date expected = secondState.getTimeExpected(); |
| 96 |
0 |
if (expected == null) { |
| 97 |
0 |
expected = add(firstTime, expectedMillis); |
| 98 |
0 |
secondState.setTimeExpected(expected); |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
0 |
if (overdueMillis > 0L) { |
| 102 |
0 |
Date overdue = secondState.getTimeOverdue(); |
| 103 |
0 |
if (overdue == null) { |
| 104 |
0 |
overdue = add(firstTime, overdueMillis); |
| 105 |
0 |
secondState.setTimeOverdue(overdue); |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
|
|
| 109 |
0 |
Date secondTime = second.evaluateState(instance); |
| 110 |
0 |
if (secondTime == null) { |
| 111 |
|
|
| 112 |
|
} |
| 113 |
|
else { |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
} |
| 121 |
0 |
} |
| 122 |
|
|
| 123 |
|
public void processExpired(ActivityState activityState) throws Exception { |
| 124 |
0 |
if (overdueAction == null && overdueProcessorFactory != null) { |
| 125 |
0 |
overdueAction = overdueProcessorFactory.createProcessor(); |
| 126 |
|
} |
| 127 |
0 |
if (overdueAction != null) { |
| 128 |
0 |
Date now = new Date(); |
| 129 |
0 |
ProcessInstance instance = activityState.getProcess(); |
| 130 |
0 |
ActivityState secondState = second.getActivityState(instance); |
| 131 |
0 |
Date overdue = secondState.getTimeOverdue(); |
| 132 |
0 |
if (now.compareTo(overdue) >= 0) { |
| 133 |
0 |
Exchange exchange = createExchange(); |
| 134 |
0 |
exchange.getIn().setBody(activityState); |
| 135 |
0 |
overdueAction.process(exchange); |
| 136 |
0 |
} |
| 137 |
|
else { |
| 138 |
0 |
log.warn("Process has not actually expired; the time is: " + now + " but the overdue time is: " + overdue); |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
0 |
} |
| 142 |
|
|
| 143 |
|
protected Exchange createExchange() { |
| 144 |
0 |
return new DefaultExchange(second.getBuilder().getProcessBuilder().getContext()); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
protected Date add(Date date, long millis) { |
| 155 |
0 |
return new Date(date.getTime() + millis); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
} |