简体   繁体   English

如何使用Spring Cloud Stream应用启动器TCP处理消息

[英]How to handle message using Spring Cloud Stream app starter TCP

I want to use the Spring Cloud Stream App Starter TCP Source project (maven artifact) in order to be able to receive TCP message through socket/port, process them and then push the result to a message broker (eg RabbitMQ). 我想使用Spring Cloud Stream App Starter TCP Source项目 (Maven 项目 ),以便能够通过套接字/端口接收TCP消息,对其进行处理 ,然后将结果推送到消息代理(例如RabbitMQ)。

This TCP source project seems to do exactly what I want, but it automatically sends the received message to the output channel. 这个TCP源项目似乎完全符合我的要求,但是它会自动将接收到的消息发送到输出通道。 So, Is there a clean way to still use the TCP source project but intercept the TCP incoming message to transform them internally before to output them to my message broker? 因此,是否有一种干净的方法仍然可以使用TCP源项目,但是在将它们输出到我的消息代理之前,先截取TCP传入消息以进行内部转换?

See aggregation . 请参阅聚合

You create an aggregated app using the source and processor. 您使用源和处理器创建聚合的应用程序。

Spring Cloud Stream provides support for aggregating multiple applications together, connecting their input and output channels directly and avoiding the additional cost of exchanging messages via a broker. Spring Cloud Stream支持将多个应用程序聚合在一起,直接连接其输入和输出通道,并避免了通过代理交换消息的额外费用。 As of version 1.0 of Spring Cloud Stream, aggregation is supported only for the following types of applications: 从Spring Cloud Stream 1.0版本开始,仅以下类型的应用程序支持聚合:

sources, sinks, processors ... 源,汇,处理器...

They can be aggregated together by creating a sequence of interconnected applications, in which the output channel of an element in the sequence is connected to the input channel of the next element, if it exists. 可以通过创建一系列相互连接的应用程序将它们聚合在一起,在该应用程序中,序列中某个元素的输出通道连接到下一个元素(如果存在)的输入通道。 A sequence can start with either a source or a processor, it can contain an arbitrary number of processors and must end with either a processor or a sink. 序列可以以源或处理器开头,可以包含任意数量的处理器,并且必须以处理器或宿结尾。

EDIT 编辑

As a work around to the Source autowiring problem, you could try something like... 要解决Source自动装配问题,您可以尝试类似...

@EnableBinding(Source.class)
@EnableConfigurationProperties(TcpSourceProperties.class)
public class MyTcpSourceConfiguration {

    @Autowired
    private Source channels;

    @Autowired
    private TcpSourceProperties properties;

    @Bean
    public TcpReceivingChannelAdapter adapter(
            @Qualifier("tcpSourceConnectionFactory") AbstractConnectionFactory connectionFactory) {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(connectionFactory);
        adapter.setOutputChannelName("toMyProcessor");
        return adapter;
    }

    @ServiceActivator(inputChannel = "toMyProcessor", outputChannel = Source.OUTPUT)
    public byte[] myProcessor(byte[] fromTcp) {
        ...
    }

    @Bean
    public TcpConnectionFactoryFactoryBean tcpSourceConnectionFactory(
            @Qualifier("tcpSourceDecoder") AbstractByteArraySerializer decoder) throws Exception {
        TcpConnectionFactoryFactoryBean factoryBean = new TcpConnectionFactoryFactoryBean();
        factoryBean.setType("server");
        factoryBean.setPort(this.properties.getPort());
        factoryBean.setUsingNio(this.properties.isNio());
        factoryBean.setUsingDirectBuffers(this.properties.isUseDirectBuffers());
        factoryBean.setLookupHost(this.properties.isReverseLookup());
        factoryBean.setDeserializer(decoder);
        factoryBean.setSoTimeout(this.properties.getSocketTimeout());
        return factoryBean;
    }

    @Bean
    public EncoderDecoderFactoryBean tcpSourceDecoder() {
        EncoderDecoderFactoryBean factoryBean = new EncoderDecoderFactoryBean(this.properties.getDecoder());
        factoryBean.setMaxMessageSize(this.properties.getBufferSize());
        return factoryBean;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从spring cloud stream app starter源生成的kafka消息中删除内容类型标头 - How to remove the content type header from kafka messages produced by the spring cloud stream app starter sources 如何指定使用 Spring Cloud Stream 向 RabbitMQ 发送消息的超时时间? - How to specify timeout for sending message to RabbitMQ using Spring Cloud Stream? 如何获取Spring Cloud Stream App Starter httpclient处理器以将错误消息从api响应发送到日志接收器? - How do I get the Spring Cloud Stream App Starter httpclient processor to send error messages from an api response to a log sink? Spring 云 Stream - 如何处理下游块? - Spring Cloud Stream - how to handle downstream blocks? 如何在本地使用 spring-cloud-starter-aws 运行应用程序? - How to run the app with spring-cloud-starter-aws locally? Spring Cloud Stream应用程序启动器在10秒后失败,并说BinderException:无法初始化绑定程序 - Spring cloud stream app starter fails after 10 secs, saying BinderException: Cannot initialize binder 如何在 Spring Cloud Stream App 中反序列化 MessagePack - How to deserialize MessagePack in Spring Cloud Stream App 是否可以在Spring Cloud Stream Starter应用程序上禁用安全性? - Is it possible to disable security on spring cloud stream starter apps? 如何处理 Spring 云 stream kafka 流活页夹中的序列化错误? - How to handle Serialization error in Spring cloud stream kafka streams binder? 使用spring cloud stream kafka读取消息的编程方式 - Programmatic way to read message using spring cloud stream kafka
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM