如何在JBoss AS 7上访问详细的Java EE应用程序部署状态?
作者:互联网
我试图写一个小的“看门狗” * .war来监视我(更大)*.ear在jboss 7.1.3上的部署状态
我如何获得* .ear的确切部署状态?
我知道我可以做到这一点(使用jboss MSC类):
ServiceContainer sc = CurrentServiceContainer.getServiceContainer(); //jboss msc
ServiceController earController = sc.getService(Services.deploymentUnitName("my.ear"));
return "my.ear - "+earController.getMode()+"-"+earController.getState()+"-"+earController.getSubstate();
但是,即使部署失败,这也将使我获得全绿色.例如-说我有一个@Startup @Singleton,谁的@PostConstruct方法(称为启动的一部分)会引发异常.在这一点上,我的部署在逻辑上失败了(初始化引发了一个异常),但是jboss会将.ear标记为已部署-使用部署目录中的标记文件(.isDeploying-> * .deployed)并使用来自上面的控制器.
jboss确实有一个ContainerStateMonitor类,该类保留了我所需要的缺少依赖项的服务列表-任何无法启动的@Singletons都将导致一堆依赖它的@EJB无法部署-但我不知道如何做到这一点.
我发现的最接近的是:
sc.getService(org.jboss.as.serverServices.JBOSS_SERVER_CONTROLLER).getService()
这使我得到一个ServerService实例,该实例具有一个保存该数据的控制器(瞬态)字段.但是所有这些都在私人领域,我真的不想诉诸反思.
所以我的问题是-有没有办法获取这些数据? jboss显然知道哪些@Singletons部署失败,哪些@EJB缺少依赖项,哪些数据源连接失败等,但是我有办法吗?不必是MSC,也可以是JMX(尽管我认为只是映射到jboss 7中的MSC)或任何其他API.
解决方法:
您可以使用management API并检查结果.
代码如下所示:
import java.net.InetAddress;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.dmr.ModelNode;
public class ClientExample {
public static void main(final String[] args) throws Exception {
final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9999);
try {
final ModelNode address = new ModelNode().setEmptyList();
address.add("deployment", "jboss-as-helloworld.war");
final ModelNode op = Operations.createReadResourceOperation(address, true);
op.get(ClientConstants.INCLUDE_RUNTIME).set(true);
final ModelNode outcome = client.execute(op);
if (Operations.isSuccessfulOutcome(outcome)) {
System.out.println(outcome);
} else {
System.err.printf("Operation failure: %s%n", Operations.getFailureDescription(outcome));
}
} finally {
client.close();
}
}
}
注意我正在使用API的7.2.0.Final版本,该版本应与JBoss AS7,JBoss EAP 6.x和WildFly的较早版本一起使用.
这将输出类似
{
"outcome" => "success",
"result" => {
"content" => [{"hash" => bytes {
0xab, 0x77, 0x61, 0x49, 0x4b, 0x30, 0x3b, 0x4f,
0xd7, 0x80, 0x13, 0x5a, 0x6c, 0x48, 0x1e, 0x3d,
0xb3, 0xbe, 0xc1, 0xc2
}}],
"enabled" => true,
"name" => "jboss-as-helloworld.war",
"persistent" => true,
"runtime-name" => "jboss-as-helloworld.war",
"status" => "OK",
"subdeployment" => undefined,
"subsystem" => {"web" => {
"active-sessions" => 0,
"context-root" => "/jboss-as-helloworld",
"duplicated-session-ids" => 0,
"expired-sessions" => 0,
"max-active-sessions" => 0,
"rejected-sessions" => 0,
"session-avg-alive-time" => 0,
"session-max-alive-time" => 0,
"sessions-created" => 0,
"virtual-host" => "default-host",
"servlet" => {"org.jboss.as.quickstarts.helloworld.HelloWorldServlet" => {
"load-time" => 0L,
"maxTime" => 9223372036854775807L,
"min-time" => 0L,
"processingTime" => 0L,
"requestCount" => 0,
"servlet-class" => "org.jboss.as.quickstarts.helloworld.HelloWorldServlet",
"servlet-name" => "org.jboss.as.quickstarts.helloworld.HelloWorldServlet"
}}
}}
}
}
或者,如果您只是想要状态,则可以稍微更改上面的示例并执行以下操作:
final ModelNode op = Operations.createReadAttributeOperation(address, "status");
final ModelNode outcome = client.execute(op);
if (Operations.isSuccessfulOutcome(outcome)) {
System.out.println(Operations.readResult(outcome).asString());
} else {
System.err.printf("Operation failure: %s%n", Operations.getFailureDescription(outcome));
}
标签:jboss7-x,java,java-ee 来源: https://codeday.me/bug/20191123/2067023.html