为什么JNDI资源只能在Tomcat中调用一次? [英] Why JNDI resource can only be called once in Tomcat?

查看:181
本文介绍了为什么JNDI资源只能在Tomcat中调用一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I was learning how to use JNDI resources in Tomcat 7, a customized bean factory has been made, called MyBeanFactory. It provides external resources to my web application. At the first time, we assume that it provides the current system timestamp.

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

However, I realised that MyBeanFactory was called only once, at the first request. It seems that the java bean MyBean had be stored in Tomcat after the first execution. Then MyBean is reused by every request. No matter when I retape the URL in browser, the response always showed the same timestamp as the first one. Here're a list of elements that might help :


  • MyBean :由<$ c $提供的bean c> MyBeanFactory

  • MyBeanFactory :工厂实现 javax.naming.spi.ObjectFactory

  • context.xml :资源的上下文描述符,位于META-INF文件夹中

  • web.xml :部署描述符

  • MyAction :http servlet

  • 控制台

  • MyBean: the bean provided by MyBeanFactory
  • MyBeanFactory: the factory implementing javax.naming.spi.ObjectFactory
  • context.xml: the context descriptor for resources, located in META-INF folder
  • web.xml: deployment descriptor
  • MyAction: the http servlet
  • console

有人可以告诉我为什么 MyBeanFactory 只被调用一次如果可以更改此功能?因为我需要修改工厂以在服务器中与另一个进程建立套接字。请参阅如何从Java EE应用程序?

Could somebody tell me why MyBeanFactory is called only once and if it is possible to change this feature ? Because I need to modify the factory to establish a socket with another process in the server. See 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
------


推荐答案

< ;资源与GT; 元素您目前已超出由Tomcat 8记录,特别是工厂和酒吧。但是,可能会从文档中错误地省略前一个属性,因为有些示例使用它。

The <Resource> element you present has attributes beyond those documented by Tomcat 8, specifically "factory" and "bar". Perhaps the former attribute is erroneously omitted from the docs, however, because some of the examples use it.

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

In any event, I would like to draw your attention to the "singleton" attribute. When the value of that attribute is true (the default), the resource is treated as a singleton, with a single instance maintained by the context and shared among all users. This seems to be the behavior you describe. If you add that attribute to your declaration, with value false, then each lookup of that resource should retrieve a new instance.

这篇关于为什么JNDI资源只能在Tomcat中调用一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆