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

控制总线

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

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

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

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

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

控制总线在输入通道上以简单的字符串格式(如beanName.methodName. 目标方法参数的参数必须作为列表提供IntegrationMessageHeaderAccessor.CONTROL_BUS_ARGUMENTS页眉。 要调用的 bean 和方法从ControlBusCommandRegistry基础设施 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。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 上的目标方法。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 与 automatic 一起使用DirectChannel创建时,您可以按如下方式创建控制总线:spring-doc.cadn.net.cn

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

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

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