为什么JNDI资源只能在Tomcat中调用一次?

时间:2015-12-28 20:00:35

标签: java tomcat java-ee jndi

当我学习how to use JNDI resources in Tomcat 7时,已经制作了一个名为MyBeanFactory的自定义bean工厂。它为我的Web应用程序提供外部资源。在第一次,我们假设它提供当前系统时间戳。

然而,我意识到MyBeanFactory在第一次请求时只被调用一次。似乎第一次执行后,java bean MyBean已存储在Tomcat中。然后每个请求都会重用MyBean。无论何时在浏览器中保留URL,响应始终显示与第一个相同的时间戳。以下是可能有用的元素列表:

  • MyBean :由MyBeanFactory
  • 提供的bean
  • MyBeanFactory :实施javax.naming.spi.ObjectFactory
  • 的工厂
  • context.xml :资源的上下文描述符,位于META-INF文件夹中
  • web.xml :部署描述符
  • MyAction :http servlet
  • 控制台

有人可以告诉我为什么MyBeanFactory只被调用一次,是否可以更改此功能?因为我需要修改工厂以在服务器中与另一个进程建立套接字。见How to serve a socket from a Java EE application?

为myBean

public class MyBean {

    private String foo = "Default Foo";
    private long bar = 0;

    public String getFoo() { return (this.foo); }
    public void setFoo(String foo) { this.foo = foo; }      
    public long getBar() { return (this.bar); }
    public void setBar(long bar) { this.bar = bar; }

} 

MyBeanFactory

public class MyBeanFactory implements ObjectFactory {

    @Override
    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
            Hashtable<?, ?> environment) throws NamingException {
        System.out.println("MyBeanFactory starts");
        MyBean bean = new MyBean();
        bean.setBar(System.currentTimeMillis());
        // Return the customized instance
        return bean;

    } 
}

context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<Context
  path="/jndi"
  reloadable="true"
  cachingAllowed="false"
  antiResourceLocking="true">

  <Resource 
    name="bean/MyBeanFactory"
    auth="Container"
    type="com.mycompany.MyBean"
    factory="com.mycompany.MyBeanFactory"
    bar="23"/>

</Context>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  id="WebApp_ID"
  version="3.1">

  <display-name>jndi</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>com.mycompany.MyAction</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <!--
      Apache Tomcat 7: JNDI Resources HOW-TO
      https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
   -->
  <resource-env-ref>
    <description>
      Object factory for MyBean instances.
    </description>
    <resource-env-ref-name>
      bean/MyBeanFactory
    </resource-env-ref-name>
    <resource-env-ref-type>
      com.mycompany.MyBean
    </resource-env-ref-type>
  </resource-env-ref>
</web-app>

MyAction

public class MyAction extends HttpServlet {

    // ...

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        System.out.println("doGet");
        response.getWriter()
            .append("Hello coming from ")
            .append(request.getRequestURI());

        Context initCtx = null;
        try {
            System.out.println("initCtx");
            initCtx = new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
        }

        Context envCtx = null;
        try {
            System.out.println("envCtx");
            envCtx = (Context) initCtx.lookup("java:comp/env");
        } catch (NamingException e) {
            e.printStackTrace();
        }

        MyBean bean;
        try {
            System.out.println("lookup");
            bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
            response.getWriter()
                .append("foo = " + bean.getFoo() + ", ")
                .append("bar = " + bean.getBar() + ";");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        System.out.println("------");
    }
}

控制台

Dec 28, 2015 7:41:03 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 412 ms
doGet
initCtx
envCtx
lookup
MyBeanFactory starts  // called only once
------
doGet
initCtx
envCtx
lookup
------
doGet
initCtx
envCtx
lookup
------

1 个答案:

答案 0 :(得分:2)

您提供的<Resource>元素具有documented by Tomcat 8以外的属性,特别是&#34; factory&#34; &&#34; bar&#34;。但是,可能会从文档中错误地省略前一个属性,因为有些示例使用它。

无论如何,我想提请你注意&#34; singleton&#34;属性。当该属性的值为true(默认值)时,资源将被视为单例,其中单个实例由上下文维护并在所有用户之间共享。这似乎是您描述的行为。如果将该属性添加到声明中,值为false,则该资源的每次查找都应检索一个新实例。