View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
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   * The Class DatasourceStatsCollectorBean.
27   */
28  public class DatasourceStatsCollectorBean extends AbstractStatsCollectorBean {
29  
30    /** The Constant PREFIX_ESTABLISHED. */
31    private static final String PREFIX_ESTABLISHED = "ds.est.";
32  
33    /** The Constant PREFIX_BUSY. */
34    private static final String PREFIX_BUSY = "ds.busy.";
35  
36    /** The Constant logger. */
37    private static final Logger logger = LoggerFactory.getLogger(DatasourceStatsCollectorBean.class);
38  
39    /** The container wrapper. */
40    @Inject
41    private ContainerWrapperBean containerWrapper;
42  
43    /**
44     * Gets the container wrapper.
45     *
46     * @return the container wrapper
47     */
48    public ContainerWrapperBean getContainerWrapper() {
49      return containerWrapper;
50    }
51  
52    /**
53     * Sets the container wrapper.
54     *
55     * @param containerWrapper the new container wrapper
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     * Reset.
83     *
84     * @throws NamingException the naming exception
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     * Reset.
98     *
99     * @param name the name
100    */
101   public void reset(String name) {
102     resetStats(PREFIX_ESTABLISHED + name);
103     resetStats(PREFIX_BUSY + name);
104   }
105 
106   /**
107    * Sets the max series expression.
108    *
109    * @param period the period
110    * @param span the span
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 }