使用Id自动装配tcp-outbound-channel-adapter不起作用

时间:2016-07-20 12:22:42

标签: java tcp spring-integration

我正在尝试自动装配我的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。如果您需要更多详细信息,请与我们联系。

1 个答案:

答案 0 :(得分:2)

您应该注入类型为SmartLifeCycle而不是TcpSendingMessageHandler类型的bean。接收适配器将以这种方式工作,但由于你想使用生命周期的东西,最好缩小类型。

发送处理程序实际上有一个bean名称outBoundClient.handler,但这不是你想要启动/停止的,它包含在一个消费者中(实现SmartLifecyle,类型取决于通道类型;在这种情况下,EventDrivenConsumer)。