2015-08-17 84 views
3

我想知道spring是否有任何解決方案來支持使用在線服務或遠程服務配置的過程調用。遠程/正在處理服務

更新1

一個例子,可以說,我們有以下幾點:

共同的項目:

public interface ServiceBInterface { 
    boolean doSomething(); 
} 

項目A(取決於通用項目):

@Service 
public class ServiceA { 

    @Autowired 
    private ServiceBInterface serviceB; 

    public void flowA() { 
      // run flow A code 
      boolean result = serviceB.doSomething(); 
      // continue run flow A code with result of service B 
    } 
} 

B項目(依靠普通項目CT):

@Service 
public class ServiceB implements ServiceBInterface { 
    public boolean doSomething() { 
     boolean result = false; 
     // execute some code 
     return result; 
    } 
} 

我想能夠配置ServiceBInterface豆下列選項進行初始化:

  1. ServiceB
  2. 某種對象的實例的實例,它會將一個RPC轉換爲獨立於ServiceA的不同進程上運行的ServiceB。

回答 項目A(取決於通用項目):

@Service 
    public class ServiceA { 

     @Autowired 
     private ServiceBInterface serviceB; 

@PostConstruct 
    public void init() { 
     if (Boolean.getBoolean("remote")) { 
      RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean(); 
      rmiProxyFactoryBean.setServiceUrl("rmi://localhost:1099/ServiceB"); 
      rmiProxyFactoryBean.setServiceInterface(ServiceBInterface.class); 
      rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true); 
      rmiProxyFactoryBean.setLookupStubOnStartup(false); 

      rmiProxyFactoryBean.afterPropertiesSet(); 
      serviceB = (ServiceBInterface) rmiProxyFactoryBean.getObject(); 
     } 
    } 
     public void flowA() { 
       // run flow A code 
       boolean result = serviceB.doSomething(); 
       // continue run flow A code with result of service B 
     } 
    } 

項目B(依賴於通用項目):

@Service 
    public class ServiceB implements ServiceBInterface { 

     RmiServiceExporter rmiServiceExporte; 
@PostConstruct 
public void init() throws RemoteException { 
     if (Boolean.getBoolean("remoteB")) { 
      rmiServiceExporter = new RmiServiceExporter(); 
      rmiServiceExporter.setServiceName("ServiceB"); 
      rmiServiceExporter.setService(serviceB()); 
      rmiServiceExporter.setServiceInterface(ServiceBInterface.class); 
      rmiServiceExporter.setServicePort(9999); 
      rmiServiceExporter.afterPropertiesSet(); 
     } 
    } 
     public boolean doSomething() { 
      boolean result = false; 
      // execute some code 
      return result; 
     } 
    } 
+0

閱讀參考指南,關於遠程處理的部分是你想要的,如果你想更詳細的支持使用Spring Integration。 –

回答

1

對不起,你的問題是不清楚我,但是我想知道你是否能夠通過閱讀Spring Integration Reference Manual找到一些幫助。例如,RMI支持表示爲一對入站/出站網關,以與遠程過程服務進行通信。

否則,請更具體一些,尤其是您希望達到的解決方案代碼示例。

+0

謝謝,請查看原始文章 –

+0

上的更新1,參考指南似乎提供了使用具有某種通道協議(tcp,amqp等)的服務激活器進行遠程調用的解決方案。如果服務是在同一個進程的上下文中,它仍然會使用連接通道,我想避免它,因爲一個簡單的java調用就足夠了,不需要通過一個通道 –