A minimal client

For the purpose of testing our MBean we'll write a simple JSP Client which will request to our Service the Calculation of an interest rate:

<%@ page import="javax.management.*,sample.jmx.*,org.jboss.mx.util.*" %>
<% CalculateInterest cal = null;

try {
MBeanServer server = MBeanServerLocator.locate();

cal = (CalculateInterest) MBeanProxyExt.create(
CalculateInterest.class,
"sampleJMX:service=calculateInterest",
server);
cal.setInterestRate(7.5); 
out.println("Amount of Interests: " +cal.calculate(10000, 180));

} catch (Exception e) {
e.printStackTrace ();
}
%> 

This will produce as output:

 

Amount of Interests: 369.86301369863014

 

Advanced Service POJOS concepts

 In the previous chapters we have learnt how to develop JMX MBean services using simple EJB 3.0 style annotations. In this trail we'll see how services can collaborate with other services, in other words we'll define the dependencies between services.

Imagine our Interest Calculator application is part of a larger Service, for example

The dependancy of a Service on another Service can be coded with a simple annotation:


@Service (objectName="sampleJMX:service=financialAdvisor")                             
@Management(FinancialAdvisor.class)
@Depends ("sampleJMX:service=calculateInterest")
public class FinancialAdvisorMBean implements FinancialAdvisor { . . . }

The @Depends annotation specifies that this service depends on the service created for calculateInterest. i.e. it cannot be started until the service created for calculateInterest has been started.


What about if your service is dependant on an array of Services ? no problem, simply add a comma to specify the list of dependany services.


@Depends ({"sampleJMX:service=calculateInterest", "sampleJMX:service=calculateLoan"})

Depends injection

One particular type of dependancy is the "Dependancy Injection". With this Instead of annotating the dependancy at Class level the dependencies are specified on fields and setter methods.


@Depends ("sampleJMX:service=calculateInterest")
public void setCalculateInterest(CalculateInterest calculateService)
{
 this.calculateService = calculateService;
}

Here the effect of this annotation is the injection of the Service calculateInterest into our service through a setter method. Those familiar with framework like Spring will find familiar with this pattern.

So let's see a complete example of a Service which is dependant on another:

package sample.jmx;

import org.jboss.annotation.ejb.Depends;
import org.jboss.annotation.ejb.Service;
import org.jboss.annotation.ejb.Management;

@Service(objectName = "sampleJMX:service=financialAdvisor")
@Management(FinancialAdvisor.class)
public class FinancialAdvisorMBean implements FinancialAdvisor {

 private CalculateInterest calculateService;

 public String getAdvice() {

 if (calculateService.getInterestRate() < 5) {
 return "Today you invest with low interest rate";
 } else if (calculateService.getInterestRate() == 5) {
 return "Today you invest with ordinary interest rate";
 } else {
 return "Today you invest with high interest rate";
 }

}

 public CalculateInterest getCalculateInterest() {
 return calculateService;
 }

 @Depends("sampleJMX:service=calculateInterest")
 public void setCalculateInterest(CalculateInterest calculateService) {
 this.calculateService = calculateService;
 }

 public void create() throws Exception {

 System.out.println("FinancialAdvisorMBean - Creating");
 }

 public void destroy() {
 System.out.println("FinancialAdvisorMBean - Destroying");
 }
}

 

In order to test your application you need the calculateInterest service. In order to compile remember you need to add to the classpath jboss-annotations-ejb3.jar which can be found under JBOSS_HOME/client

javac -classpath c:\jboss-4.2.0.GA\client\jboss-annotations-ejb3.jar;. -d . *.java

now jar everything and deploy :

11:03:02,546 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=Calculate.jar,name=CalculateInterestMBean,service=EJB3 with dependencies:
11:03:02,593 INFO [EJBContainer] STARTED EJB: sample.jmx.CalculateInterestMBean
ejbName: CalculateInterestMBean
11:03:02,609 INFO [STDOUT] CalculateInterestMBean - Creating
11:03:02,625 INFO [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.service.ServiceContainer
11:03:02,625 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=Calculate.jar,name=FinancialAdvisorMBean,service=EJB3 with dependencies:
11:03:02,625 INFO [JmxKernelAbstraction] sampleJMX:service=calculateInterest
11:03:02,687 INFO [EJBContainer] STARTED EJB: sample.jmx.FinancialAdvisorMBean ejbName: FinancialAdvisorMBean
11:03:02,703 INFO [STDOUT] FinancialAdvisorMBean - Creating
11:03:02,718 INFO [EJB3Deployer] Deployed: file:/C:/jboss-4.2.0.GA/server/default/deploy/Calculate.jar/

 This simple application will yield a financial advice depending on the interest rate (which is set by CalculateInterestMBean)

Interceptors

You can define interceptors for your service beans in the same way as shown in EJB3 Interceptors. This example defines one in the ServiceThree bean class itself.   


@AroundInvoke    
public Object intercept(InvocationContext ctx) throws Exception     {       
 System.out.println("Calculator - Interceptor");       
 return ctx.proceed();    
} 



The call to ctx.proceed() causes the next object in the chain of interceptors to get invoked. At the end of the chain of interceptors, the actual bean method gets called.

Summary

In this trail, you learned how to develop JMX MBean services using simple EJB 3.0 style annotations. MBean services are very useful when you need to provide manageable services to other components or services inside the JBoss application server.

Download the code of this article here.

0
0
0
s2smodern

Related articles available on mastertheboss.com

How to a dump of JNDI tree with JMX console ?

#2 JBoss Howto. This is the recipe of the day

How to configure JBoss to bind to a different IP ?

  If you want to change the jboss.bind.address property use the 

How to shut down JBoss from remote ?

JBoss recipe of the day

How to create multiple instances of an Mbean?

  Supposing you need two instances of an MBean, let's say one fo

JBoss classloader issues

By default JBoss (prior to version 3.2) uses a flat class loading

jboss.xml reference

JBoss xml reference   JBoss AS uses a set of custom xml de