java - Camel Splitters 执行后会保留交换主体吗?

标签 java apache-camel integration-patterns

这里是 Java 8 和 Apache Camel 2.19.5。我有以下 Bean 处理器:

@Component("foobarResolver")
public class FoobarResolver {
  public List<Foobar> resolve(Fizzbuzz fizzbuzz) {
    List<Foobar> foobars = new ArrayList<Foobar>();

    // Use some logic in here to make the fizzbuzz populate the foobars list.

    return foobars;
  }
}

@Component("fizzbuzzProcessor")
public class FizzbuzzProcessor {
  public FizzbuzzOutput process(Fizzbuzz fizzbuzz) {
    // Blah whatever
  }
}

以及以下 Camel 路线:

<route id="fizzbuzzHandler">
  <!-- XML '<fizzbuzz>' messages get sent here by an upstream process -->
  <from uri="activemq:fizzbuzzes"/>

  <!-- Use XStream to deserialize the XML into a 'Fizzbuzz' POJO instance -->
  <unmarshal ref="xs"/>

  <split>
    <method ref="bean:foobarResolver"/>
    <to uri="activemq:analyze"/>
  </split>

  <!-- Now assuming our body is once again the Fizzbuzz we can just continue as normal... -->

  <!-- Process the fizzbuzz -->
  <to uri="bean:fizzbuzzProcessor"/>

  <!-- Send fizzbuzzProcessor's output to 'output' queue -->
  <to uri="activemq:output"/>
</route>

如您所见,反序列化后的Fizzbuzz实例被发送到FoobarResolver bean 处理器,它将该实例变成 List<Foobar>然后发送每个 Foobar前往analyze排队,一一排队。至少这就是我设计的目的!

我好奇的是: split 之后,交换体变成了什么?它是否“恢复”回 Fizzbuzz (这就是我想要的),或者现在的交换机构是 List<Foobar>FoobarResolver 制作(这不是我想要的)?如果现在的 body 是List<Foobar> ,我怎样才能重新配置,以便 FizzbuzzProcessor收到Fizzbuzz相反?

最佳答案

似乎恢复到分割前的主体:

@SpringBootApplication
public class SocamelApplication extends RouteBuilder implements ApplicationRunner {
    @Autowired
    private FooProcessor fooProcessor;

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

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Thread.sleep(5000);
    }

    @Override
    public void configure() throws Exception {
        from("timer://foo?period=100&repeatCount=1").setBody()
                                                    .constant(Arrays.asList("Hello", "World"))
                                                    .log("1 >>> ${body} ")
                                                    .split(body())
                                                    .log("2 >>> ${body}")
                                                    .bean(fooProcessor)
                                                    .log("3 >>> ${body}")
                                                    .end()
                                                    .log("4 >>> ${body}");

    }

    @Bean
    public FooProcessor fooProcessor() {
        return new FooProcessor();
    }

}

class FooProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        String reverseMe = exchange.getIn()
                                   .getBody(String.class);

        String reversed = new StringBuilder(reverseMe).reverse()
                                                      .toString();

        exchange.getOut()
                .setBody(reversed);
    }

}

产量:

1 >>> Hello,World 
2 >>> Hello
3 >>> olleH
2 >>> World
3 >>> dlroW
4 >>> Hello,World

关于java - Camel Splitters 执行后会保留交换主体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386025/

相关文章:

java - 使用java将图像存储在sql server数据库中

java - setTextColor 的参数是一个 int 但可以接受 8 位十六进制值是怎么回事?

java - 如何处理 Jackson 与 Camel REST DSL 绑定(bind)的异常?

java - "seda + concurrentConsumers"和 "direct + threads"有什么区别

Akka 的 Java 聚合器

java - 在 Cygwin 上从自解压 bin 安装 JDK

java - 为什么在将持久 activemq 消息更改为非持久消息时无法获得性能提升?

java - 如何在 Java 中以同步方式处理异步回调?

java - 使用 java 通过 Twilio API 进行电话验证