我在int-ip:tcp端点的基础上编写客户端 - 服务器应用程序。代码写在Kotlin。应用程序包含两个tcp clienst,它们应该以一个接一个的方式连接到服务器,当第一个cleint已经建立了与服务器的连接并进行了一些初始化时。
作为此同步的解决方案,我想使用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"
/>
我打算在我的拦截器类中调用startLifecyclesInRole(String role)
的stopLifecyclesInRole(String role)
和SmartLifecycleRoleController
方法。所以,我在Spring-integration / docs中解释了@Autowired private val roleController:SmartLifecycleRoleController到InterceptorFactory
我的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 - 无法在com.tcpclient.DirectCannel.DCConnectionInterceptorFactory.getInterceptor中使用dcClientConnectionFactory,host = localhost,port = 9001 kotlin.KotlinNullPointerException建立连接( 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(List roles,List lifecycles),我不知道如何在xml文件中填写我的情况:
如果您知道如何执行此操作,请提供在xml配置文件中使用SmartLifecycleRoleController
类的bean的实例。
首先,最好不要在那里使用@Autowired
,因为不清楚是否在应用程序上下文中启用了注释配置。仅仅因为你只展示了Spring的XML配置。
因此,你的DCConnectionInterceptorFactory
应该有一个roleController
属性的setter。或者在Kotlin中声明Java bean属性的正确方法是什么?
然后你需要在XML配置中使用这样的东西:
<bean class="com.tcpclient.DirectCannel.DCConnectionInterceptorFactory">
<property name="roleController" ref="integrationLifecycleRoleController"/>
</bean>
框架自动为您的SmartLifecycleRoleController
创建并使用提到的IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER
bean名称进行注册。
请提出一个关于此问题的JIRA,以改进文档,使其更清洁。
顺便说一句,你的方法是正确的。