将嵌入式Tomcat从v6更改为v7会导致InitialContext查找失败 [英] Changing embedded Tomcat from v6 to v7 causes InitialContext lookup to fail

查看:75
本文介绍了将嵌入式Tomcat从v6更改为v7会导致InitialContext查找失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JUnit测试用例来使用嵌入式Tomcat来运行我的Web服务.在Tomcat 6下,一切都工作正常,但是当我将项目切换到Tomcat 7时,我将无法使用.

I'm using JUnit test cases to exercise my web service using embedded Tomcat. Under Tomcat 6 everything was working fine, but when I switched my project to Tomcat 7 I'm coming unstuck.

设置嵌入式Tomcat服务器的测试代码如下:

The test code to setup the embedded Tomcat server is as follows:

Embedded container = new Embedded();
container.setCatalinaHome("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11");
container.setRealm(new MemoryRealm());
container.setName("Catalina");
Engine engine = container.createEngine();
container.addEngine(engine);
Host host = container.createHost("localhost", "/DecoderServiceTest");
Context rootContext = container.createContext("/DecoderServiceTest", System.getProperty("user.dir") + "/build/web");
host.addChild(rootContext);
engine.setName("Catalina");
engine.addChild(host);
engine.setDefaultHost("localhost");
container.addEngine(engine);
Connector connector = container.createConnector(InetAddress.getLocalHost(), 4321, false);
container.addConnector(connector);
container.start();

由于嵌入式API在版本6和版本7之间进行了更改,因此我将自己的代码更改为以下内容:

As the embedded API has changed between versions 6 and 7, I've changed my own code to the following:

Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11");
tomcat.setPort(1234);
tomcat.addWebApp("/DecoderServiceTest", System.getProperty("user.dir")+"/build/web");
tomcat.setHostname("localhost");
tomcat.start();

当我执行JUnit测试时,实际的Web服务可以正常启动(我可以使用我的Web浏览器并看到正在提供WSDL).

The actual web service starts up fine when I execute the JUnit test (I can use my web browser and see the WSDL being served up).

但是,在我的Web服务的构造函数中,我根据web.xml文件(位于System.getProperty("user.dir")+"/build/web/WEB-INF/web.xml"中)的值初始化一些变量,如下所示:

However, in the constructor of my web service I intialise some variables based on the values in the web.xml file (which is located in System.getProperty("user.dir")+"/build/web/WEB-INF/web.xml"), as follows:

  Context initCtx = new InitialContext();
  Context envCtx = (Context) initCtx.lookup("java:comp/env");
  int thumbnailSize = (Integer) envCtx.lookup("thumbnail-pixel-size");

我的web.xml文件包含以下条目:

Where my web.xml file contains the following entry:

<env-entry>
  <env-entry-name>thumbnail-pixel-size</env-entry-name>
  <env-entry-type>java.lang.Integer</env-entry-type>
  <env-entry-value>64</env-entry-value>
</env-entry>

当我尝试创建envCtx对象时,我得到了 NamingException 和消息Name java:comp is not bound in this Context.我很困惑,因为它可以在Tomcat 6上正常工作.我错过了以前在Tomcat 6设置中定义的Tomcat 7设置吗?

When I try and create the envCtx object I get a NamingException with the message that Name java:comp is not bound in this Context. I'm confused because it worked fine with Tomcat 6. Have I missed something in the setup of Tomcat 7 that I had previously defined in the setup of Tomcat 6?

推荐答案

通过tomcat-users邮件列表标记Thomas,

Mark Thomas via the tomcat-users mailing list suggested

tomcat.enableNaming();

在启动服务器之前.这对我有用(我想他们将默认行为更改为6到7).

before the server is started. This worked for me (I guess they changed the default behaviour between 6 and 7).

这篇关于将嵌入式Tomcat从v6更改为v7会导致InitialContext查找失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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