繁体   English   中英

使用ID自动装配tcp-outbound-channel-adapter无效

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM