我怎样才能创建的Apache Tomcat启动一个单 [英] How can I create a singleton in Apache Tomcat startup

查看:202
本文介绍了我怎样才能创建的Apache Tomcat启动一个单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个单独的Apache Tomcat启动时,这样我就可以在servlet访问它们。单身定义将在servlet的给什么反应。

I need to create a singleton when Apache Tomcat starts, so that I can access them with servlets. The singleton defines what response will the servlets give.

我想知道是否tomcat的具有构造函数,因此我可以添加code,这样的单身可以被创建。

I wanted to know whether tomcat has a constructor so I can add code so that the singleton can be created.

*编辑:搜索一点后,我发现我可以尝试使用一个Web服务(JAX-WS)。
我不知道在JVM中的Web服务如何处理实例虽然。我可以访问不同的连接在同一个对象的WS?

*edit: after searching a little, I found that I could try using a web service (JAX-WS). I don't know how the jvm treats instances in the web-service though. Can I access the same object on different connections to the WS?

我用bmargulies液中加入这些资源到的的web.xml 的描述:

I used bmargulies solution adding these resources to the web.xml descriptor:

<resource-env-ref>
  <description>
     Factory for the Arduino Connection
  </description>
  <resource-env-ref-name>
     arduino/ArduinoConnectionFactory
  </resource-env-ref-name>
  <resource-env-ref-type>
     br.com.evans.jndi.basic.ArduinoConnection
  </resource-env-ref-type>
</resource-env-ref>

这到的 context.xml中的:

<Context>
  <Resource name="arduino/ArduinoConnectionFactory" auth="Container"
        type="br.com.evans.jndi.basic.ArduinoConnection"
        factory="br.com.evans.jndi.basic.ArduinoConnectionFactory"/>
</Context>

创造了单例类:

public enum ArduinoConnection implements SerialPortEventListener {
    INSTANCE;
    public void initialize() {...}
}

创建单身工厂:

public class ArduinoConnectionFactory implements ObjectFactory {

    public ArduinoConnectionFactory() {
        ArduinoConnection.INSTANCE.initialize();
        try {
            Thread.sleep(1800);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Object getObjectInstance(Object obj, Name name, 
        Context nameCtx,Hashtable environment) throws NamingException {
            // Return the customized instance
            return (ArduinoConnection.INSTANCE);
        }
    }

和最后这在servlet get函数:

and finally this to a get function in a servlet:

    Context initCtx;
    try {
        initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        ArduinoConnection arduino = (ArduinoConnection) envCtx.lookup("arduino/ArduinoConnectionFactory");
        arduino.doSomething();
    } catch (NamingException e) {
        System.out.println("Something went wrong!");
        e.printStackTrace();
    }

我现在的问题是要知道我该怎么称呼从另一个servlet这些方法而不进行新的InitialContext()

My problem now is to know how do I call those methods from another servlet without making a new InitialContext()

编辑:由于新的InitialContext()'不是真的很贵,我这样做的方式,它的工作原理pretty很多很适合我。

Since new InitialContext()'s not really expensive I'm doing that way, It works pretty much well for me

推荐答案

阅读上Tomcat中JNDI配置。该文档是这里。您可以定义要创建一个对象,一次,当抬头。

Read up on JNDI configuration in Tomcat. The documentation is here. You can define an object to be created, once, when looked up.

这篇关于我怎样才能创建的Apache Tomcat启动一个单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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