View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.jta;
18  
19  import org.apache.commons.monitoring.Role;
20  import org.apache.commons.monitoring.configuration.Configuration;
21  import org.apache.commons.monitoring.counters.Unit;
22  import org.apache.commons.monitoring.gauges.Gauge;
23  import org.apache.commons.monitoring.gauges.GaugeFactory;
24  
25  import java.util.concurrent.atomic.AtomicLong;
26  
27  public class JTAGauges implements GaugeFactory {
28      public static final Role JTA_COMMITED = new Role("jta-commited", Unit.UNARY);
29      public static final Role JTA_ROLLBACKED = new Role("jta-rollbacked", Unit.UNARY);
30      public static final Role JTA_ACTIVE = new Role("jta-active", Unit.UNARY);
31  
32      static final AtomicLong ACTIVE = new AtomicLong(0);
33      static final AtomicLong COMMITTED = new AtomicLong(0);
34      static final AtomicLong ROLLBACKED = new AtomicLong(0);
35  
36      @Override
37      public Gauge[] gauges() {
38          final long period = Configuration.getInteger(Configuration.COMMONS_MONITORING_PREFIX + "gauge.jta.period", 4000);
39          return new Gauge[] {
40              new JTAGauge(JTA_COMMITED, COMMITTED, period),
41              new JTAGauge(JTA_ROLLBACKED, ROLLBACKED, period),
42              new JTAActiveGauge(JTA_ACTIVE, ACTIVE, period)
43          };
44      }
45  
46      protected static class JTAGauge implements Gauge {
47          private final Role role;
48          private final long period;
49          protected final AtomicLong counter;
50  
51          protected JTAGauge(final Role role, final AtomicLong counter, final long period) {
52              this.role = role;
53              this.counter = counter;
54              this.period = period;
55          }
56  
57          @Override
58          public Role role() {
59              return role;
60          }
61  
62          @Override
63          public double value() {
64              return counter.getAndSet(0);
65          }
66  
67          @Override
68          public long period() {
69              return period;
70          }
71      }
72  
73      protected static class JTAActiveGauge extends JTAGauge {
74          protected JTAActiveGauge(final Role role, final AtomicLong counter, final long period) {
75              super(role, counter, period);
76          }
77  
78          @Override
79          public double value() {
80              return counter.get();
81          }
82      }
83  }