【问题标题】:An RMIPRoxyFactoryBean factory in Spring?Spring 中的 RMIPRoxyFactoryBean 工厂?
【发布时间】:2009-08-12 09:14:56
【问题描述】:

我目前正在使用 Spring RmiProxyFactoryBean 来访问远程服务。由于需求发生了变化,我需要在运行时指定一个不同的主机 - 可以有很多 - ,但 remoteServiceInterfaceremoteServiceUrl 的非主机组件保持不变。

从概念上讲,我会看到一个类似于以下内容的 bean 定义:

<bean class="org.springframework.remoting.rmi.RmiProxyFactoryBeanFactory">
     <property name="serviceInterface" value="xxx"/>
     <property name="serviceUrl" value="rmi://#{HOST}:1099/ServiceUrl"/>
</bean>

暴露了一个

Object getServiceFor(String hostName);

Spring 是否提供这样的服务?或者,您是否看到了另一种方法?


请注意,主机列表在编译或启动时将知道,因此我无法在 xml 文件中生成它。

【问题讨论】:

    标签: spring rmi


    【解决方案1】:

    如果你查看 RmiProxyFactoryBean 的源代码,你可以看到它是 RmiClientInterceptor 的一个非常瘦的子类,它只是一个标准的 AOP MethodInterceptor。这对我来说建议您可以编写一个自定义类来实现您想要的 getServiceFor(hostname) 方法,并且此方法可以使用 Spring ProxyFactory 以类似于 RmiProxyFactoryBean 的方式为您的特定主机生成运行时代理。

    例如:

    public Object getProxyFor(String hostName) {
        RmiClientInterceptor rmiClientInterceptor = new RmiClientInterceptor();
        rmiClientInterceptor.setServiceUrl(String.format("rmi://%s:1099/ServiceUrl", hostName));
        rmiClientInterceptor.setServiceInterface(rmiServiceInterface);
        rmiClientInterceptor.afterPropertiesSet();
    
        return new ProxyFactory(proxyInterface, rmiClientInterceptor).getProxy();
    }
    

    其中rmiServiceInterfaceproxyInterface 是您定义的类型。

    【讨论】:

      【解决方案2】:

      我最终实现了类似的东西:

      public class RmiServiceFactory implements BeanClassLoaderAware {
        public Service getServiceForHost(String hostName) {
          factory = new RmiProxyFactoryBean();
          factory.setLookupStubOnStartup(false);
          factory.setRefreshStubOnConnectFailure(true);
          factory.setServiceInterface(Service.class);
          factory.setServiceUrl(String.format(_serviceUrlFormat, hostName));
          if (_classLoader != null)
              factory.setBeanClassLoader(_classLoader);
      
          factory.afterPropertiesSet();
        }
      }
      

      当然,其中涉及到一些健全性检查和缓存,但我省略了它们。

      【讨论】:

        猜你喜欢
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多