![](/img/trans.png)
[英]what is equivalent of <tcp-outbound-channel-adapter> in java config?
[英]Autowiring tcp-outbound-channel-adapter using Id does not work
我正在嘗試自動接線定義如下的tcp-inbound-channel-adapter
bean。 稍后,我將在某些條件下開始此操作。
<int-ip:tcp-inbound-channel-adapter
id="inboundClient" channel="replies" connection-factory="client"
client-mode="true" auto-startup="false" retry-interval="10000" />
自動裝配為:
@Autowired
@Qualifier("inboundClient")
TcpReceivingChannelAdapter inboundClient;
我的inboundClient
始終為null
。
但是,當我執行以下操作時,它可以在沒有NPE的情況下工作, tc
具有對象引用並啟動入站適配器並接收數據。
TcpReceivingChannelAdapter tc = (TcpReceivingChannelAdapter)
context.getBean("inboundClient");
Thread.sleep(5000);
tc.start();
如果您遇到或解決了此問題,請告訴我。
編輯:
我的代碼貼在這里:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">
<context:component-scan base-package="com.tcpclient" />
<context:annotation-config />
<!--Deserializer for incoming data on the socket -->
<bean
class="org.springframework.integration.ip.tcp.serializer.ByteArraySingleTerminatorSerializer"
id="serializeAndDeserializer">
<constructor-arg type="byte" value="0" />
</bean>
<!-- TCP Client configuration -->
<!-- Channels for communication -->
<int:channel id="tcp-client-input" />
<int:channel id="message" />
<int:channel id="message-serviceActivator" />
<int:gateway id="gateway" service-interface="com.tcpclient.ClientGateway"
default-request-channel="tcp-client-input" default-reply-channel="message" />
<int-ip:tcp-connection-factory id="clientFactory"
type="client" host="10.255.233.21" port="1234" single-use="false"
so-timeout="10000" deserializer="serializeAndDeserializer" serializer="serializeAndDeserializer" />
<int-ip:tcp-outbound-channel-adapter
id="outBoundClient" channel="tcp-client-input" connection-factory="clientFactory"
retry-interval="60000" auto-startup="false" />
<int-ip:tcp-inbound-channel-adapter
id="inBoundClient" channel="message" connection-factory="clientFactory"
client-mode="true" auto-startup="false" retry-interval="60000" />
<int:object-to-string-transformer
input-channel="message" output-channel="message-serviceActivator" />
<int:service-activator input-channel="message-serviceActivator"
method="onRtlsMessageArrival">
<bean class="com.tcpclient.HandleRtlsMessage" />
</int:service-activator></beans>
@component
public class RtlsConfigManager {
@Autowired
@Qualifier("inBoundClient")
TcpReceivingChannelAdapter inboundClient;
@Autowired
@Qualifier("outBoundClient")
TcpSendingMessageHandler outBoundClient;
public void initialize(Boolean canStart) {
try {
if (canStart) {
inboundClient.start();
outBoundClient.start();
}
} catch (Exception e) {
logger.error("Error occured while fetching data.");
}
}
}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.integration.ip.tcp.TcpSendingMessageHandler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=outBoundClient)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
如果我從現有代碼中摘下
@Autowired
@Qualifier("outBoundClient")
TcpSendingMessageHandler outBoundClient;
和
outBoundClient.start();
它啟動了我的inBoundClient
我是否嘗試以錯誤的方式自動連接outBoundClient。 如果您需要更多詳細信息,請告訴我。
您應該使用SmartLifeCycle
類型SmartLifeCycle
不是TcpSendingMessageHandler
類型注入這些bean。 接收適配器將以這種方式工作,但是,由於您要使用生命周期的東西,最好縮小類型。
發送處理程序實際上具有Bean名稱outBoundClient.handler
但這不是您要啟動/停止的名稱,它包裝在使用者中(該使用者實現SmartLifecyle
且類型取決於通道類型;在這種情況下,是EventDrivenConsumer
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.