简体   繁体   English

Open EJB embeded容器找不到JNDI Name

[英]Open EJB embeded container can not find the JNDI Name

I tried to use OpenEJB for my EJB Timer task Unit test . 我尝试使用OpenEJB进行EJB Timer任务Unit test I get an error saying that it can't find the JNDI name. 我收到一个错误,说它无法找到JNDI名称。

  Properties props = new Properties();
  props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");


  IService bean;
  try {
        Context context = new InitialContext(props);
        bean = (IService) context.lookup("java:global/My-App/SingleOccurrenceServiceBean");

       assertNotNull(bean);

  } catch (NamingException e) {
        e.printStackTrace();
  }

And below is the log out put. 以下是注销。

Apache OpenEJB 3.1.4    build: 20101112-03:32
http://openejb.apache.org/
INFO - openejb.home = D:\workspace\task\My-App
INFO - openejb.base = D:\workspace\task\My-App
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Configuring enterprise application: classpath.ear
INFO - Enterprise application "classpath.ear" loaded.
INFO - Assembling app: classpath.ear
INFO - Deployed Application(path=classpath.ear)
 javax.naming.NameNotFoundException: Name "global/My-App/SingleOccurrenceServiceBean" not found.

Here is the Maven dependency. 这是Maven的依赖。

 <groupId>org.apache.openejb</groupId>
 <artifactId>openejb-core</artifactId>
 version>3.1.4</version>

What could be the error? 可能是什么错误?


UPDATE UPDATE

@Remote
public interface IService {

 public void initializeTimer(Date firstDate, long timeout, String info) throws RemoteException;

 void cancelTimer(String timerName) throws RemoteException;

 /**
   * doService call when the EJBTimer timeout. And it start the service intend to do.
   * @param timer
  */
     @Timeout
 public void doService(Timer timer);


}





@Stateless
public class SingleOccurrenceServiceBean implements IService{

    @Override
public void initializeTimer(Date firstDate, long timeout, String info) throws RemoteException {
    try {
        timerService.createTimer(firstDate, info); 

    } catch (Exception e) {
        log.fatal("Exception create timer : "+ e.toString()); 
        e.printStackTrace();
    }

}

@Override
public void cancelTimer(String timerName) throws RemoteException {
    // do nothing as this timer is a single-event-timer
}


@Override
@Timeout
public void doService(Timer timer) {
    log.debug("Read data from DB");
}
}

I was able to fix this. 我能解决这个问题。 I had to add ejb-jar.xml file in to resources > META-INF . 我必须将ejb-jar.xml文件添加到resources > META-INF But the file contains nothing except 但该文件除了包含任何内容

<ejb-jar/>

and got a help from this link 并从这个链接得到了帮助

Special Thank to Camilo for helping me so far. 特别感谢Camilo帮助我到目前为止。

Seems to me that the JNDI name you're looking up doesn't exist, in OpenEJB the default JNDI name for an EJB should be: 在我看来,你正在查找的JNDI名称不存在,在OpenEJB中,EJB的默认JNDI名称应该是:

{deploymentId}{interfaceType.annotationName}

and you don't need the java: , check the docs , and this post . 并且你不需要java: ,检查文档这篇文章

UPDATE UPDATE

Try 尝试

...bean = (IService) context.lookup("SingleOccurrenceServiceBeanRemote");...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM