0

我在 Tapestry 应用程序中检索 EJB 时遇到问题。应用程序的另一部分在 OSGi 包中,并且全部部署在 Glassfish 上。

目前,这就是我的东西的样子:

我的界面(打包为jar):

@Remote
public interface MyEJBInterface {
    public static final String JNDI_NAME = "ejb/MyEJBInterface";
    public String sayHello(String name);

}

我的实现(打包为捆绑包):

@Stateless
@EJB(name = MyEJBInterface.JNDI_NAME, beanInterface = MyEJBInterface.class)
public class MyEJBImplementation implements MyEJBInterface {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

我在 AppModule 类中的 Tapestry 构建方法:

public MyEJBService buildMyEJBService() {
    MyEJBInterface myEjb = new JndiLookupHelper<MyEJBInterface>().lookupNoPrefix(MyEJBInterface.JNDI_NAME);

    MyEJBService service = new MyEJBServiceMock(myEjb);

    return service;
}

我是这样查找的:

public class JndiLookupHelper<T> {
    private T remoteObject;

    public T lookupNoEjbPrefix(String name) {
        String jndiPath = "java:comp/env/" + name;
        try {
            InitialContext ic = new InitialContext();
            remoteObject = (T) ic.lookup(jndiPath);
        } catch (NamingException ne) {
            ne.printStackTrace();
        }
        return remoteObject;
    }
}

我的问题是找不到 EJB 并引发异常:

javax.naming.NamingException: Lookup failed for 'java:comp/env/ejb/MyEJBInterface' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: No object bound to name java:comp/env/ejb/MyEJBInterface]
4

3 回答 3

1

你可能会发现这样更好:

public static class EjbServiceBuilder<T> implements ServiceBuilder<T> {
    private Class<T> type;
    private String name;

    public EjbServiceBuilder<T>(Class<T> type, String name) {
        this.type= type;
        this.name = name;
    }

    public T buildService(ServiceResources resources) {
        try {
            return type.cast(InitialContext.doLookup(name));
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }
    }
}

public static void bind(ServiceBinder binder) {
    binder.bind(MyEJBInterface.class, new EjbServiceBuilder<MyEJBInterface>(MyEJBInterface.class, MyEJBInterface.JNDI_NAME));
    binder.bind(MyEJBOtherInterface.class, new EjbServiceBuilder<MyEJBOtherInterface>(...));

    // MyEJBInterface will be injected into MyEJBServiceMock constructor
    binder.bind(MyEJBService.class, MyEJBServiceMock.class);
}
于 2013-09-26T08:17:20.900 回答
0

您需要Export-EJB: ALL在 manifest.mf中声明

我不确定 EJB 是否可以在“java:comp/env/”中使用。您可能需要通过可移植的全局 JNDI 名称进行查找

于 2013-09-23T10:35:21.810 回答
0

我终于找到了解决方案。这里是:

界面:

public interface MyEJBInterface {
    public static final String JNDI_NAME = "java:global/ejb/MyEJBInterface";
    public String sayHello(String name);

}

实施:

@Stateless
@EJB(name = MyEJBInterface.JNDI_NAME, beanInterface = MyEJBInterface.class)
public class MyEJBImplementation implements MyEJBInterface {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

构建方法:

public MyEJBService buildMyEJBService() {
    MyEJBInterface myEjb = new JndiLookupHelper<MyEJBInterface>().lookup(MyEJBInterface.JNDI_NAME);

    MyEJBService service = new MyEJBServiceMock(myEjb);

    return service;
}

查找:

public class JndiLookupHelper<T> {

    private T remoteObject;

    @SuppressWarnings("unchecked")
    public T lookup(String name) {
        try {
            remoteObject = (T) InitialContext.doLookup(name);
        } catch (NamingException ne) {
            ne.printStackTrace();
        }
        return remoteObject;
    }
}
于 2013-09-24T14:15:37.927 回答