001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.monitoring.jta;
018    
019    import org.apache.commons.monitoring.Role;
020    import org.apache.commons.monitoring.configuration.Configuration;
021    import org.apache.commons.monitoring.counters.Unit;
022    import org.apache.commons.monitoring.gauges.Gauge;
023    import org.apache.commons.monitoring.gauges.GaugeFactory;
024    
025    import java.util.concurrent.atomic.AtomicLong;
026    
027    public class JTAGauges implements GaugeFactory {
028        public static final Role JTA_COMMITED = new Role("jta-commited", Unit.UNARY);
029        public static final Role JTA_ROLLBACKED = new Role("jta-rollbacked", Unit.UNARY);
030        public static final Role JTA_ACTIVE = new Role("jta-active", Unit.UNARY);
031    
032        static final AtomicLong ACTIVE = new AtomicLong(0);
033        static final AtomicLong COMMITTED = new AtomicLong(0);
034        static final AtomicLong ROLLBACKED = new AtomicLong(0);
035    
036        @Override
037        public Gauge[] gauges() {
038            final long period = Configuration.getInteger(Configuration.COMMONS_MONITORING_PREFIX + "gauge.jta.period", 4000);
039            return new Gauge[] {
040                new JTAGauge(JTA_COMMITED, COMMITTED, period),
041                new JTAGauge(JTA_ROLLBACKED, ROLLBACKED, period),
042                new JTAActiveGauge(JTA_ACTIVE, ACTIVE, period)
043            };
044        }
045    
046        protected static class JTAGauge implements Gauge {
047            private final Role role;
048            private final long period;
049            protected final AtomicLong counter;
050    
051            protected JTAGauge(final Role role, final AtomicLong counter, final long period) {
052                this.role = role;
053                this.counter = counter;
054                this.period = period;
055            }
056    
057            @Override
058            public Role role() {
059                return role;
060            }
061    
062            @Override
063            public double value() {
064                return counter.getAndSet(0);
065            }
066    
067            @Override
068            public long period() {
069                return period;
070            }
071        }
072    
073        protected static class JTAActiveGauge extends JTAGauge {
074            protected JTAActiveGauge(final Role role, final AtomicLong counter, final long period) {
075                super(role, counter, period);
076            }
077    
078            @Override
079            public double value() {
080                return counter.get();
081            }
082        }
083    }