Skip to content

Commit

Permalink
[AMQ-8463] TODO: Squash -- refactor to use generics for stats types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrpav committed Nov 29, 2024
1 parent c44ee5c commit 75064f4
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public class DestinationStatistics extends StatsImpl {
protected CountStatisticImpl networkDequeues;

// [AMQ-8463] Advanced Message Statistics are optionally enabled
protected LongStatisticImpl enqueuedMessageBrokerInTime;
protected StringStatisticImpl enqueuedMessageClientID;
protected StringStatisticImpl enqueuedMessageID;
protected LongStatisticImpl enqueuedMessageTimestamp;
protected LongStatisticImpl dequeuedMessageBrokerInTime;
protected LongStatisticImpl dequeuedMessageBrokerOutTime;
protected StringStatisticImpl dequeuedMessageClientID;
protected StringStatisticImpl dequeuedMessageID;
protected LongStatisticImpl dequeuedMessageTimestamp;
protected UnsampledStatisticImpl<Long> enqueuedMessageBrokerInTime;
protected UnsampledStatisticImpl<String> enqueuedMessageClientID;
protected UnsampledStatisticImpl<String> enqueuedMessageID;
protected UnsampledStatisticImpl<Long> enqueuedMessageTimestamp;
protected UnsampledStatisticImpl<Long> dequeuedMessageBrokerInTime;
protected UnsampledStatisticImpl<Long> dequeuedMessageBrokerOutTime;
protected UnsampledStatisticImpl<String> dequeuedMessageClientID;
protected UnsampledStatisticImpl<String> dequeuedMessageID;
protected UnsampledStatisticImpl<Long> dequeuedMessageTimestamp;

public DestinationStatistics() {

Expand Down Expand Up @@ -87,16 +87,16 @@ public DestinationStatistics() {
networkEnqueues = new CountStatisticImpl("networkEnqueues", "The number of messages that have been sent to the destination via network connection");
networkDequeues = new CountStatisticImpl("networkDequeues", "The number of messages that have been acknowledged from the destination via network connection");

enqueuedMessageBrokerInTime = new LongStatisticImpl("enqueuedMessageBrokerInTime", "Broker in time (ms) of last enqueued message to the destination");
enqueuedMessageClientID = new StringStatisticImpl("enqueuedMessageClientID", "ClientID of last enqueued message to the destination");
enqueuedMessageID = new StringStatisticImpl("enqueuedMessageID", "MessageID of last enqueued message to the destination");
enqueuedMessageTimestamp = new LongStatisticImpl("enqueuedMessageTimestamp", "Message timestamp of last enqueued message to the destination");
enqueuedMessageBrokerInTime = new UnsampledStatisticImpl<>("enqueuedMessageBrokerInTime", "ms", "Broker in time (ms) of last enqueued message to the destination", Long.valueOf(0l));
enqueuedMessageClientID = new UnsampledStatisticImpl<>("enqueuedMessageClientID", "id", "ClientID of last enqueued message to the destination", null);
enqueuedMessageID = new UnsampledStatisticImpl<>("enqueuedMessageID", "id", "MessageID of last enqueued message to the destination", null);
enqueuedMessageTimestamp = new UnsampledStatisticImpl<>("enqueuedMessageTimestamp", "ms", "Message timestamp of last enqueued message to the destination", Long.valueOf(0l));

dequeuedMessageBrokerInTime = new LongStatisticImpl("dequeuedMessageBrokerInTime", "Broker in time (ms) of last dequeued message to the destination");
dequeuedMessageBrokerOutTime = new LongStatisticImpl("dequeuedMessageBrokerOutTime", "Broker out time (ms) of last dequeued message to the destination");
dequeuedMessageClientID = new StringStatisticImpl("dequeuedMessageClientID", "ClientID of last dequeued message to the destination");
dequeuedMessageID = new StringStatisticImpl("dequeuedMessageID", "MessageID of last dequeued message to the destination");
dequeuedMessageTimestamp = new LongStatisticImpl("dequeuedMessageTimestamp", "Message timestamp of last dequeued message to the destination");
dequeuedMessageBrokerInTime = new UnsampledStatisticImpl<>("dequeuedMessageBrokerInTime", "ms", "Broker in time (ms) of last dequeued message to the destination", Long.valueOf(0l));
dequeuedMessageBrokerOutTime = new UnsampledStatisticImpl<>("dequeuedMessageBrokerOutTime", "ms", "Broker out time (ms) of last dequeued message to the destination", Long.valueOf(0l));
dequeuedMessageClientID = new UnsampledStatisticImpl<>("dequeuedMessageClientID", "id", "ClientID of last dequeued message to the destination", null);
dequeuedMessageID = new UnsampledStatisticImpl<>("dequeuedMessageID", "id", "MessageID of last dequeued message to the destination", null);
dequeuedMessageTimestamp = new UnsampledStatisticImpl<>("dequeuedMessageTimestamp", "ms", "Message timestamp of last dequeued message to the destination", Long.valueOf(0l));

addStatistic("enqueues", enqueues);
addStatistic("dispatched", dispatched);
Expand Down Expand Up @@ -202,39 +202,39 @@ public CountStatisticImpl getNetworkDequeues() {
return networkDequeues;
}

public LongStatisticImpl getEnqueuedMessageBrokerInTime() {
public UnsampledStatistic<Long> getEnqueuedMessageBrokerInTime() {
return enqueuedMessageBrokerInTime;
}

public StringStatisticImpl getEnqueuedMessageClientID() {
public UnsampledStatistic<String> getEnqueuedMessageClientID() {
return enqueuedMessageClientID;
}

public StringStatisticImpl getEnqueuedMessageID() {
public UnsampledStatistic<String> getEnqueuedMessageID() {
return enqueuedMessageID;
}

public LongStatisticImpl getEnqueuedMessageTimestamp() {
public UnsampledStatistic<Long> getEnqueuedMessageTimestamp() {
return enqueuedMessageTimestamp;
}

public LongStatisticImpl getDequeuedMessageBrokerInTime() {
public UnsampledStatistic<Long> getDequeuedMessageBrokerInTime() {
return dequeuedMessageBrokerInTime;
}

public LongStatisticImpl getDequeuedMessageBrokerOutTime() {
public UnsampledStatistic<Long> getDequeuedMessageBrokerOutTime() {
return dequeuedMessageBrokerOutTime;
}

public StringStatisticImpl getDequeuedMessageClientID() {
public UnsampledStatistic<String> getDequeuedMessageClientID() {
return dequeuedMessageClientID;
}

public StringStatisticImpl getDequeuedMessageID() {
public UnsampledStatistic<String> getDequeuedMessageID() {
return dequeuedMessageID;
}

public LongStatisticImpl getDequeuedMessageTimestamp() {
public UnsampledStatistic<Long> getDequeuedMessageTimestamp() {
return dequeuedMessageTimestamp;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
* A marker interface for Statistics without
* sampleTime or or startTime.
*/
public interface UnsampledStatistic extends Statistic {}

public interface UnsampledStatistic<T> extends Statistic, Resettable {
public T getValue();
public void setValue(T value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,29 @@
*/
package org.apache.activemq.management;

import java.util.concurrent.atomic.AtomicReference;

/**
* Base class for a UnsampledStatistic implementation
*
* UnsampledStatistic<T> implementation
*
*/
public abstract class UnsampledStatisticImpl extends StatisticImpl implements UnsampledStatistic {
public class UnsampledStatisticImpl<T> extends StatisticImpl implements UnsampledStatistic<T> {

protected final AtomicReference<T> value = new AtomicReference<>();
protected final T defaultValue;

public UnsampledStatisticImpl(String name, String unit, String description) {
public UnsampledStatisticImpl(String name, String unit, String description, T defaultValue) {
super(name, unit, description, 0l, 0l);
this.value.set(defaultValue);
this.defaultValue = defaultValue;
}

@Override
public void reset() {}
public void reset() {
if (isDoReset()) {
value.set(defaultValue);
}
}

@Override
protected void updateSampleTime() {}
Expand All @@ -42,4 +52,22 @@ public long getStartTime() {
public long getLastSampleTime() {
return 0l;
}

@Override
public T getValue() {
return value.get();
}

@Override
public void setValue(T value) {
if (isEnabled()) {
this.value.set(value);
}
}

protected void appendFieldDescription(StringBuffer buffer) {
buffer.append(" value: ");
buffer.append(value.get());
super.appendFieldDescription(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,36 @@
public class UnsampledStatisticsTest {

@Test
public void testUnsampledStatisticsEnabled() {
LongStatisticImpl longStatisticImpl = new LongStatisticImpl("longStat", "long", "A long statistic");
longStatisticImpl.setEnabled(true);
longStatisticImpl.setValue(Long.MAX_VALUE);
LongStatistic longStatistic = longStatisticImpl;
public void testUnsampledStatisticsEnabledTest() {
UnsampledStatisticImpl<Long> longStatistic = new UnsampledStatisticImpl<>("longStat", "long", "A long statistic", Long.valueOf(0l));
longStatistic.setEnabled(true);
longStatistic.setValue(Long.MAX_VALUE);

StringStatisticImpl stringStatisticImpl = new StringStatisticImpl("stringStat", "chars", "A string statistic");
stringStatisticImpl.setEnabled(true);
stringStatisticImpl.setValue("Hello World!");
StringStatistic stringStatistic = stringStatisticImpl;
UnsampledStatisticImpl<String> stringStatistic = new UnsampledStatisticImpl<>("stringStat", "chars", "A string statistic", null);
stringStatistic.setEnabled(true);
stringStatistic.setValue("Hello World!");

assertEquals("A long statistic", longStatistic.getDescription());
assertEquals(Long.valueOf(0l), Long.valueOf(longStatistic.getLastSampleTime()));
assertEquals("longStat", longStatistic.getName());
assertEquals(Long.valueOf(0l), Long.valueOf(longStatistic.getStartTime()));
assertEquals("long", longStatistic.getUnit());
assertEquals(Long.valueOf(Long.MAX_VALUE), longStatistic.getValue());
assertTrue(longStatistic.toString().contains("value: " + Long.MAX_VALUE));
longStatistic.reset();
assertEquals(Long.valueOf(0l), longStatistic.getValue());
assertTrue(longStatistic.toString().contains("value: 0"));

assertEquals("A string statistic", stringStatistic.getDescription());
assertEquals(Long.valueOf(0l), Long.valueOf(stringStatistic.getLastSampleTime()));
assertEquals("stringStat", stringStatistic.getName());
assertEquals(Long.valueOf(0l), Long.valueOf(stringStatistic.getStartTime()));
assertEquals("chars", stringStatistic.getUnit());
assertEquals("Hello World!", stringStatistic.getValue());
assertTrue(stringStatistic.toString().contains("value: Hello World!"));
stringStatistic.reset();
assertNull(stringStatistic.getValue());
assertTrue(stringStatistic.toString().contains("value: null"));
}

}

0 comments on commit 75064f4

Please sign in to comment.