Dzh*_*zhu 4 jndi java-ee glassfish-embedded java-ee-6 glassfish-3
这个问题是关于Java EE 6,使用glassfish v3 embedded-all.
我有一个单元测试,使用EJBContainer来测试我的无状态EJB.问题是我在使用JNDI查找EJB(远程)时遇到问题:
setup() {
ctx = EJBContainer.createEJBContainer().getContext();
}
...
test() {
BookService bookService = (BookService)ctx.lookup("java:global/BookServiceEJB!com.something.service.BookService");
...
}
@Stateless
public class BookServiceEJB implements BookService {
...
}
@Remote
public interface BookService {
...
}
Run Code Online (Sandbox Code Playgroud)
给出例外:
javax.naming.NamingException: Lookup failed for 'java:global/BookServiceEJB!com.something.service.BookService' in SerialContext [Root exception is javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found]
...
caused by: javax.naming.NameNotFoundException: BookServiceEJB!com.something.service.BookService not found
Run Code Online (Sandbox Code Playgroud)
我尝试了几个JNDI资源路径:
例如
java:global/BookServiceEJB
java:global/BookService
Run Code Online (Sandbox Code Playgroud)
甚至:
java:global/BookShelf-1.0-SNAPSHOT/BookServiceEJB
Run Code Online (Sandbox Code Playgroud)
等等...
没有用
我没有配置任何xml部署文件,只有persistence.xmlMETA-INF.
测试使用maven surefire:
mvn clean test
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!
注意:完全部署到Glassfish服务器(使用appclient和@EJB注入)
经过多次搜索,找到适合我的解决方案......
您必须使用以下属性配置EJBContainer:EJBContainer.MODULES,以及模块类所在的位置(如果使用maven,'target/classes').
例如
...
props = new Properties();
props.put(EJBContainer.MODULES, new File("target/classes"));
ec = EJBContainer.createEJBContainer(props);
...
Run Code Online (Sandbox Code Playgroud)
如果您的EJB使用JPA,则另一个问题是您将无法在嵌入式容器中定义数据源,因此必须使用默认的ds:'jdbc/__ default'.
所以例如我的persistence.xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="bookshelf" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.blah.domain.Book</class>
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
我还没想出如何配置嵌入式容器测试以使用一个DS(jdbc/__默认值),以及我的应用程序使用另一个(例如jdbc/booksDS)
请参阅:http://www.mentby.com/glassfish/embedded-testing-woes.html
请参阅:http://forums.java.net/jive/thread.jspa?messageID = 395759
说实话,我不知道为什么人们在使用Java EE时会遇到麻烦,因为像spring这样的解决方案要简单得多......
这非常令人沮丧,浪费了很多时间......希望这会有所帮助.
| 归档时间: |
|
| 查看次数: |
8151 次 |
| 最近记录: |