通常Web应用的停止、启动、重启只需要管理员来做,可是我有个系统为了能使应用切换数据库,而需要让用户来操作,其实用户不知道这么多内部操作,他只知道我点击了一个连接,然后应用就切换到另外一个数据库了。殊不知,后台还需要重启应用才能生效,于是我要使用程序来控制,我的应用服务器系统是WebSphere,以前是Tomcat,我用程序写了代码可以使Tomcat的应用启动、停止、重启操作,可是WebSphere我不知道如何写了,查找了相关资料,看到一些代码:
//--代码:
private void createAdminClient() {
// Set up a Properties object for the JMX connector attributes
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE,
AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, "192.168.0.211");
connectProps.setProperty(AdminClient.CONNECTOR_PORT, "8880");
connectProps.setProperty(AdminClient.USERNAME, "administrator");
connectProps.setProperty(AdminClient.PASSWORD, "abc");
connectProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED , "true");
try {
adminClient = AdminClientFactory.createAdminClient(connectProps);
} catch (ConnectorException e) {
System.out.println("Exception creating admin client.");
e.printStackTrace();
System.exit(-1);
} catch (Exception e){
System.out.println(e);
}
}
//--
private void invokeStopApplication(String serverName) {
// Use the launchProcess operation on the NodeAgent MBean to start
// the given server
String opName = "stopApplication";
String signature[] = { "java.lang.String" };
String params[] = { serverName };
boolean launched = false;
try {
Boolean b = (Boolean) adminClient.invoke(
nodeAgent, opName, params, signature);
launched = b.booleanValue();
if (launched)
System.out.println(serverName + " was stoped");
else
System.out.println(serverName + " was not stoped");
} catch (Exception e) {
System.out.println("Exception invoking stopApplication: " + e);
}
}
可是,似乎没有成功,WebSphere的配置我也看了,应该没什么问题。
我还把WebSphere的代码反编译了,可是看不懂,太复杂了,郁闷啊。
继续研究……