user2304777
user2304777

Reputation: 1

Regarding adding spring jmx functionality in the below application

I am new to the world of JMX , so far I have explored that mostly it is used in monitoring and managing the applications , since I am very much interested in spring JMX , I have developed this interface and class , could you please advise me how to make in perfect for JMX specially with spring , what settings need to be done in spring xml for this...

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);
} 

and the below is class..

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;
   }
}

Upvotes: 0

Views: 138

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136122

1) Rename SimpleCalculatorIf to SimpleCalculatorMBean. Then these 2 lines in context.xml will be enough for Spring to detect and register your SimpleCalculator as MBean http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html

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

2) But the most efficient way is to use Spring annotations, then you dont even need an 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;
   }
   ...

actually default @ManagedResource with no param would work too, I just wanted to show how many options you have with annotations. Read more in Spring docs

Upvotes: 2

Related Questions