【问题标题】:How to implement SmartLifecycleRoleController in Spring Integration application如何在 Spring Integration 应用程序中实现 SmartLifecycleRoleController
【发布时间】:2019-02-05 15:12:29
【问题描述】:

我正在基于 int-ip:tcp 端点编写客户端-服务器应用程序。用 Kotlin 编写的代码。 应用程序包含两个 tcp 客户端,它们应该以一定的顺序连接到服务器, 一个接一个,当第一个客户端已经建立与服务器的连接并进行了一些初始化时。

作为这种同步的解决方案,我想使用SmartLifecycleRoleController 来启动依赖 tcp 客户端的端点组。 为此,在依赖(第二个)客户端中,我将 role="rcCluster"auto-startup="false" 属性添加到 tcp-outbound-channel-adapter 和 tcp-inbound-channel-adapter

<int-ip:tcp-connection-factory id="rcClientConnectionFactory"
                               type="client"
                               host="${tdirelay.host}"
                               port="${tdirelay.rcPort}"
                               single-use="false"
                               so-timeout="10000"
                               so-keep-alive="false"
                               serializer="rawSerializerDeserializer"
                               deserializer="rawSerializerDeserializer"
                               ssl-context-support="sslContext"/>


<int-ip:tcp-outbound-channel-adapter id="rcOutBoundAdapter"
                                     channel="rcPacketQueueChannel"
                                     phase="5000"
                                     connection-factory="rcClientConnectionFactory"
                                     role="rcCluster"
                                     auto-startup="false"
/>


<int-ip:tcp-inbound-channel-adapter id="rcInboundAdapter"
                                    channel="rcFromServer"
                                    client-mode="true"
                                    retry-interval="5000"
                                    connection-factory="rcClientConnectionFactory"
                                    role="rcCluster"
                                    auto-startup="false"


/>  

领先的(第一个)tcp客户端使用拦截器端点按照协议与服务器进行序言交换:

<bean id="dcInterceptorFactoryChain"
      class="org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain">
    <property name="interceptors">
        <array>
            <bean class="com.tcpclient.DirectCannel.DCConnectionInterceptorFactory">
            </bean>
        </array>
    </property>
</bean>


<int-ip:tcp-connection-factory id="dcClientConnectionFactory"
                               type="client"
                               host="${tdirelay.host}"
                               port="${tdirelay.dcPort}"
                               single-use="false"
                               so-timeout="10000"
                               so-keep-alive="false"
                               interceptor-factory-chain="dcInterceptorFactoryChain"
                               serializer="rawSerializerDeserializer"
                               deserializer="rawSerializerDeserializer"
                               ssl-context-support="sslContext"
/>

我打算在我的 Interceptor 类中调用 SmartLifecycleRoleControllerstartLifecyclesInRole(String role)stopLifecyclesInRole(String role) 方法。 因此,我将 @Autowired private val roleController: SmartLifecycleRoleController 添加到 InterceptorFactory,如 spring-integration/docs 中所述

我的 InterceptorFactory 是:

class DCConnectionInterceptorFactory() : TcpConnectionInterceptorFactory, ApplicationEventPublisherAware {

    @Autowired
    private val roleController: SmartLifecycleRoleController? = null

    @Volatile
    private var applicationEventPublisher: ApplicationEventPublisher? = null

    override fun setApplicationEventPublisher(applicationEventPublisher: ApplicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher
    }

    override fun getInterceptor(): TcpConnectionInterceptorSupport {
        return DCConnectionInterceptor(this.applicationEventPublisher!!, roleController!!)
    }
}

IntelliJ IDEA 发出警告: 无法自动接线。未找到“SmartLifecycleRoleController”类型的 bean

并且构建给出错误:

[task-scheduler-2] 错误 org.springframework.integration.ip.tcp.connection.ClientModeConnectionManager - 无法使用 dcClientConnectionFactory,host=localhost,port=9001 建立连接 kotlin.KotlinNullPointerException 在 com.tcpclient.DirectCannel.DCConnectionInterceptorFactory.getInterceptor(DCConnectionInterceptorFactory.kt:25)

我想我需要在xml配置文件中定义SmartLifecycleRoleController类型bean (文档中没有提到:https://docs.spring.io/spring-integration/docs/4.3.4.RELEASE/reference/html/messaging-endpoints-chapter.html#endpoint-roles)。 这个类的构造函数有参数: public SmartLifecycleRoleController(列出角色,列出生命周期) 我不知道如何在 xml 文件中填写我的案例:

如果你知道怎么做,请提供一个在xml配置文件中使用SmartLifecycleRoleController类的bean的实例。

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    首先,最好不要在那里使用@Autowired,因为不清楚是否在您的应用程序上下文中启用了注释配置。仅仅因为您只显示 Spring 的 XML 配置。

    因此,您的 DCConnectionInterceptorFactory 应该有一个用于该 roleController 属性的设置器。或者在 Kotlin 中声明 Java bean 属性的正确方法是什么...

    然后你需要在你的 XML 配置中使用这样的东西:

    <bean class="com.tcpclient.DirectCannel.DCConnectionInterceptorFactory">
        <property name="roleController" ref="integrationLifecycleRoleController"/>
    </bean>
    

    框架会自动为您创建一个SmartLifecycleRoleController 并使用提到的IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER bean 名称注册它。

    请就此事提出 JIRA 以改进文档以使文档更清晰。

    顺便说一句,你的方法是正确的。

    【讨论】:

    • 我刚刚用一个 Spring Boot 应用对其进行了测试,它运行良好(但注释处理在那里自动启用)。
    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 2020-08-12
    • 2018-09-19
    • 1970-01-01
    相关资源
    最近更新 更多