Skip to content

Commit

Permalink
Issue #175: Move setting the history size from attribute to mbean cre…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
mk23 committed Feb 9, 2018
1 parent 43f701e commit eee0949
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
29 changes: 26 additions & 3 deletions src/main/java/com/github/mk23/jmxproxy/core/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*
* @see <a href="https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/JsonSerializable.html">com.fasterxml.jackson.databind.JsonSerializable</a>
*
* @since 2015-05-11
* @since 2018-02-08
* @author mk23
* @version 3.2.0
* @version 3.4.0
*/
public class Host implements JsonSerializable {
private final Map<String, MBean> mbeans;
Expand All @@ -45,7 +45,8 @@ public Host() {
* <p>Inserts a new MBean name to value association.</p>
*
* Creates a new {@link MBean} object and inserts into the map store
* associating it to the specified mbean name.
* associating it to the specified mbean name. Uses default attribute
* {@link com.github.mk23.jmxproxy.util.History} size configuration.
*
* @param mbeanName name of the {@link MBean} used as the map key.
*
Expand All @@ -60,6 +61,28 @@ public final MBean addMBean(final String mbeanName) {
return mbeans.get(mbeanName);
}

/**
* <p>Inserts a new MBean name to value association.</p>
*
* Creates a new {@link MBean} object and inserts into the map store
* associating it to the specified mbean name. Uses custom attribute
* {@link com.github.mk23.jmxproxy.util.History} size configuration.
*
* @param mbeanName name of the {@link MBean} used as the map key.
* @param historySize number of items to preserve in the associated
* {@link com.github.mk23.jmxproxy.util.History}.
*
* @return the newly created {@link MBean} object.
*/
public final MBean addMBean(final String mbeanName, final int historySize) {
if (!mbeans.containsKey(mbeanName)) {
MBean mbean = new MBean(historySize);
mbeans.put(mbeanName, mbean);
}

return mbeans.get(mbeanName);
}

/**
* <p>Deletes an mbean association.</p>
*
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/com/github/mk23/jmxproxy/core/MBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
*
* @see <a href="https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/JsonSerializable.html">com.fasterxml.jackson.databind.JsonSerializable</a>
*
* @since 2015-05-11
* @since 2018-02-08
* @author mk23
* @version 3.2.0
* @version 3.4.0
*/
public class MBean implements JsonSerializable {
private final int historySize;

private final Map<String, History<Attribute>> attributes;
private final ThreadLocal<Integer> limit = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
@Override protected Integer initialValue() {
return -1;
}
};
Expand All @@ -44,6 +45,19 @@ protected Integer initialValue() {
* Creates a map of {@link Attribute} name to {@link History} of associated values.
*/
public MBean() {
this.historySize = 1;
attributes = new HashMap<String, History<Attribute>>();
}

/**
* <p>Default constructor.</p>
*
* Creates a map of {@link Attribute} name to {@link History} of associated values.
*
* @param historySize number of items to preserve in the associated {@link History}.
*/
public MBean(final int historySize) {
this.historySize = historySize;
attributes = new HashMap<String, History<Attribute>>();
}

Expand All @@ -56,13 +70,8 @@ public MBean() {
*
* @param attributeName name of the {@link Attribute} used as the map key.
* @param attributeObject value of the {@link Attribute} added to history.
* @param historySize number of items to preserve in the associated {@link History}.
*/
public final void addAttribute(
final String attributeName,
final Object attributeObject,
final int historySize
) {
public final void addAttribute(final String attributeName, final Object attributeObject) {
if (!attributes.containsKey(attributeName)) {
attributes.put(attributeName, new History<Attribute>(historySize));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ private void fetchJMXValues() {
LOG.debug("discovered mbean " + mbeanName);
freshMBeans.add(mbeanName.toString());

MBean mbean = host.addMBean(mbeanName.toString());
MBean mbean = host.addMBean(mbeanName.toString(), historySize);
try {
for (MBeanAttributeInfo attributeObject : server.getMBeanInfo(mbeanName).getAttributes()) {
if (attributeObject.isReadable()) {
try {
Object attribute = server.getAttribute(mbeanName, attributeObject.getName());

mbean.addAttribute(attributeObject.getName(), attribute, historySize);
mbean.addAttribute(attributeObject.getName(), attribute);
} catch (java.lang.NullPointerException
| java.rmi.RemoteException
| javax.management.JMException
Expand Down

0 comments on commit eee0949

Please sign in to comment.