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