rmi.md 4.9 KB
Newer Older
dallascao's avatar
dallascao 已提交
1 2
# RMI 支助

3
## RMI 支持
dallascao's avatar
dallascao 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

|   |这个模块从 5.4 开始就不推荐了,不需要替换,将在 6.0 中删除。|
|---|-----------------------------------------------------------------------------------|

本章将解释如何使用特定于 RMI(远程方法调用)的通道适配器在多个 JVM 上分发系统。第一部分涉及通过 RMI 发送消息。第二部分展示了如何通过 RMI 接收消息。最后一节展示了如何通过使用名称空间支持来定义 RMI 通道适配器。

你需要在项目中包含此依赖项:

Maven

```
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-rmi</artifactId>
    <version>5.5.9</version>
</dependency>
```

Gradle

```
compile "org.springframework.integration:spring-integration-rmi:5.5.9"
```

28
### 出站 RMI
dallascao's avatar
dallascao 已提交
29 30 31 32 33 34 35 36 37 38 39 40

要通过 RMI 从信道发送消息,可以定义`RmiOutboundGateway`。该网关在内部使用 Spring 的`RmiProxyFactoryBean`为远程网关创建代理。请注意,要调用不使用 Spring 集成的远程接口,你应该结合 Spring 的 RmiproxyFactoryBean 使用一个服务激活器。

要配置出站网关,你可以编写一个类似于以下内容的 Bean 定义:

```
<bean id="rmiOutGateway" class=org.spf.integration.rmi.RmiOutboundGateway>
    <constructor-arg value="rmi://host"/>
    <property name="replyChannel" value="replies"/>
</bean>
```

41
### 入站 RMI
dallascao's avatar
dallascao 已提交
42 43 44 45 46 47 48 49 50 51 52 53

要通过 RMI 接收消息,你需要使用`RmiInboundGateway`。你可以按照下面的示例配置网关:

```
<bean id="rmiInGateway" class=org.spf.integration.rmi.RmiInboundGateway>
    <property name="requestChannel" value="requests"/>
</bean>
```

|   |如果在入站网关上使用`errorChannel`,它的错误流通常返回一个结果或抛出一个异常。<br/>这是因为它很可能有一个相应的出站网关在等待某种响应。<br/>在错误流上消耗消息并且不回复将导致入站网关没有回复。<br/>异常(在主流上没有`errorChannel`时或在错误流上)传播到相应的入站网关。|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

54
### RMI 名称空间支持
dallascao's avatar
dallascao 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

要配置入站网关,你可以使用对它的名称空间支持。以下代码片段显示了所支持的不同配置选项:

```
<int-rmi:inbound-gateway id="gatewayWithDefaults" request-channel="testChannel"/>

<int-rmi:inbound-gateway id="gatewayWithCustomProperties" request-channel="testChannel"
    expect-reply="false" request-timeout="123" reply-timeout="456"/>

<int-rmi:inbound-gateway id="gatewayWithHost" request-channel="testChannel"
    registry-host="localhost"/>

<int-rmi:inbound-gateway id="gatewayWithPort" request-channel="testChannel"
    registry-port="1234" error-channel="rmiErrorChannel"/>

<int-rmi:inbound-gateway id="gatewayWithExecutorRef" request-channel="testChannel"
    remote-invocation-executor="invocationExecutor"/>
```

你还可以使用名称空间支持来配置出站网关。以下代码片段显示了出站 RMI 网关的不同配置:

```
<int-rmi:outbound-gateway id="gateway"
    request-channel="localChannel"
    remote-channel="testChannel"
    host="localhost"/>
```

83
### 使用 Java 配置进行配置
dallascao's avatar
dallascao 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

下面的示例展示了如何使用 Java 配置入站网关和出站网关:

```
@Bean
public RmiInboundGateway inbound() {
    RmiInboundGateway gateway = new RmiInboundGateway();
    gateway.setRequestChannel(requestChannel());
    gateway.setRegistryHost("host");
    gateway.setRegistryPort(port);
    return gateway;
}

@Bean
@ServiceActivator(inputChannel="inChannel")
public RmiOutboundGateway outbound() {
    RmiOutboundGateway gateway = new RmiOutboundGateway("rmi://host:port/"
        + RmiInboundGateway.SERVICE_NAME_PREFIX + "remoteChannelName");
    return gateway;
}
```

从版本 4.3 开始,出站网关有第二个构造函数,它接受`RmiProxyFactoryBeanConfigurer`实例以及服务 URL 参数。它允许在创建代理之前进行进一步的配置——例如,注入 Spring 安全性`ContextPropagatingRemoteInvocationFactory`,如下例所示:

```
@Bean
@ServiceActivator(inputChannel="inChannel")
public RmiOutboundGateway outbound() {
    RmiOutboundGateway gateway = new RmiOutboundGateway("rmi://host:port/"
                + RmiInboundGateway.SERVICE_NAME_PREFIX + "remoteChannelName",
        pfb -> {
            pfb.setRemoteInvocationFactory(new ContextPropagatingRemoteInvocationFactory());
        });
    return gateway;
}
```

从版本 5.0 开始,你可以使用`configurer`属性在 XML 命名空间中设置此项。