如何在骆驼中快速停止seda

时间:2013-05-27 15:01:33

标签: asynchronous apache-camel shutdown

我有一个带有分离器(使用流)的camel路由,它将消息发送到要处理的seda队列。当我试图轻轻地停止应用程序时,seda队列不会立即停止,它会在最终关闭之前处理所有消息。 我该怎么做才能马上阻止它?

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.ExpressionBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class MySedaShutdownTest extends RouteBuilder  {

@Override
public void configure() throws Exception {
    onException(Exception.class)
        .process(new Processor() {

            @Override
            public void process(Exchange exchange) throws Exception {
                System.out.println("exception");
            }

        });

    from("timer:myTimer?repeatCount=1")
        .split(ExpressionBuilder.beanExpression(new MySplitter(), "myIterator"))
                .streaming()
                .to("seda:mySeda");

    from("seda:mySeda")
        .throttle(1)
        .process(new Processor() {

            @Override
            public void process(Exchange exchange) throws Exception {
                System.out.println("processing: " + exchange.getIn().getBody()
                        + "; app status: " + exchange.getContext().getStatus());

            }

        });
}

public static class MySplitter {

    public Iterator<String> myIterator() {
        List<String> values = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            values.add("string nr : " + i);
        }
        System.out.println("in myIterator");
        return values.iterator();
    }
}

public static void main(String[] a) throws Exception {
    final Main main = new Main();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                System.out.println("invoking shutdown");
                main.shutdown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }).start();
    System.out.println("starting app");
    main.enableHangupSupport();
    main.addRouteBuilder(new MySedaShutdownTest());
    main.run();
}

}

2 个答案:

答案 0 :(得分:1)

SedaEndpoint上有一个purgeQueue方法。因此,您可以获取端点并调用此方法。您也可以从JMX访问它。

有点相关我们有这张改进票

我为此记录了一张票

答案 1 :(得分:0)

只需在下面添加字符串作为 configure()方法的第一行:

getContext().getShutdownStrategy().setTimeout(1);

它会将关机超时从300秒(默认)减少到1

查看有关controlling start-up and shutdown of routes的更多信息。