package cn.rtdata.jmx; |
import java.io.IOException; |
import java.lang.management.ManagementFactory; |
import java.rmi.registry.LocateRegistry; |
import java.util.HashMap; |
import java.util.Map; |
import java.util.Properties; |
import javax.management.InstanceAlreadyExistsException; |
import javax.management.MBeanRegistrationException; |
import javax.management.MBeanServer; |
import javax.management.MalformedObjectNameException; |
import javax.management.NotCompliantMBeanException; |
import javax.management.ObjectName; |
import javax.management.remote.JMXConnectorServer; |
import javax.management.remote.JMXConnectorServerFactory; |
import javax.management.remote.JMXServiceURL; |
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
import cn.rtdata.Config; |
import com.sun.jdmk.comm.AuthInfo; |
import com.sun.jdmk.comm.HtmlAdaptorServer; |
public class JMXService { |
private static Log logger = LogFactory.getLog(JMXService. class ); |
private MBeanServer mbs = null ; |
private HtmlAdaptorServer htmlAdaptor = null ; |
private JMXConnectorServer cs = null ; |
private static JMXService jmx = null ; |
public boolean init() { |
Config config = Config.getInstance(); |
// 取得JMX配置信息 |
Properties jmxPro = config.getJmxPrams(); |
try { |
mbs = ManagementFactory.getPlatformMBeanServer(); |
// htmlAdaptor |
htmlAdaptor = new HtmlAdaptorServer(); |
final ObjectName htmlAdaptorON = new ObjectName( |
"com.example.mbeans:name=HtmlAdaptor" ); |
int port = Integer.parseInt(jmxPro.getProperty( "htmlAdaptorPort" , |
"9876" )); |
htmlAdaptor.setPort(port); |
// htmlAdaptor验证信息 |
AuthInfo login = new AuthInfo(); |
login.setLogin(jmxPro.getProperty( "htmlUser" )); |
login.setPassword(jmxPro.getProperty( "htmlPasswd" )); |
// 添加用户验证 |
htmlAdaptor.addUserAuthenticationInfo(login); |
// 注册该模块 |
mbs.registerMBean(htmlAdaptor, htmlAdaptorON); |
System.out.println( "注册应用程序启动入口" ); |
Launcher_jmxMBean launcher = new Launcher_jmx(); |
mbs.registerMBean(launcher, new ObjectName( |
"cn.rtdata:launcher=ILauncherMBean" )); |
htmlAdaptor.start(); |
System.out.println( "启动JMX的HtmlAdaptor,端口号:" + port); |
int jmxServiceURIPort = Integer.parseInt(jmxPro.getProperty( |
"jmxServiceURIPort" , "9999" )); |
// if(LocateRegistry.getRegistry(jmxServiceURIPort)==null)s |
LocateRegistry.createRegistry(jmxServiceURIPort); |
String jmxServiceURI_ = jmxPro.getProperty( "jmxServiceURI" ); |
String jmxServiceURI = jmxServiceURI_.substring( 1 , |
jmxServiceURI_.length() - 1 ); |
// 密码文件 |
/** |
* 内容为键值对 admin 111111 user 111111 |
*/ |
String userFile = "config//jmx//passwd.txt" ; |
Map<String, String> env = new HashMap<String, String>(); |
env.put( "jmx.remote.x.password.file" , userFile); |
JMXServiceURL url = new JMXServiceURL(jmxServiceURI); |
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, |
mbs); |
cs.start(); |
} catch (MalformedObjectNameException e) { |
logger.error( "JMX服务启动异常" , e); |
e.printStackTrace(); |
return false ; |
} catch (InstanceAlreadyExistsException e) { |
logger.error( "JMX服务启动异常" , e); |
e.printStackTrace(); |
return false ; |
} catch (MBeanRegistrationException e) { |
logger.error( "JMX服务启动异常" , e); |
e.printStackTrace(); |
return false ; |
} catch (NotCompliantMBeanException e) { |
logger.error( "JMX服务启动异常" , e); |
e.printStackTrace(); |
return false ; |
} catch (NullPointerException e) { |
logger.error( "JMX服务启动异常" , e); |
e.printStackTrace(); |
return false ; |
} catch (IOException e) { |
logger.error( "JMX服务启动异常" , e); |
e.printStackTrace(); |
return false ; |
} |
return true ; |
} |
public static JMXService getInstance() { |
if (jmx == null ) { |
jmx = new JMXService(); |
} |
return jmx; |
} |
private JMXService() { |
} |
public void stopService() { |
if (htmlAdaptor != null ) |
htmlAdaptor.stop(); |
if (cs != null ) { |
try { |
cs.stop(); |
} catch (IOException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
} |
} |