QueryResultDispatcher.java
1.67 KB
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
package io.mycat.statistic.stat;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.mycat.MycatServer;
/**
* SQL执行后的派发 QueryResult 事件
*
* @author zhuam
*
*/
public class QueryResultDispatcher {
private static final Logger LOGGER = LoggerFactory.getLogger(QueryResultDispatcher.class);
private static List<QueryResultListener> listeners = new CopyOnWriteArrayList<QueryResultListener>();
// 初始化强制加载
static {
listeners.add( UserStatAnalyzer.getInstance() );
listeners.add( TableStatAnalyzer.getInstance() );
listeners.add( QueryConditionAnalyzer.getInstance() );
}
public static void addListener(QueryResultListener listener) {
if (listener == null) {
throw new NullPointerException();
}
listeners.add(listener);
}
public static void removeListener(QueryResultListener listener) {
listeners.remove(listener);
}
public static void removeAllListener() {
listeners.clear();
}
public static void dispatchQuery(final QueryResult queryResult) {
// 是否派发 QueryResult 事件
int useSqlStat = MycatServer.getInstance().getConfig().getSystem().getUseSqlStat();
if ( useSqlStat == 0 ) {
return;
}
//TODO:异步分发,待进一步调优
MycatServer.getInstance().getBusinessExecutor().execute(new Runnable() {
public void run() {
for(QueryResultListener listener: listeners) {
try {
listener.onQueryResult( queryResult );
} catch(Exception e) {
LOGGER.error("error:",e);
}
}
}
});
}
}