打开 EJB 嵌入式容器找不到 JNDI 名称



我试图将OpenEJB用于我的EJB Timer任务Unit test。我收到一个错误,说它找不到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();
  }

下面是注销输出。

Apache OpenEJB 3.1.4    build: 20101112-03:32
http://openejb.apache.org/
INFO - openejb.home = D:workspacetaskMy-App
INFO - openejb.base = D:workspacetaskMy-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.

这是 Maven 依赖项。

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

可能是什么错误?


更新

@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");
}
}

我能够解决这个问题。我不得不将ejb-jar.xml文件添加到resources> META-INF.但是该文件不包含任何内容,除了

<ejb-jar/>

并从此链接获得帮助

特别感谢卡米洛到目前为止对我的帮助。

在我看来

,您正在查找的 JNDI 名称不存在,在 OpenEJB 中,EJB 的默认 JNDI 名称应该是:

{deploymentId}{interfaceType.annotationName}

而且您不需要java:,请查看文档和这篇文章。

更新

尝试

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

最新更新