此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.3spring-doc.cadn.net.cn

控制总线

正如 Enterprise Integration Patterns (EIP) 一书中描述的,控制总线背后的思想是,可以使用与用于 “应用程序级” 消息传递相同的消息传递系统来监控和管理框架内的组件。 在 Spring 集成中,我们构建在上述适配器的基础上,以便您可以发送消息作为调用公开作的一种方式。spring-doc.cadn.net.cn

由于 Control Bus 足够强大,可以更改系统状态,因此建议保护其消息接收(参见SecurityContextChannelInterceptor),并将控制总线管理(消息源)仅公开到 DMZ 中。

以下示例说明如何使用 XML 配置控制总线:spring-doc.cadn.net.cn

<int:control-bus input-channel="operationChannel"/>

控制总线有一个 Importing 通道,可以访问该通道以调用应用程序上下文中的 bean 上的作。 它还具有服务激活终端节点的所有通用属性。 例如,如果作结果具有要发送到下游通道的返回值,则可以指定输出通道。spring-doc.cadn.net.cn

控制总线将输入通道上的消息作为托管作以简单的字符串格式运行,例如beanName.methodName. 目标方法参数的参数必须在IntegrationMessageHeaderAccessor.CONTROL_BUS_ARGUMENTS页眉。 Bean 和要调用的方法从ControlBusCommandRegistryinfrastructure bean 的 bean 中。 默认情况下,ControlBusCommandRegistry按需注册命令:其eagerInitialization标志可以通过以下方式打开@EnableIntegrationManagement(loadControlBusCommands = "true").spring-doc.cadn.net.cn

Control Bus 的功能类似于 JMX,因此命令的方法资格必须满足以下要求:spring-doc.cadn.net.cn

确保您自己的方法可用于控制总线的最简单方法是使用@ManagedAttribute@ManagedOperation附注。 由于这些注释也用于向 JMX MBean 注册表公开方法,因此它们提供了一个方便的副产品:通常,要向控制总线公开的相同类型的作对于通过 JMX 公开是合理的。 有关更多信息,请参阅ControlBusCommandRegistryControlBusMethodFilterJavadocs 的 Java 文档。spring-doc.cadn.net.cn

要在 Spring Bean 上执行方法,客户端可以向作通道发送消息,如下所示:spring-doc.cadn.net.cn

Message<?> operation = MessageBuilder.withPayload("myServiceBean.shutdown").build();
operationChannel.send(operation);

如果要调用的目标方法有参数(例如ThreadPoolTaskExecutor.setMaxPoolSize(int maxPoolSize)),这些值必须以IntegrationMessageHeaderAccessor.CONTROL_BUS_ARGUMENTS页眉:spring-doc.cadn.net.cn

Message<?> operation =
        MessageBuilder.withPayload("myTaskExecutor.setMaxPoolSize")
        .setHeader(IntegrationMessageHeaderAccessor.CONTROL_BUS_ARGUMENTS, List.of(10))
        .build();
operationChannel.send(operation);

您可以将这些命令视为PreparedStatementJDBC 中具有参数绑定的实例。 参数的类型必须与方法参数的类型匹配。 它们用作附加标准,以根据 Java 方法重载功能选择要调用的方法。 例如,组件:spring-doc.cadn.net.cn

@ManagedResource
class TestManagementComponent {

    @ManagedOperation
    public void operation() {

    }

    @ManagedOperation(description = "The overloaded operation with int argument")
    public void operation(int input) {

    }

    @ManagedOperation(description = "The overloaded operation with two arguments")
    public void operation(int input1, String input2) {

    }

    @ManagedOperation
    public int operation2() {
    	return 123;
    }

}

将公开 3 个命令operation名字。 当我们调用testManagementComponent.operation命令中,我们应该为IntegrationMessageHeaderAccessor.CONTROL_BUS_ARGUMENTS标头,让ControlBusCommandRegistry以过滤掉 Bean 上的 Target 方法。spring-doc.cadn.net.cn

使用 Java 注释,您可以按如下方式配置控制总线:spring-doc.cadn.net.cn

@Bean
@ServiceActivator(inputChannel = "operationChannel")
public ControlBusFactoryBean controlBus() {
    return new ControlBusFactoryBean();
}

同样,您可以按如下方式配置 Java DSL 流定义:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow controlBusFlow() {
    return IntegrationFlow.from("controlBus")
              .controlBus()
              .get();
}

如果您更喜欢将 lambda 表达式与自动DirectChannel创建,您可以按如下方式创建 Control Bus:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow controlBus() {
    return IntegrationFlowDefinition::controlBus;
}

在这种情况下,通道被命名为controlBus.input.spring-doc.cadn.net.cn

另请参阅 Control Bus REST 控制器,了解如何通过 HTTP 公开 Control Bus 管理。spring-doc.cadn.net.cn