Endpoint Roles
首先, 端点可以分配给角色, 从 4.2 版本开始。角色允许将端点作为一个组来启动和停止。在使用领导选举时, 这特别有用, 其中一组端点可以分别在授予或撤销领导者时启动或停止。为此, 框架使用名称 “IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER” 在应用程序上下文中注册一个 SmartLifecycleRoleController
Bean。每当需要控制生命周期时, 都可以注入或 @Autowired
这个 Bean:
Starting with version 4.2, endpoints can be assigned to roles.
Roles let endpoints be started and stopped as a group.
This is particularly useful when using leadership election, where a set of endpoints can be started or stopped when leadership is granted or revoked, respectively.
For this purpose the framework registers a SmartLifecycleRoleController
bean in the application context with the name IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER
.
Whenever it is necessary to control lifecycles, this bean can be injected or @Autowired
:
<bean class="com.some.project.SomeLifecycleControl">
<property name="roleController" ref="integrationLifecycleRoleController"/>
</bean>
您可以使用 XML、Java 配置或以编程方式将端点分配给角色。以下示例展示了如何使用 XML 配置端点角色:
You can assign endpoints to roles using XML, Java configuration, or programmatically. The following example shows how to configure endpoint roles with XML:
<int:inbound-channel-adapter id="ica" channel="someChannel" expression="'foo'" role="cluster"
auto-startup="false">
<int:poller fixed-rate="60000" />
</int:inbound-channel-adapter>
以下示例展示了如何为在 Java 中创建的 Bean 配置端点角色:
The following example shows how to configure endpoint roles for a bean created in Java:
@Bean
@ServiceActivator(inputChannel = "sendAsyncChannel", autoStartup="false")
@Role("cluster")
public MessageHandler sendAsyncHandler() {
return // some MessageHandler
}
以下示例展示了如何以 Java 为方法配置端点角色:
The following example shows how to configure endpoint roles on a method in Java:
@Payload("#args[0].toLowerCase()")
@Role("cluster")
public String handle(String payload) {
return payload.toUpperCase();
}
以下示例展示了如何使用 Java 中的 SmartLifecycleRoleController
配置端点角色:
The following example shows how to configure endpoint roles by using the SmartLifecycleRoleController
in Java:
@Autowired
private SmartLifecycleRoleController roleController;
...
this.roleController.addSmartLifeCycleToRole("cluster", someEndpoint);
...
以下示例展示了如何使用 Java 中的 IntegrationFlow
配置端点角色:
The following example shows how to configure endpoint roles by using an IntegrationFlow
in Java:
IntegrationFlow flow -> flow
.handle(..., e -> e.role("cluster"));
每个示例都将端点添加到 “cluster” 角色。
Each of these adds the endpoint to the cluster
role.
调用 roleController.startLifecyclesInRole("cluster")
和相应的 stop…
方法来启动和停止端点。
Invoking roleController.startLifecyclesInRole("cluster")
and the corresponding stop…
method starts and stops the endpoints.
可以以编程方式添加实现 |
Any object that implements |
SmartLifecycleRoleController
实现了 ApplicationListener<AbstractLeaderEvent>
, 当授予或撤销领导者时它会自动启动和停止其配置的 SmartLifecycle
对象(当一些 Bean 分别发布 OnGrantedEvent
或 OnRevokedEvent
时)。
The SmartLifecycleRoleController
implements ApplicationListener<AbstractLeaderEvent>
and it automatically starts and stops its configured SmartLifecycle
objects when leadership is granted or revoked (when some bean publishes OnGrantedEvent
or OnRevokedEvent
, respectively).
在使用领导者选举来启动和停止组件时,必须将 auto-startup
XML 属性(autoStartup
bean 属性)设置为 false
,以便应用程序上下文在上下文初始化期间不会启动组件。
When using leadership election to start and stop components, it is important to set the auto-startup
XML attribute (autoStartup
bean property) to false
so that the application context does not start the components during context initialization.
从 4.3.8 版本开始, SmartLifecycleRoleController
提供了几个状态方法:
Starting with version 4.3.8, the SmartLifecycleRoleController
provides several status methods:
public Collection<String> getRoles() 1
public boolean allEndpointsRunning(String role) 2
public boolean noEndpointsRunning(String role) 3
public Map<String, Boolean> getEndpointsRunningStatus(String role) 4
1 | Returns a list of the roles being managed. |
2 | Returns true if all endpoints in the role are running. |
3 | Returns true if none of the endpoints in the role are running. |
4 | Returns a map of component name : running status .
The component name is usually the bean name. |