【问题标题】:Using Spring to Access an EJB Across Clusters in WebSphere using Grails使用 Spring 在 WebSphere 中使用 Grails 跨集群访问 EJB
【发布时间】:2010-07-08 20:27:36
【问题描述】:

过去几天我一直在尝试将 Grails(版本 1.3.2)应用程序与部署在 WebSphere 6.1 上的 EJB 2.1 应用程序集成。一旦我们的 Grails 应用程序投入生产,它们也将部署到 WebSphere。 EJB 2.1 应用程序在我们公司广泛使用,并且在本地开发环境之外的任何地方都部署到它自己的集群中。我们在现有的 Java EE 应用程序(所有这些应用程序都是非 Spring、非 Grails)中处理这个问题的方式是在我们的每个其他集群中绑定一个 CORBA CosNaming 命名上下文,然后可以使用它来获取对我们共享 EJB 的引用2.1 应用。因此,到目前为止,如果我们的某个应用程序需要与该应用程序交互,他们会使用如下方法:

String cosNameBinding = "ejbApp.HighAvail.cluster";
InitialContext initial = new InitialContext();
Context fedContext = (javax.naming.Context) initialCtx.lookup(cosNameBinding);

然后使用 federated/CosNaming 上下文执行正常的 EJB 样式查找/缩小/调用:

Object ejbHomeAsObject = fedContext.lookup(jndiNameOfService);        
EJBHome home = (EJBHome) PortableRemoteObject.narrow(ejbHomeAsObject, homeClass);
Object service = invokeMethod(homeClass, home, "create");

如您所见,这里发生了一个间接级别,以便从 InitialContext 转到可用于与共享 EJB 应用程序交互的联合命名上下文。

在本地运行,我将 Grails 应用程序和 EJB 应用程序部署到同一台服务器(非网络部署 WAS,相同的配置文件和节点)。我的 Spring 配置如下:

beans = {
        ejbJndi(org.springframework.jndi.JndiTemplate) {
            environment = ["java.naming.factory.initial" : 
                           "com.ibm.websphere.naming.WsnInitialContextFactory"]
        }
        crewMemberService(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean) {
            jndiName="hotelService/ejb/HotelService"
            businessInterface="com.company.appName.hotel.HotelService"
            lookupHomeOnStartup="false"
            cacheHome="false"
            refreshHomeOnConnectFailure="true"
            jndiTemplate = ref("ejbJndi")
        }
}

我可以成功地将 ejb 引用注入我的 Grails 控制器并调用它们。但是,WebSphere 只能解析 JNDI 查找,因为它们都部署在同一台服务器上。当我们将它移动到我们的开发环境之一时,我们需要这些服务的 jndi 查找来对抗联合命名上下文。

所以我的问题是:

  1. 有没有办法使用 Spring 中提供的类来做到这一点?如果有,您能否告诉我我需要如何修改 Spring 配置才能做到这一点?
  2. 鉴于我们在如何部署其他应用或获取对其服务的引用方面没有灵活性(我们必须使用联合上下文),我是否应该考虑扩展 JndiTemplate 并自己进行必要的接线?

如果有人遇到过这种情况,我将非常感谢您提供的任何见解。

【问题讨论】:

    标签: spring grails websphere ejb


    【解决方案1】:

    如果有人在路上遇到同样的问题,我最终实现了对 Spring 的 JndiTemplate 的扩展并使用它。代码如下:

    public class FederatedJndiTemplate extends org.springframework.jndi.JndiTemplate
    {
        protected static final String JNDI_CONTEXT_BINDING_NAME = "fed.context.jndiName";
    
        /**
         * Obtain a JNDI naming context for the specified federated naming context.
         * 
         * @throws NamingException if no "fed.context.jndiName" has been specified in
         * the environment properties for the jndiTemplate or the container throws a naming
         * exception.
         */
        @Override
        protected Context createInitialContext() throws NamingException {
            Properties props = super.getEnvironment();
    
            if(!props.containsKey(JNDI_CONTEXT_BINDING_NAME)) {
                throw new NamingException("You must specify the federated naming context JNDI binding name");
            }
    
            String jndiBinding = props.getProperty(JNDI_CONTEXT_BINDING_NAME);
            InitialContext initCtx = new InitialContext();
            Context fedCtx = (Context) initCtx.lookup(jndiBinding);
            return fedCtx;
        }
    }
    

    然后在我的 resources.groovy 中,我只是使用了这个 JndiTemplate:

                ejbJndi(com.myCompany.spring.jndi.FederatedJndiTemplate) {
                        environment = [
                            "fed.context.jndiName":"myServices.HighAvail.Cluster"]
                }
                hotelService(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean) {
                    jndiName="hotelService/ejb/HotelService"
                    businessInterface="com.mycompany.appName.hotel.HotelService"
                    homeInterface="com.mycompany.appName.hotel.HotelServiceHome"
                    lookupHomeOnStartup="false"
                    jndiTemplate = ref("ejbJndi")
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-10
      • 1970-01-01
      • 2022-08-13
      相关资源
      最近更新 更多