1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans.stats.collectors;
12
13 import javax.inject.Inject;
14 import javax.naming.NamingException;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.springframework.beans.factory.annotation.Value;
19
20 import psiprobe.beans.ContainerWrapperBean;
21 import psiprobe.model.ApplicationResource;
22 import psiprobe.model.DataSourceInfo;
23 import psiprobe.tools.TimeExpression;
24
25
26
27
28 public class DatasourceStatsCollectorBean extends AbstractStatsCollectorBean {
29
30
31 private static final String PREFIX_ESTABLISHED = "ds.est.";
32
33
34 private static final String PREFIX_BUSY = "ds.busy.";
35
36
37 private static final Logger logger = LoggerFactory.getLogger(DatasourceStatsCollectorBean.class);
38
39
40 @Inject
41 private ContainerWrapperBean containerWrapper;
42
43
44
45
46
47
48 public ContainerWrapperBean getContainerWrapper() {
49 return containerWrapper;
50 }
51
52
53
54
55
56
57 public void setContainerWrapper(ContainerWrapperBean containerWrapper) {
58 this.containerWrapper = containerWrapper;
59 }
60
61 @Override
62 public void collect() throws NamingException, InterruptedException {
63 long currentTime = System.currentTimeMillis();
64 if (containerWrapper == null) {
65 logger.error("Cannot collect data source stats. Container wrapper is not set.");
66 } else {
67 for (ApplicationResource ds : getContainerWrapper().getDataSources()) {
68 String appName = ds.getApplicationName();
69 String name = (appName == null ? "" : appName) + '/' + ds.getName();
70 DataSourceInfo dsi = ds.getDataSourceInfo();
71 int numEstablished = dsi.getEstablishedConnections();
72 int numBusy = dsi.getBusyConnections();
73 logger.trace("Collecting stats for datasource: {}", name);
74 buildAbsoluteStats(PREFIX_ESTABLISHED + name, numEstablished, currentTime);
75 buildAbsoluteStats(PREFIX_BUSY + name, numBusy, currentTime);
76 }
77 logger.debug("datasource stats collected in {}ms", System.currentTimeMillis() - currentTime);
78 }
79 }
80
81
82
83
84
85
86 public void reset() throws NamingException {
87 if (containerWrapper == null) {
88 logger.error("Cannot reset application stats. Container wrapper is not set.");
89 } else {
90 for (ApplicationResource ds : getContainerWrapper().getDataSources()) {
91 reset(ds.getName());
92 }
93 }
94 }
95
96
97
98
99
100
101 public void reset(String name) {
102 resetStats(PREFIX_ESTABLISHED + name);
103 resetStats(PREFIX_BUSY + name);
104 }
105
106
107
108
109
110
111
112 public void setMaxSeries(
113 @Value("${psiprobe.beans.stats.collectors.datasource.period}") long period,
114 @Value("${psiprobe.beans.stats.collectors.datasource.span}") long span) {
115 super.setMaxSeries((int) TimeExpression.dataPoints(period, span));
116 }
117
118 }