骆驼豆生命周期策略?

时间:2018-11-12 21:45:39

标签: apache-camel

我有一条骆驼路线,如下所示。每次运行路由时,如何设置骆驼来创建CodeRunner的新实例?

        public void configure() {
            from("activemq:queue:foo?asyncConsumer=true&concurrentConsumers=10")
                    .bean(new codeRunner(), "runCode")
                    .to("stream:out");
        }

1 个答案:

答案 0 :(得分:1)

您可以只在bean上使用scope =“ prototype”。这里有一些例子。路线:

from("timer://foo?period=30s")
            .setBody(simple("bean:test?method=getDate"))
            .log(LoggingLevel.INFO, "Body:${body}");

Bean:

<bean id="test" class="my.test.package.Test" scope="prototype"  />

代码:

public class Test {

final Timestamp date;

public Test() {
    this.date = new Timestamp(System.currentTimeMillis());
}

public Timestamp getDate() {
    return date;
}

}

输出:

2018-11-13 16:45:07,372 | INFO  | #6 - timer://foo | route4                           | 98 - org.apache.camel.camel-core - 2.16.3 | Body:2018-11-13 16:45:07.37
2018-11-13 16:45:37,371 | INFO  | #6 - timer://foo | route4                           | 98 - org.apache.camel.camel-core - 2.16.3 | Body:2018-11-13 16:45:37.37
2018-11-13 16:46:07,371 | INFO  | #6 - timer://foo | route4                           | 98 - org.apache.camel.camel-core - 2.16.3 | Body:2018-11-13 16:46:07.371
2018-11-13 16:46:37,375 | INFO  | #6 - timer://foo | route4                           | 98 - org.apache.camel.camel-core - 2.16.3 | Body:2018-11-13 16:46:37.375