4

我在 Glassfish 4.0-b87 下运行一个简单的 .war 文件,该文件是使用 Oracle Java 7 JDK/JVM 在 Eclipse Kepler M6 中使用 M2E 作为添加了动态 Web 模块方面的简单项目创建的。

本质上只有一个类,我希望该post_construct方法只被调用一次。但是它被调用了两次,第一次是在部署应用程序时,然后在第一个也是第一个 HTTP 请求上再调用一次。

这是类(日志输出如下):

package com.example.main;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.LocalBean;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;

@Singleton
@Startup
@LocalBean
@ApplicationPath("/rest")
@Path("/life")
public class Life extends Application implements PassivationCapable {

  @PostConstruct
  public void post_construct () {
    System.out.println("hello world!");
  }

  @PreDestroy
  public void pre_destroy () {
    System.out.println("so long and thanks for the fish!");
  }

  @PrePassivate
  public void pre_passivate () {
    System.out.println("taking a break");
  }

  @PostActivate
  public void post_activate () {
    System.out.println("back from break");
  }

  @Override
  public String getId () {
    return "life";
  }

  @GET
  public String greet () {
    return "hi";
  }
}

日志输出如下,用虚线分隔第一次 HTTP 调用之前的部分和 HTTP 调用之后的部分:

[2013-05-09T19:42:26.660-1000] [glassfish 4.0] [INFO] [NCLS-DEPLOYMENT-00027] [javax.enterprise.system.tools.deployment.autodeploy] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546660] [levelValue: 800] [[
  Selecting file /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/singleton.war for autodeployment]]

[2013-05-09T19:42:26.692-1000] [glassfish 4.0] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546692] [levelValue: 800] [[
  visiting unvisited references]]

[2013-05-09T19:42:26.702-1000] [glassfish 4.0] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546702] [levelValue: 800] [[
  visiting unvisited references]]

[2013-05-09T19:42:26.706-1000] [glassfish 4.0] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546706] [levelValue: 800] [[
  visiting unvisited references]]

[2013-05-09T19:42:26.709-1000] [glassfish 4.0] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546709] [levelValue: 800] [[
  visiting unvisited references]]

[2013-05-09T19:42:26.749-1000] [glassfish 4.0] [INFO] [ejb.portable_jndi_names] [javax.enterprise.system.container.ejb.com.sun.ejb.containers] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546749] [levelValue: 800] [[
  EJB5181:Portable JNDI names for EJB Life: [java:global/singleton/Life, java:global/singleton/Life!com.example.main.Life]]]

[2013-05-09T19:42:26.959-1000] [glassfish 4.0] [WARNING] [] [org.jboss.weld.Bootstrap] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546959] [levelValue: 900] [[
  WELD-001473 javax.enterprise.inject.spi.Bean implementation org.glassfish.jms.injection.JMSCDIExtension$LocalBean@cbcbf4a declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.]]

[2013-05-09T19:42:26.970-1000] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=59 _ThreadName=Thread-3] [timeMillis: 1368164546970] [levelValue: 800] [[
  hello world!]]

[2013-05-09T19:42:26.982-1000] [glassfish 4.0] [INFO] [] [org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546982] [levelValue: 800] [[
  Registering the Jersey servlet application, named com.example.main.Life, at the servlet mapping /rest/*, with the Application class of the same name.]]

[2013-05-09T19:42:26.988-1000] [glassfish 4.0] [INFO] [AS-WEB-GLUE-00172] [javax.enterprise.web] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164546988] [levelValue: 800] [[
  Loading application [singleton] at [/singleton]]]

[2013-05-09T19:42:27.003-1000] [glassfish 4.0] [INFO] [] [javax.enterprise.system.core] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164547003] [levelValue: 800] [[
  singleton was successfully deployed in 332 milliseconds.]]

[2013-05-09T19:42:27.006-1000] [glassfish 4.0] [INFO] [NCLS-DEPLOYMENT-00035] [javax.enterprise.system.tools.deployment.autodeploy] [tid: _ThreadID=59 _ThreadName=AutoDeployer] [timeMillis: 1368164547006] [levelValue: 800] [[
  [AutoDeploy] Successfully autodeployed : /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/singleton.war.]]

------------------------------在第一次 HTTP 调用之后---- --------------

[2013-05-09T19:42:44.940-1000] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=21 _ThreadName=Thread-3] [timeMillis: 1368164564940] [levelValue: 800] [[
  hello world!]]

[2013-05-09T19:42:44.940-1000] [glassfish 4.0] [INFO] [] [org.glassfish.jersey.server.ApplicationHandler] [tid: _ThreadID=21 _ThreadName=http-listener-1(4)] [timeMillis: 1368164564940] [levelValue: 800] [[
  Initiating Jersey application, version Jersey: 2.0-rc2 2013-04-23 12:04:25...]]

[2013-05-09T19:42:44.956-1000] [glassfish 4.0] [INFO] [] [org.glassfish.jersey.gf.ejb.EjbComponentProvider] [tid: _ThreadID=21 _ThreadName=http-listener-1(4)] [timeMillis: 1368164564956] [levelValue: 800] [[
  The Jersey EJB interceptor is bound. JAX-RS EJB integration support is enabled.]]
4

1 回答 1

1

<update>这是一个 Glassfish 错误https://java.net/jira/browse/GLASSFISH-20505 </update>

似乎@Singleton, 和@ApplicationPath+extends Application彼此之间不太好相处。也许行为是正确的,因为 JAX-RS 需要将Application类实例化一次,而 EJB 需要实例化@Singleton一次,但在有人指出标准的相关部分之前,最好将其视为错误。Jersey 应该识别@Singleton注释并寻找extends Application类而不是创建自己的类。

可能的 Jersey 错误的解决方法是不使用与@SingletonJersey 类相同的Application类。

于 2013-05-10T20:43:15.840 回答