骆驼拆分器执行后是否保留交换体?

时间:2019-03-27 20:36:22

标签: java apache-camel integration-patterns

此处为Java 8和Apache Camel 2.19.5。我有以下bean处理器:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error


#
i = 1 #difficult index

X_train = np.arange(-2,2,0.1/i).reshape(-1,1)
y_train = 1+ np.sin(i*np.pi*X_train/4)

fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0,0,1,1])
ax.plot(X_train,y_train,'b*-')
ax.set_xlabel('X_train')
ax.set_ylabel('y_train')
ax.set_title('Function')
nn = MLPRegressor(
    hidden_layer_sizes=(1,),  activation='tanh', solver='sgd', alpha=0.000, batch_size='auto',
    learning_rate='constant', learning_rate_init=0.01, power_t=0.5, max_iter=1000, shuffle=True,
    random_state=0, tol=0.0001, verbose=True, warm_start=False, momentum=0.0, nesterovs_momentum=False,
    early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08)

nn = nn.fit(X_train, y_train)

predict_train=nn.predict(X_train)



print('MSE training : {:.3f}'.format(mean_squared_error(y_train, predict_train)))

以及以下骆驼路线:

@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
  }
}

如您所见,反序列化的<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 bean处理器,该处理器将该实例转换为FoobarResolver,然后关闭每个List<Foobar>Foobar队列中。无论如何,至少那是我设计的意图!

我很好奇的是: 拆分之后,交换主体变成了什么?它是“还原”到analyze(这就是我想要的),还是现在交换机构是Fizzbuzz产生的List<Foobar>(这不是我想要的)?如果主体现在是FoobarResolver,我该如何重新配置​​内容,以便List<Foobar>改为接收FizzbuzzProcessor

1 个答案:

答案 0 :(得分:1)

出现会还原为拆分前的主体:

@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