/
SimpleRouterContext.java
100 lines (82 loc) · 3.09 KB
/
SimpleRouterContext.java
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
package org.jumpmind.symmetric.route;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.model.NodeChannel;
import org.springframework.jdbc.core.JdbcTemplate;
public class SimpleRouterContext implements IRouterContext {
protected NodeChannel channel;
protected JdbcTemplate jdbcTemplate;
protected boolean encountedTransactionBoundary = false;
protected Map<String, Object> contextCache = new HashMap<String, Object>();
protected String nodeId;
public SimpleRouterContext(String nodeId, JdbcTemplate jdbcTemplate, NodeChannel channel) {
this.init(jdbcTemplate, channel, nodeId);
}
public SimpleRouterContext() {
}
protected void init(JdbcTemplate jdbcTemplate, NodeChannel channel, String nodeId) {
this.channel = channel;
this.jdbcTemplate = jdbcTemplate;
this.nodeId = nodeId;
}
public String getNodeId() {
return nodeId;
}
public NodeChannel getChannel() {
return this.channel;
}
public Map<String, Object> getContextCache() {
return this.contextCache;
}
public JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
public void setEncountedTransactionBoundary(boolean encountedTransactionBoundary) {
this.encountedTransactionBoundary = encountedTransactionBoundary;
}
public boolean isEncountedTransactionBoundary() {
return this.encountedTransactionBoundary;
}
private final String getStatKey(String name) {
return String.format("Stat.%s", name);
}
synchronized public void incrementStat(long amount, String name) {
final String KEY = getStatKey(name);
Long val = (Long) contextCache.get(KEY);
if (val == null) {
val = 0l;
}
val += amount;
contextCache.put(KEY, val);
}
public long getStat(String name) {
final String KEY = getStatKey(name);
Long val = (Long) contextCache.get(KEY);
if (val == null) {
val = 0l;
}
return val;
}
public void logStats(ILog log, int dataCount, long totalTimeInMs) {
boolean infoLevel = totalTimeInMs > 30000;
Set<String> keys = new HashSet<String>(contextCache.keySet());
for (String key : keys) {
if (key.startsWith("Stat.")) {
String keyString = key.substring(key.indexOf(".") + 1);
if (infoLevel) {
log.info("RouterStats", channel.getChannelId(), keyString, contextCache.get(key));
} else {
log.debug("RouterStats", channel.getChannelId(), keyString, contextCache.get(key));
}
}
}
if (infoLevel) {
log.info("RouterTimeForChannel", totalTimeInMs, dataCount, channel.getChannelId());
} else {
log.debug("RouterTimeForChannel", totalTimeInMs, dataCount, channel.getChannelId());
}
}
}