gpt4 book ai didi

java - 关于在下面的应用程序中添加 spring jmx 功能

转载 作者:行者123 更新时间:2023-12-02 07:03:27 25 4
gpt4 key购买 nike

我是 JMX 世界的新手,到目前为止,我探索的主要是它用于监视和管理应用程序,因为我对 spring JMX 非常感兴趣,所以我开发了这个接口(interface)和类,你能告诉我如何使JMX特别与spring完美配合吗,为此需要在spring xml中进行哪些设置...

package dustin.jmx.modelmbeans;

/**
* Interface to expose Model MBean via Spring.
*/
public interface SimpleCalculatorIf
{
public int add(final int augend, final int addend);

public int subtract(final int minuend, final int subtrahend);

public int multiply(final int factor1, final int factor2);

public double divide(final int dividend, final int divisor);
}

下面是类..

package dustin.jmx.modelmbeans;


public class SimpleCalculator implements SimpleCalculatorIf
{
/**
* Calculate the sum of the augend and the addend.
*
* @param augend First integer to be added.
* @param addend Second integer to be added.
* @return Sum of augend and addend.
*/
public int add(final int augend, final int addend)
{
return augend + addend;
}

/**
* Calculate the difference between the minuend and subtrahend.
*
* @param minuend Minuend in subtraction operation.
* @param subtrahend Subtrahend in subtraction operation.
* @return Difference of minuend and subtrahend.
*/
public int subtract(final int minuend, final int subtrahend)
{
return minuend - subtrahend;
}

/**
* Calculate the product of the two provided factors.
*
* @param factor1 First integer factor.
* @param factor2 Second integer factor.
* @return Product of provided factors.
*/
public int multiply(final int factor1, final int factor2)
{
return factor1 * factor2;
}

/**
* Calculate the quotient of the dividend divided by the divisor.
*
* @param dividend Integer dividend.
* @param divisor Integer divisor.
* @return Quotient of dividend divided by divisor.
*/
public double divide(final int dividend, final int divisor)
{
return dividend / divisor;
}
}

最佳答案

1) 将 SimpleCalculatorIf 重命名为 SimpleCalculatorMBean。那么 context.xml 中的这两行足以让 Spring 检测到您的 SimpleCalculator 并将其注册为 MBean http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html

<context:mbean-export/>
<bean class="dustin.jmx.modelmbeans.SimpleCalculator"/>

2)但最有效的方法是使用 Spring 注解,那么你甚至不需要接口(interface)

@ManagedResource(objectName="bean:name=SimpleCalculator", description="My Managed Calculator", log=true,
logFile="jmx.log", currencyTimeLimit=15, persistPolicy="OnUpdate", persistPeriod=200,
persistLocation="foo", persistName="bar")
public class SimpleCalculator implements SimpleCalculatorIf
{
@ManagedOperation
public int add(final int augend, final int addend)
{
return augend + addend;
}
...

实际上,没有参数的默认 @ManagedResource 也可以工作,我只是想显示您有多少个带注释的选项。在 Spring 文档中阅读更多内容

关于java - 关于在下面的应用程序中添加 spring jmx 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16371379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com