我编写了一个负载均衡器,每 10 秒生成一个报告,并将其发送到 localhost:9991 上的 MINA 服务器,或者在第一个失败时发送到 localhost:9992 上的 MINA 服务器。 MINA 服务器收到报告后,会对其进行更改并将其发送回负载均衡器,然后负载均衡器打印报告的正文。
public class MyApp_A {
public static void main(String... args) throws Exception {
// create CamelContext
CamelContext context = new DefaultCamelContext();
ConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
context.addComponent("jms", jmsComponentClientAcknowledge(connectionFactory));
context.addRoutes(
new RouteBuilder(){
@Override
public void configure() throws Exception {
from("timer://org.apache.camel.example.loadbalancer?period=10s")
.beanRef("service.Generator", "createReport")
.to("direct:loadbalance");
from("direct:loadbalance")
.loadBalance().failover()
// will send to A first, and if fails then send to B afterwards
.to("mina:tcp://localhost:9991?sync=true")
.to("mina:tcp://localhost:9992?sync=true")
.log("${body}")
.end();
}
}
);
context.start();
}
}
但是,一旦我执行负载均衡器,它就会立即完成。我尝试通过在 context.start()
调用后添加无限循环来解决此问题:
while(true){
/* Dummy for loop, so our server does not die immediately after
* being born and can answer and send requests.
* */
}
然而,这是一个糟糕的解决方案,因为程序只会陷入循环并停止生成报告和发送报告。
我该如何解决这个问题?如何保持负载均衡器运行,同时能够生成请求并打印收到的报告?
最佳答案
camel-core 中提供了一个名为 Main 的类,可以完全满足您的需求。看看这个网站:http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html
关于java - 如何使用JavaDSL保持 Camel 上的路由运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800126/