编写自己的标准MBean
让我们创建一个标准MBean来管理Web服务器.
以下为属性:
- ServerName: 服务器运行的物理机器名称.
- ServerId: An identifier for the Server. In a same machine, multiple servers may run at different ports.
- ServerStarted: 检查服务器是否开着.
- Port: T服务器监听端口.
以下为操作:
- startService: 启动服务器
- stopService: 停止服务器
作为一个标准MBean,应继承它自己的相应的MBean接口.
首先,我们将定义这个标准MBean的接口.如果要定义标准MBean名称为ServerInfo.因此,管理接口应为ServerInfoMBean.
public abstract interface ServerInfoMBean
{
public String getServerName( );
public String getServerId( );
public boolean isServerStarted( );
public int getPort( );
public void setPort(int port);
public void startService( );
public void stopService( );
}
从以上接口定义,MBean 有getter方法的属性有ServerName, ServerId, ServerStarted, and Port.setter方法只有Port属性有.则只有Port属性是可读写的,其他都为只读.
public class ServerInfo implements ServerInfoMBean
{
private String serverName = null;
private String serverId = null;
private boolean serverStarted;
private int port;
// At least one public constructor is required
public ServerInfo( )
{
serverName = "test-server";
serverId = "test-server_1";
serverStarted = true;
port = 8072;
}
// overloaded public constructor
public ServerInfo(String serverName, String serverId, boolean serverStarted, int port)
{
this.serverName = serverName ;
this.serverId = serverId ;
this.serverStarted = serverStarted ;
this.port = port ;
}
// Implementating the ServerInfoMBean
public String getServerName( )
{
// get the ServerName
return serverName;
}
public String getServerId( )
{
// get the ServerIdentifier
return serverId;
}
public boolean isServerStarted( )
{
// check whether the Server is Started
return serverStarted;
}
public int getPort( )
{
// get the ServerPort
return port;
}
public void setPort(int port)
{
// Stop the server. Set the ServerPort. Start the server.
stopService( );
this.port = port;
startService( );
}
public void startService( )
{
System.out.println("Starting server..... ");
// start the Server
System.out.println("Server started successfully. ");
serverStarted =true;
}
public void stopService( )
{
System.out.println("Stopping server..... ");
// stop the Server
System.out.println("Server stopped ");
serverStarted =false;
}
// Additional methods not exposed for management
public void restart( )
{
stopService( );
startService( );
}
}
属性名称重载
We have already seen in the Standard MBean section that attribute names cannot be overloaded, that is, there cannot be two setters or getter and setter pair for the same name that operates on different types. Let us see this with some examples.
写一个简单的MBean实现类Server实现ServerMBean接口.
Case 1
两个setter已经被重载:
public class Server implements ServerMBean
{
.......
public void setState(int state)
{
// implementation of setState
}
public void setState(boolean flag)
{
// implementation of setState
}
}
在以上的case中,两个setter被重载,它不可能确定属性State是int类型还是boolean类型.于是,上面的类不是合法的类.
Case 2
一对相同名字不同类型的getter和setter方法:
public class Server implements ServerMBean
{
.......
public int getState()
{
// implementation of getState
}
public void setState(boolean flag)
{
// implementation of setState
}
}
For the above case, it is not possible to determine whether State is a readOnly attribute of type int or a writeOnly attribute of type boolean. 于是,上面的类也不是合法的MBean.
特别的类
以下的类是可能的:
Case 1
public class Server implements ServerMBean
{
.......
public void setState(int state)
{
// implementation of setState
}
public void setState(boolean flag, int state)
{
// implementation of setState
}
}
Case 2
public class Server implements ServerMBean
{
.......
public int getState()
{
// implementation of getState
}
public int getState(boolean flag)
{
// implementation of getState
}
}
Case 1中,setState(boolean flag, int state) 将作为一个操作处理.
Case 2中,getState(boolean flag) 也作为一个操作处理.
发表于 @ 2008年02月17日 05:29:00|评论(loading...)