您的位置:首页 > 编程语言 > Java开发

如何访问状态对JBoss详细的Java EE应用部署的7?

2013-12-06 17:30 337 查看
Im trying to write a small "watchdog" *.war that monitors the deployment state of my (much larger) *.ear on jboss 7.1.3

How do I get at the exact deployment state of the *.ear?

I know I can do this (using jboss MSC classes):

ServiceContainer sc = CurrentServiceContainer.getServiceContainer(); //jboss msc
ServiceController earController = sc.getService(Services.deploymentUnitName("my.ear"));
return "my.ear - "+earController.getMode()+"-"+earController.getState()+"-"+earController.getSubstate();

but this will give me the all-green even if deployment failed. for exmaple - say I have a @Startup @Singleton, who's @PostConstruct method (called as part of boot) throws an exception. at this point my deployment has logically failed (initialization threw
an exception) yet jboss will mark the .ear as deployed - both using the marker files in the deployment directory (.isDeploying --> *.deployed) and using the values from the controller above.

jboss does have a ContainerStateMonitor class that keeps a list of services with missing dependencies which is just what I need - any @Singletons that fails to start will cause a bunch of @EJBs that rely on it to fail to deploy - but I have no idea how to
get at it.

the closest I found was this:

sc.getService(org.jboss.as.serverServices.JBOSS_SERVER_CONTROLLER).getService()

this gets me an instance of
ServerService that has a controller (transient) field which holds that data. but its all in private fields and I really dont want to resort to reflection.

so my question is - is there any way to get at that data? jboss obviously knows what @Singletons failed to deploy, what @EJBs are missing dependencies, what datasources failed to connect etc, but is there a way for me to get to it? doesnt have to be MSC,
could be JMX (though I think that just maps to MSC in jboss 7) or any other API.

You could use the
management API and check the results.

The code would look something like:

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();
}
}
}

Note I am using the 7.2.0.Final version of the API which should work with older versions of JBoss AS7, JBoss EAP 6.x and WildFly.

This outputs a result like

{
"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"
}}
}}
}
}

Or if you just want the status you could change the above example slightly and do:

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));
}

2013年10月30日55分00秒
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: