在处理器和接收器之间转换消息

时间:2017-09-18 19:27:43

标签: spring-cloud-stream

我正在尝试创建一个示例应用,它会发送一个人来处理,然后收集所有错误,我正在使用Jackson进行序列化。

但我的Sink尝试转换从处理器收到的消息时失败了。看起来它正在接收具有不同contentType而不是application / json的消息。我该怎么办才能让它发挥作用?

我正在使用Spring Cloud Dalston.SR3

主:

@SpringBootApplication
public class PersonStreamApplication {

    @Bean
    public MessageConverter customMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setObjectMapper(new ObjectMapper().registerModule(new VavrModule()));
        return converter;
    }

    public static void main(String[] args) {
        SpringApplication.run(PersonStreamApplication.class, args);
    }
}

来源:

@EnableBinding(PersonSource.Source.class)
public class PersonSource {

    @Bean
    @InboundChannelAdapter(value = Source.SAMPLE, poller = @Poller(fixedDelay = "10000", maxMessagesPerPoll = "1"))
    public MessageSource<Person> getDate() {
        Map<String, Object> headers = new HashMap<>();
        headers.put("directory", "/dir");
        return () -> createMessage(new Person("joe@joe.com", 28), new MessageHeaders(headers));
    }

    public interface Source {

        String SAMPLE = "sample-source";

        @Output(SAMPLE)
        MessageChannel sampleSource();
    }
}

处理器:

@RequiredArgsConstructor
@EnableBinding(Processor.class)
public class PersonProcessor {

    private final PersonValidator validator;

    @Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
    public Either<String, Person> process(Person person) {
        return this.validator.validate(person).toEither();
    }
}

水槽:

@EnableBinding(PersonSink.Sink.class)
public class PersonSink {

    @Bean
    public IntegrationFlow log() {
        return IntegrationFlows.from(Sink.SAMPLE).log().get();
    }

    public interface Sink {

        String SAMPLE = "sample-sink";

        @Input(SAMPLE)
        SubscribableChannel sampleSink();
    }
}

application.yml

spring:
  cloud:
    stream:
      bindings:
        sample-source:
          destination: testing.stream.input
        input:
          destination: testing.stream.input
        output:
          content-type: application/json
          destination: testing.stream.output
        sample-sink:
          destination: testing.stream.output

1 个答案:

答案 0 :(得分:1)

你的问题在于:

@Bean
public IntegrationFlow log() {
    return IntegrationFlows.from(Sink.SAMPLE).log().get();
}

您必须使用@StreamListener获得Content-Type转换获得的收益:https://docs.spring.io/spring-cloud-stream/docs/Chelsea.SR2/reference/htmlsingle/index.html#_using_streamlistener_for_automatic_content_type_handling

  

在考虑具有String有效负载和@StreamListener @ServiceActivator标头contentType的入站消息时,可以看到application/json和Spring Integration @StreamListener之间的区别。对于MessageConvertercontentType机制将使用String标头将Vote有效内容解析为# CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-5.9 - Base #mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=$basearch&repo=os baseurl=http://vault.centos.org/5.9/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 #released updates [updates] name=CentOS-5.9 - Updates #mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=$basearch&repo=updates baseurl=http://vault.centos.org/5.9/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 #additional packages that may be useful [extras] name=CentOS-5.9 - Extras #mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=$basearch&repo=extras baseurl=http://vault.centos.org/5.9/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-5.9 - Plus #mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=$basearch&repo=centosplus baseurl=http://vault.centos.org/5.9/os/$basearch/centosplus/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 #contrib - packages by Centos Users [contrib] name=CentOS-5.9 - Contrib #mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=$basearch&repo=contrib baseurl=http://vault.centos.org/5.9/os/$basearch/contrib/ gpgcheck=1 enabled=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 对象。