使用Open EJB配置JNDI名称 [英] Configure JNDI names with Open EJB

查看:115
本文介绍了使用Open EJB配置JNDI名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图(无需单元测试)我的EJB类,而不必启动我的Websphere环境.现在,我正在使用打开EJB ,但是解决该问题有一些问题在我的EJB中使用的其他EJB的JNDI名称...,现在我无法从测试中注入模拟的类.

I'm trying to (unit) test my EJB class without having to startup my websphere environment. Now I'm using Open EJB, but there are some issues with resolving the JNDI Names for other EJBs that are used within my EJB... and there is no way for me to inject mocked classes from my test right now.

获取InitialContext

final Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
properties.setProperty("log4j.category.OpenEJB.options ", "debug");
properties.setProperty("log4j.category.OpenEJB.startup ", "debug");
properties.setProperty("log4j.category.OpenEJB.startup.config ", "debug");
properties.setProperty("MyOwnDatasource.JdbcDriver ", "com.ibm.as400.access.AS400JDBCDriver");
properties.setProperty("MyOwnDataSource.JdbcUrl ", "jdbc:as400:MYHOSTNAME;database name=MYDATABASE;libraries=MYDEFAULTTABLE");
ic = new InitialContext(properties);

在我的受测类中,有一个java:comp/env/ejb/PrefixEjbNameLocalHome的查找,并且我无法设置Open EJB来生成该格式的JNDI名称.

Inside my class under test there is a lookup for java:comp/env/ejb/PrefixEjbNameLocalHome and I can not set Open EJB to generate JNDI names in that format.

JNDI名称格式的附加属性

我尝试设置如下格式规则:

I tried setting the formatting rule like this:

properties.setProperty("openejb.jndiname.format ", "comp/env/ejb/{interfaceClass}");

不使用属性?

也未使用日志记录配置.我只从Open EJB看到 INFO WARN 消息,尽管我将log4j.category.OpenEJB.*等设置为 DEBUG TRACE .

Also the logging configuration isn't used. I'm only seeing INFO and WARN messages from Open EJB, although I set log4j.category.OpenEJB.* and the like to DEBUG or TRACE.

推荐答案

这是"java:"部分,弄乱了您的测试用例.基本上Context.INITIAL_CONTEXT_FACTORY和"java:"是互斥的. InitialContext类对"java:"或任何"foo:"查找有特殊的理解,如果它们位于名称的开头,则将使用您指定的INITIAL_CONTEXT_FACTORY. JNDI的一部分令人沮丧.

It's the "java:" part that is messing up your test case. Basically Context.INITIAL_CONTEXT_FACTORY and "java:" are mutually exclusive. The InitialContext class has a special understanding of "java:" or any "foo:" lookups and if they are at the beginning of the name it will not use INITIAL_CONTEXT_FACTORY you specified. A somewhat frustrating part of JNDI.

如果您查找与日志中打印的名称完全相同的名称,它将起作用.因此,例如,以下日志消息:

If you lookup the name exactly as printed in the log, it will work. So for example this log message:

INFO - Jndi(name=WidgetBeanRemote) --> Ejb(deployment-id=WidgetBean)

然后输入代码:

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
// set any other properties you want
Context context = new InitialContext(p);

Object o = context.lookup("WidgetBeanRemote");

这篇关于使用Open EJB配置JNDI名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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