【问题标题】:How to setup a socket client with spring-integration?如何使用弹簧集成设置套接字客户端?
【发布时间】:2017-10-15 09:19:51
【问题描述】:

我正在使用基于 spring 4 注释的配置,并且想设置一个简单的 telnet/socket 客户端。

这是我目前所拥有的:

@MessageEndpoint
public class MySocket {
    @Bean
    public TcpConnectionFactoryFactoryBean clientFactory() {
        TcpConnectionFactoryFactoryBean fact = new TcpConnectionFactoryFactoryBean();
        fact.setType("client");
        fact.setHost(host);
        fact.setPort(port);
        fact.setUsingNio(true);
        fact.setSingleUse(true);
        fact.setSoTimeout(timeout);
        return fact;
    }

    @Bean
    public MessageChannel clientChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "clientChannel")
    public TcpOutboundGateway outGateway(TcpNioClientConnectionFactory factory, 
            @Qualifier("clientChannel") MessageChannel clientChannel) throws Exception {
        TcpOutboundGateway gate = new TcpOutboundGateway();
        gate.setConnectionFactory(factory);
        gate.setReplyChannel(clientChannel);
        return gate;
    }
}

@Component
public class MyMessageService {
  @Autowired
  @Qualifier("clientChannel")
  private MessageChannel clientChannel;

  public void run() {
    Message<String> msg = MessageBuilder.withPayload("test").build();
    Message<?> rsp = new MessagingTemplate(clientChannel).sendAndReceive(msg);
  }
}

结果:org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers

我在这里缺少什么来通过套接字发送消息并接收回复?

【问题讨论】:

    标签: java spring sockets spring-integration


    【解决方案1】:

    您不需要@MessageEndpoint 注释,但您需要频道上的消费者...

    @ServiceActivator(inputChannel = "clientChannel")
    @Bean
    public TcpOutboundGateway outGateway(AbstractClientConnectionFactory scf) {
        ...
    }
    

    网关需要对连接工厂的引用。由于您使用的是工厂 bean,因此最简单的方法是将其作为参数添加到 bean 的工厂方法中。

    【讨论】:

    • 您可能是指AbstractClientConnectionFactory 作为 scf 参数?在我上面的帖子中添加了TcpOutboundGateway。但现在出现错误:Parameter 0 of method outGateway in MySocket required a bean of type 'org.springframework.integration.ip.tcp.connection. AbstractClientConnectionFactory' that could not be found. 网关上的@ServiceActivator 是否正确?
    • 对不起;是的,我的意思是客户。是的,@ServiceActivator 将网关包装在消费者 bean 中。我建议你将clientFactory bean 更改为直接创建TcpNioClientConnectionFactory 而不是使用FB。
    • 如果这样做,我仍然会收到错误消息。一定还是少了点什么。应用程序启动错误消息(简而言之):The dependencies of some of the beans in the application context form a cycle: MyMessageService &gt; MySocket &gt; outGateway &gt; clientChannel.
    • 我将InputChannel 设置为@ServiceActivatorgate.replyChannel 可能不正确?
    • 你需要按照我的建议去做 - clientChannel 是输入通道;不指定replyChannel,框架会将回复发送到消息传递模板。如果您想先将结果发送到其他地方,您只需要一个回复通道(例如,ObejctToStringTransformerbyte[] 转换为String。在这种情况下,您将省略变压器上的输出通道。跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多