If you have spring experience, you might know what MBeanRegistration interface is for.In fact, in my point of view, it acts as BeanFactoryAware interface in Spring framework. BeanFactoryAware is used for beans implemented this interface to hold a reference to a bean factory, then later it can look up collaborating beans via this beanFactory reference.
From the picture above shows, the preRegister() method requires a MBeanServer parameter,through this way, the MBean implements MBeanRegistration can hold the reference to a MBeanServer. We can even image how MBeanRegistration works with MBeanServer, when registering MBeans in a MBeanServer, if the registered MBean implements MBeanRegistration, it will invoke preRegister() method before registering MBean in MBeanServer, and invoke postRegister() method after MBean registering.
#MBean Server
public ObjectInstance registerMBean(Object object, ObjectName name)
{
...
if(object instanceof MBeanRegistration)
(MBeanRegistration).preRegister(this,name);
// do registering
boolean isSuccessful=register(object,name);
if(object instanceof MBeanRegistration)
(MBeanRegistration).postRegister(isSuccessful);
}
And preDeregister() and postDeregister() are for unregistering case.
Although MBeanRegistration often acts like a BeanFactoryAware, but its initial purpose is to carry out operations before or after MBean registering from the MBeanServer.
After examining the MBeanRegistration, let's talk about MBeanServer. First, let's review some basic functions of MBeanServer. As we saw in previous examples, we have used MBeanServer to register mbeans. Besides that, MBean Server can also handle notification listener(add or remove), get or set attributes of mbean by their object name. As the below class diagram shows:
Method |
Description |
ObjectInstance registerMBean(Object object, ObjectName name) |
register an object(MBean) to MBean Server by its object name |
void unregisterMBean(ObjectName name) |
unregister mbean from the MBean Server by its object name |
ObjectInstance createMBean(String className, ObjectName name) |
create a MBean via its classname and register it to the MBean Server |
addNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback) |
add listener via MBean Server, in fact, MBean Server first look up the corrsponding MBean by the first parameter object name, the found MBean must be a NotificationBroadCaster, otherwise MBean server will throw an exception, then MBean server uses the found NotificationBroadCaster to add the listener. |
Object getAttribute(ObjectName name, String attribute) |
get the value of attribute from the MBean which matches the object name |
void setAttribute(ObjectName name, Attribute attribute) |
set the value of attribute from the MBean which matches the object name |
In addition to the basic functions metioned above, MBean Server also provides a mechanism of querying mbeans, as we know, you could easily have more than 50 or 100 mbeans residing in your application, so that is the place where mbean server querying mechanism could play.
Method |
Description |
Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) |
return the set of ObjectInstance objects identifying the set of MBeans by the given query |
Set<ObjectName> queryNames(ObjectName name, QueryExp query) |
return the set of ObjectName objects identifying the set of MBeans by the given query |
If we want to know how to query mbeans via MBean Server, then you have to know how the query mechanism works first. The query mechanism of MBean Server actually consists of two parts: QueryExp and ValueExp.
The above picture only shows the commonly used QueryExp, QueryExp is used to work with ValueExp or combine different QueryExp in order to build complex queries.
From the picture above, we could easily infer their purposes from their name,for example, BooleanValueExp acts as the wrapper of the boolean value and the AttibuteValueExp refers to the value of the attribute of the MBean. Before we move into some concreate examples, we need to examine the parent class of QueryExp and ValueExp,QueryEval.
public abstract class QueryEval implements Serializable {
/* Serial version */
private static final long serialVersionUID = 2675899265640874796L;
private static ThreadLocal<MBeanServer> server =
new InheritableThreadLocal<MBeanServer>();
public void setMBeanServer(MBeanServer s) {
server.set(s);
}
public static MBeanServer getMBeanServer() {
return server.get();
}
}
As we saw from the code, QueryEval uses ThreadLocal to avoid multi-thread problem. You might be wondering why QueryEval holds MBean Server reference. Recall from the class diagram of QueryExp and ValueExp, you can see that we have a 'setMBeanServer()' method in QueryExp/ValueExp interface. QueryEval provides a default implementation for setMBeanServer(MBeanServer) of QueryExp and ValueExp, that's why some QueryExps or ValueExps extend from QueryEval.
But from the usage of QueryEval, I see no need for QueryExp or ValueExp to extend from QueryEval. Take AttributeValueExp for example, as we mentioned before, AttributeValueExp indicates the value of attribute of the MBean, in order to get the attribute value from the MBean, we need to get the MBean Server first. And then get the attribute value via its getAttribute(ObjectName objName,String attribute).
#AttributeValueExp
protected Object getAttribute(ObjectName name) {
try {
// Get the value from the MBeanServer
MBeanServer server = QueryEval.getMBeanServer();
return server.getAttribute(name, attr);
} catch (Exception re) {
return null;
}
}
And when we query MBeans via MBean Server, we'll first set the MBean Server, then call the QueryExp's apply() method.
#queryMBeans
for (ObjectInstance oi : list) {
boolean res = false;
MBeanServer oldServer =QueryEval.getMBeanServer();
query.setMBeanServer(server);
try {
res = query.apply(oi.getObjectName());
} catch (Exception e) {
res = false;
} finally {
query.setMBeanServer(oldServer);
}
}
In fact, we can replace the sentence 'query.setMBeanServer(server)' with the following one since query.setMBeanServer is actually QueryEval.setMBeanServer
QueryEval.setMBeanServer(server)
Therefore, there is no need for QueryExp and ValueExp to expose the setMBeanServer() method since we all know that MBeanServer for QueryExp and ValueExp is thread-related, not QueryExp-related or ValueExp-related.And setMBeanServer() method of ValueExp has been depreciated. And I think the one in QueryExp should be depreciated too, since they both can access the MBean Server via using QueryEval.getMBeanServer().
All right, now, let's take a look at how QueryExp and ValueExp work together to form a query. Assume that we have a person MBean which has two attributes, name and age. And we have more than 50 person MBeans residing in a MBean Server, now we want to find a suitable person MBean which name is 'kevin', and we can build our query as follow:
//int 4 means the operation equals
QueryExp query=new BinaryRelQueryExp(4,new AttributeValueExp("name"),new StringValueExp("kevin")
And if we want to find a suitable person MBean which name is 'kevin' and his age is greater than 23, we can build our query as below:
QueryExp nameQuery=new BinaryRelQueryExp(4,new AttributeValueExp("Name"),new StringValueExp("kevin"));
QueryExp ageQuery=new BinaryRelQueryExp(2,new AttributeValueExp("Age"),new NumericValueExp(23));
QueryExp andQuery=new AndQueryExp(nameQuery,ageQuery);
Some of the QueryExp such as AndQueryExp, OrQueryExp can combine other query expressions together to build a complex query.
In fact, you'll find that BinaryRelQueryExp or AndQueryExp can not be imported into your class since they are not public. But we can use Query class to build queries. Let's rewrite the last example with Query.
QueryExp nameQuery=Query.eq(Query.attr("Name"), Query.value("kevin"));
QueryExp ageQuery=Query.gt(Query.attr("Age"), Query.value(23));
QueryExp andQuery=Query.and(nameQuery, ageQuery);
And it's much shorter, isn't? Well, using the querying mechanism, you can retrieve specific MBeans without manually examing the contents of each to get the one you need.
- 大小: 2.9 KB
- 大小: 6.9 KB
- 大小: 6.7 KB
分享到:
相关推荐
**JMX(一)——MBean Server** Java Management Extensions (JMX) 是Java平台上的一个标准,用于管理和监控应用程序、设备和服务。它提供了一个灵活的框架,使得开发者能够轻松地创建可管理的组件,并通过标准接口...
2. **MBeanServer操作**:熟悉MBeanServer提供的API,如`createMBean()`、`queryMBeans()`等,用于管理MBean。 3. **MBean的交互**:学习如何通过MBeanServer获取和设置MBean的属性,调用其操作方法。 4. **连接器和...
Spring JMX的核心概念包括MBean(Managed Bean)、MBeanServer和代理(MBeanProxy)。MBean是JMX中的基本管理单元,代表一个可管理的资源,可以是一个对象、服务或任何其他需要管理的组件。MBeanServer则是管理这些...
4. **Protocol Adapters and Connectors**: - 这些组件负责JMX Agent与外部世界的通信。Adapter通过特定协议(如HTTP、SNMP)连接到JMX Agent,处理协议相关的细节。Connector则在Agent和客户端之间提供通信接口,...
1. **JMX架构**:JMX架构由三个主要组件构成:MBeans(Managed Beans)、MBeanServer和代理(Agents)。MBeans代表可管理的资源,它们是JMX的核心;MBeanServer是运行MBeans并处理管理操作的实体;代理则用于暴露...
在这个例子中,`MyMBeanImpl`是一个实现了特定MBean接口的类,它可以通过`ObjectName`指定的名称注册到MBeanServer中。 ### 结论 JMX为Java平台提供了一套强大的管理工具和技术,使得开发者能够轻松地管理和监控...
2. **MBeanServer**: MBeanServer是管理MBean的中心实体,每个JVM通常只有一个MBeanServer。所有MBean都需要注册到这个服务器上,以便通过MBeanServer对外提供服务。开发者可以使用`ManagementFactory....
**Protocol Adapters and Connectors** 用于JMX Agent与其他系统通信。适配器(Adapter)处理特定协议(如HTTP、SNMP),而连接器(Connector)则在Agent端和客户端都有相应组件,处理请求和响应。例如,RMI ...
2. **MBeanServer**:MBeanServer是JMX的核心组件,负责注册MBean,执行MBean的操作,并提供查询和管理MBean的能力。 3. **MBeanInfo**:描述MBean的属性、操作、构造函数、通知等元数据。 4. **Notification**:...
MBeanServer提供了注册机制,允许外部客户端通过特定的适配器(Adapter)和连接器(Connector)访问MBean。客户端可以使用MBeanServer的名字服务找到并操作对应的MBean实例。 JMX Agent是一个包含MBeanServer和附加...
JMI是基于 OMG 的 Management and Instrumentation (DMI) 规范,允许开发者创建可管理的应用程序,并通过管理代理进行远程控制。在这个示例中,我们有两个关键文件:`JmiServer` 和 `JmiClient`。 **JmiServer** `...
MBeans可以是标准的、开放的或私有的,它们通过MBeanServer进行注册,然后可以通过JMX客户端工具进行访问和操作。MBeanServer是JMX架构的核心,它负责管理MBeans,并提供查询、通知和远程访问等功能。 "jmx-1_2_1-...
3. Connector:Connector是一种通信协议,用于连接MBeanServer和外部管理应用程序。 二、JMX架构组件 JMX架构主要由五个组件组成: 1. MBean:MBean是JMX架构的核心组件,提供了管理接口,用于监控和控制Java应用...
综上所述,"JMX开发步骤.doc"可能涵盖了JMX的基本概念、MBean的创建与注册、MBeanServer的使用、管理接口设计、连接器配置、安全管理实践以及如何在实际项目中应用JMX等详细内容。这份文档对于学习和理解JMX技术,...
它允许开发者创建可管理的组件,即MBeans(Managed Beans),并通过MBeanServer进行交互。JMX提供了一种标准的方式来暴露应用程序的内部状态和操作,使得管理员可以通过管理控制台或者远程接口进行管理。 **一、JMX...
`Main`类负责创建MBeanServer并将`ServerMonitor`实例注册为MBean,最后通过MBeanServer获取服务器运行时间。 ##### Dynamic MBean Dynamic MBean相比Standard MBean更加灵活,它允许在运行时动态地添加或删除属性...
在我们的示例中,我们使用JDK 1.5或更高版本,使用RMI连接方式来连接MBeanServer和Client。我们在机器23上实现了Router类,并在机器26上实现了Client,用于远程访问Router类提供的管理功能。 Router类是一个简单的...
总结来说,JMX提供了一种统一的管理和监控机制,通过MBeans、MBeanServer和JMX Connectors,开发者可以轻松地为Java应用程序添加管理和监控功能。理解并熟练运用JMX对于构建可扩展、可维护的大型Java系统至关重要。
JMX 包括管理对象(MBeans)、代理(MBeanServer)和连接器(Connector),通过这些元素,可以实现对应用程序的全面监控。 二、MBeans MBeans是JMX的核心,代表可管理的实体或资源。它们可以是简单的数据对象,也...