在Websphere(以及Weblogic/JBoss)中进行远程EJB查找

4

我有一个EJB类用于搜索功能。

@Local(ILuceneEmployeeSearchManagerLocal.class)
@Remote(ILuceneEmployeeSearchManagerRemote.class)
public class LuceneEmployeeSearchManager implements ILuceneEmployeeSearchManagerLocal, ILuceneEmployeeSearchManagerRemote{
....
}

在同一个EAR项目中的WAR项目中,存在另一个类可以访问这个类。

public class EmployeeAccessor {

    private ILuceneEmployeeSearchManagerRemote searcher;

    public EmployeeMstAccessor() {

        Context ic = null;
        try {
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "<WHAT_TO_PUT_HERE>");
            props.put(Context.PROVIDER_URL, "iiop://127.0.0.1:9083");
            ic = new InitialContext(props);
            this.searcher = (ILuceneEmployeeSearchManagerRemote) ic
                .lookup("<WHAT_TO_PUT_HERE?>");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    ....

}

当我使用JNDI执行本地查找时,一切都运作得很完美。由于一些要求,现在需要将EAR部署在多个AP(无论是集群还是非集群环境)。另外,使用的应用服务器将在不同的AP上相同,但可能不一定是Websphere,也就是说所有AP都可以使用JBoss / Websphere / Weblogic。

我的问题是是否存在实现独立的方式来查找和调用远程EJB?这样就可以在Websphere,Weblogic或JBoss环境(同质环境)中使用。

我的第二个问题是假设AP服务器正在运行Websphere(在集群或非集群环境下),如果我希望所有AP服务器上的EmployeeAccessor都使用部署在AP01(ip:x.x.x.x,port:yy)中的LuceneEmployeeSearchManager EJB,该如何配置?是否必须给出完整的JNDI名称(包括Websphere的单元格名称和节点名称)?INITIAL_CONTEXT_FACTORY的正确值是什么?正确的JNDI查询语法是什么?

感谢任何帮助 :)

1个回答

2

您应该使用默认的InitialContext()构造函数——这样可以实现平台无关性,并使用ejb引用——这样就可以实现位置无关性——在安装期间映射您的引用。因此,代码是独立的,但映射是特定于平台的,但是在应用程序外部完成。如果您使用符合Java EE 6标准的服务器,还可以使用注入@EJB。

因此,您的代码可能类似于:

public EmployeeMstAccessor() {

        Context ic = null;
        try {
            ic = new InitialContext();
            this.searcher = (ILuceneEmployeeSearchManagerRemote) ic
                .lookup("java:comp/env/ejb/ILuceneEmployeeSearchManagerRemoteRef");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

那么,您需要在 Web 模块的 web.xml 中创建 ejb/ILuceneEmployeeSearchManagerRemoteRef EJB 引用,并将该引用映射到您的 bean 的 JNDI 名称。这可以在应用程序的安装/配置期间完成,或者在 WebSphere 中通过 ibm-web-bnd.xml 文件完成。
以下是带有 @EJB 的示例(对于 Java EE 5,此必须在 Servlet 或 JSF managedBean 中,对于 Java EE 6,您可以将其放在任何类中,但需要为应用程序启用 CDI):
public class MyServlet exetends HttpServlet {
    @EJB(name="ejb/ILuceneEmployeeSearchManagerRemote")
    private ILuceneEmployeeSearchManagerRemote searcher;  // replaces lookup
...}

再次,您需要将由@EJB注释定义的ejb/ILuceneEmployeeSearchManagerRemote引用映射到实际的JNDI名称。

关于您的第二个问题(针对WebSphere):

  • If the bean is located on the SAME cell, but on other server it will be in the form:

    cell/nodes/<node-name>/servers/<server-name>/<name binding location> 
    

    e.g.:

    cell/nodes/S47NLA1/servers/Server47A1/ejb/Department549/AccountProcessors/CheckingAccountReconciler 
    
  • If the bean is in external cell, then the best way, if you want to avoid providing properties (host, port, factory) in the Initial context, is to use configured bindings. Remember that for cross cell in default configuration you will have to exchange SSL certificates to make it work.

另请参阅:


嗨@Gas,如果要访问的远程EJB位于另一台机器上,您能否详细介绍一下您的答案,或者指导我其他解决此问题的资源?根据我对您回答的理解,我需要编辑我的web.xml和ibm-web-bnd.xml文件以引用EJB。然后还需要使用SSL证书才能使其正常工作。 - Fritz
1
@Fritz,请查看此帖子 - 您可以使用间接绑定来访问外部EJB。关于SSL - 您需要将目标服务器证书导入源服务器信任存储区,并可能共享LTPA令牌密码(如果EJB受到保护)。如果您有更详细的问题,请创建单独的问题。 - Gas
是的,谢谢。我已经让它为我工作了。感谢这些细节,因为它指引我走上了正确的道路。 :D 干杯! - Fritz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接