java - 使用 Id Autowiring tcp-outbound-channel-adapter 不起作用

标签 java tcp spring-integration

我正在尝试 Autowiring 我的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" />

Autowiring 为:

@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 类型而不是 TcpSendingMessageHandler 类型注入(inject)这些 Bean。不过,接收适配器将以这种方式工作,但由于您想使用生命周期内容,所以最好缩小类型。

发送处理程序实际上有一个 bean 名称 outBoundClient.handler 但这不是您想要启动/停止的,它被包装在消费者中(它实现了 SmartLifecyle 和类型取决于 channel 类型;在本例中为 EventDrivenConsumer)。

关于java - 使用 Id Autowiring tcp-outbound-channel-adapter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481192/

相关文章:

java - Spring集成UDP服务器不监听

java - Spring 集成错误 - 调度程序没有 channel 订阅者

java - 需要 isFlush 和 isThreeKind 方法、扑克类型程序的一些帮助

java - 定期更新静态变量的正确方法

java - 如何分配对象而不将它们保存在变量中?

java - 使用 java 客户端/服务器重现 tcp CLOSE_WAIT 状态

c++ - 尝试通过 tcp 发送 vector

java - 如何等待QueueChannel处理所有消息?

Java同步块(synchronized block)不工作

networking - 建立tcp连接时,如果TCP使用初始序列号作为固定数字怎么办?