我有一个jerey(2.26)webapplication。我的目的是监听应用程序事件,例如"应用程序已初始化"或者"应用程序被销毁"并启动|停止外部数据库连接。这里有一个基本概述(https://jersey.github.io/documentation/latest/monitoring_tracing.html)。为此,我使用 ApplicationEventListener 界面并注册其实现的类 " MyApplicationEventListener" 作为提供者。我在 wildfly-10.1.0.Final 上部署它,似乎wildfly运行时可以识别 MyApplicationEventListener 作为提供者
但是没有记录或捕获任何应用程序事件?
我使用web.xml少部署。所以我的所有应用程序都是基于注释的。以下是我使用的代码片段。 Wildfly服务器日志
09:17:02,866 INFO [org.jboss.as.repository] (management-handler-thread - 3) WFLYDR0001: Content added at location /home/kalpa/jeeservers/wildfly-10.1.0.Final/standalone/data/content/7a/2241598098ebadae4083289691ad2bd3768097/content
09:17:02,925 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 69) WFLYUT0022: Unregistered web context: /butterfly
09:17:03,232 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0028: Stopped deployment butterfly.war (runtime-name: butterfly.war) in 319ms
09:17:03,234 INFO [org.jboss.as.server.deployment] (MSC service thread 1-7) WFLYSRV0027: Starting deployment of "butterfly.war" (runtime-name: "butterfly.war")
09:17:05,061 INFO [org.jboss.weld.deployer] (MSC service thread 1-6) WFLYWELD0003: Processing weld deployment butterfly.war
09:17:05,488 INFO [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 72) Initializing Mojarra 2.2.13.SP1 20160303-1204 for context '/butterfly'
09:17:06,493 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 72) RESTEASY002225: Deploying javax.ws.rs.core.Application: class butterfly.v1.rest.AppConfig
09:17:06,503 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 72) RESTEASY002200: Adding class resource butterfly.v1.rest.AccountService from Application class butterfly.v1.rest.AppConfig
09:17:06,503 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 72) RESTEASY002205: Adding provider class butterfly.v1.rest.MyApplicationEventListener from Application class butterfly.v1.rest.AppConfig
09:17:06,511 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 72) WFLYUT0021: Registered web context: /butterfly
09:17:06,543 INFO [org.jboss.as.server] (management-handler-thread - 3) WFLYSRV0016: Replaced deployment "butterfly.war" with deployment "butterfly.war"
09:17:06,566 INFO [org.jboss.as.repository] (management-handler-thread - 3) WFLYDR0002: Content removed from location /home/kalpa/jeeservers/wildfly-10.1.0.Final/standalone/data/content/8a/1139838573568dcc6e81b42dd00592b4dbb517/content
的build.gradle
dependencies {
compile 'javax:javaee-api:7.0'
compile 'org.mongodb:mongodb-driver:3.5.0'
compile 'javax.ws.rs:javax.ws.rs-api:2.0'
compile 'org.glassfish.jersey.core:jersey-common:2.26'
compile 'org.glassfish.jersey.core:jersey-client:2.26-b03'
compile 'org.glassfish.jersey.core:jersey-server:2.26'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.26'
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.13'
compile 'org.json:json:20160810'
testCompile 'org.testng:testng:6.10'
testCompile 'org.mockito:mockito-core:2.+'
}
应用程序类(我也使用过jersey的ResourceConfig。但没有运气)
@ApplicationPath(value = "/apiHub")
public class AppConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
// register root resource
classes.add(AccountService.class);
classes.add(MyApplicationEventListener.class);
return classes;
}
public Map<String, Object> getProperties() {
final Map<String, Object> properties = new HashMap<String, Object>();
// Enable Tracing support.
properties.put(ServletProperties.JAXRS_APPLICATION_CLASS, "butterfly.v1.rest.AppConfig.class");
properties.put(ServerProperties.APPLICATION_NAME, "Butterfly");
properties.put(ServerProperties.TRACING, "ALL");
properties.put(ServerProperties.MONITORING_ENABLED, true);
properties.put(ServerProperties. MONITORING_STATISTICS_ENABLED, true);
properties.put(ServerProperties.PROVIDER_PACKAGES, "butterfly.v1.rest");
return properties;
}
}
应用程序事件监听器类
public class MyApplicationEventListener implements ApplicationEventListener {
private volatile int requestCnt = 0;
@Override
public void onEvent(ApplicationEvent event) {
switch (event.getType()) {
case INITIALIZATION_FINISHED:
System.out.println("Application "+ event.getResourceConfig().getApplicationName()+ " was initialized.");
break;
case DESTROY_FINISHED:
System.out.println("Application "+ event.getResourceConfig().getApplicationName() + "destroyed.");
break;
default:
System.out.println("Default case");
break;
}
}
@Override
public RequestEventListener onRequest(RequestEvent requestEvent) {
requestCnt++;
System.out.println("Request " + requestCnt + " started.");
// return the listener instance that will handle this request.
return new MyRequestEventListener(requestCnt);
}
}