- Spring Beans with JMX(Java Management Extension)
Technology that enables you to create elements of management, monitoring and configuration of applications.
- Remoting MBeans
JMX é a tecnologia(API) que permite implementar “management interfaces” ou seja gerenciar aplicações Java.
• Abstrai a o trabalho de gerencia de informação para uma interface comum para facilitar o gerenciamento integrado.
• Distribui o gerenciamento movendo as funções de gerenciamento para os agentes.
• Deploy real-time de serviços e modificações
• Possibilidade de uso para gerenciar qualquer aplicação ou device ((via JNI or SNMP etc..)
Key component-> MBean(Managed bean).
MBean is a JavaBean that exposes certain methods which define the management interface.
Four types of MBeans
Standard MBeans: Management interface is determined by reflection on a fixed Java interface that is implemented by the bean class.
Dynamic MBean: Management interface is determined at runtime by invoking methods of the DynamicMBean interface.
Open MBeans: Special kind of dynamic MBean. attributes and operations are limited to private elements(types, class wrappers)
Model MBean: Special kind of dynamic MBean. Bridges a management interface to the managed resource.
Exporting Spring beans as MBeans
Ex of changing a value of a attribute in a Controller
class TestController {
...
private int intValue = 10;
public void setIntValue(int value) {
intValue = value;
}
publid int getIntValue() { return intValue;}
}
TestController can be exposed as an MBean.
<bean id="mbeanExporter" <--- Is the bean that exports Spring-managed beans as Model MBeans in the MBeanServer(container)
class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="beanTest:name=TestController" value-ref="testController"/>
</map>
</property>
<property name="server"ref="mbeanServer"/>
<property name="assembler"ref="assembler"/> <---- Expose the methods
</bean>
- After this, JConsole or VisualVM can view the bean's properties and invoke their methods.
- Exposing methods by name
<bean id="assembler"
class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler"
p:managedMethods="getSpittlesPerPage,setSpittlesPerPage"/>
or
<bean id="assembler" class="org.springframework.jmx.export.assembler.MethodExclusionMBeanInfoAssembler"
p:ignoredMethods="showHomePage"/>
- Beans with annotations
In app-spring.xml
<context:mbean-export server="mbeanServer"/>
User @ManagedResource, @ManagedOperation, @ManagedAttribute
@Controller
@ManagedResource(objectName="testObject:name=TestController")//
public class TestController{
...
@ManagedAttribute//
public void setSpittlesPerPage(int spittlesPerPage){
this.spittlesPerPage=spittlesPerPage;
}
@ManagedAttribute//
public int getSpittlesPerPage(){
return spittlesPerPage;
}
}
- Mbean Server
É o core do agente. Ele provê um registo para os Mbeans.
Permite que os clientes descubram e executem operações expostar pelos Mbeans.
Disponibiliza vários serviços para facilitar a gerência (ex: monitoramento, escalonamento, etc)
Usa “ObjectName” class para registrar os objetos com o MBeanServer
Lida com os recursos como MBeans
suporte Mbean para “managed bean”
MBeans podem representar um device físico ou uma aplicação.
Usuário decide quais atributos e métodos ele deseja expor para gerenciamento.
Usa design patters similares aos JavaBeans
MBeans são expostos em um agente.
- Java Management Extensions Remote API
To make MBeans available as remote objects:
<bean class="org.springframework.jmx.support.ConnectorServerFactoryBean"/>
Default: service:jmx:jmxmp://localhost:9875.
There are other remoting protocol options rather than JMXMP, including RMI, SOAP, IIOP.
To specify a diferent protocol:
<bean class="org.springframework.jmx.support.ConnectorServerFactoryBean"
p:serviceUrl="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter"/>
Accessing remote MBeans
JMX Remote API
Adiciona capacidade remota para a especificação JMX
Faz o agente JMX acessível fora da JVM.
Suporte padrão via RMI
Suporte opcional via TCP Sockets (JMXMP)
Suporta serviços de discovery/lookup e define segurança entre cliente e servidor
Assim como com código RMI, deve lidar com exceções de comunicação
Configure MBeanServerConnectionFactoryBean in app-spring.xml
Factory to create MBeanServerConnection.
<bean id="mBeanServerClient"class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean"
p:serviceUrl="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter"/>
MBeanServerConnection
Local proxy to the remote MBean server.
<bean id="jmxClient"class="com.springinaction.jmx.JmxClient">
<property name="mbeanServerConnection"ref="mBeanServerClient"/>
</bean>
Using the bean
int mbeanCount=mbeanServerConnection.getMBeanCount();
System.out.println("Thereare"+mbeanCount+"MBeans");
mbeanServerConnection.invoke(new ObjectName("spitter:name=HomeController"),
"setSpittlesPerPage",
new Object[]{100},
new String[]{"int"});
- Handging notifications
Nenhum comentário:
Postar um comentário