杀死被阻止的Camel路由中的进程

时间:2015-12-15 15:33:41

标签: java routes apache-camel kill shutdown

如何强行杀死路线中的进程?

考虑以下测试类,其中定义了具有延迟的路由。 消息的主体确定延迟的长度,在这种情况下发送3条消息,这导致延迟1,10和1秒。 在运行时,Timer会查看最长的飞行消息,当消息超过2秒时,它应该被杀死。

import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.InflightRepository.InflightExchange;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StopRouteTest extends CamelTestSupport {
    static final Logger LOG = LoggerFactory.getLogger(StopRouteTest.class);

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Override
    public void setUp() throws Exception {
        context = new DefaultCamelContext();

        template = context.createProducerTemplate();

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("direct:start")
                .to("direct:b");

                from("direct:b").routeId("testroute")
                .log("a ${body}")
                .delay(simple("${body}"))
                .log("b ${body}");
            }
        });

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                Collection<InflightExchange> flights = context.getInflightRepository().browse(10, true);
                for (InflightExchange flight : flights) {
                    LOG.error("This flight is " + flight.getElapsed() + "ms stuck (?) in process " + flight.getNodeId() + " in route " + flight.getRouteId() + ". " + flight.getExchange().toString());
                    if (flight.getElapsed() > 2000) {
                        try {
                        context.stopRoute("testroute", 1, TimeUnit.SECONDS, false);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, 500, 500);

        context.start();
    }

    @Test
    public void testRoute() throws InterruptedException {
        template.sendBody("direct:start", 1000);
        template.sendBody("direct:start", 10000);

        Thread.sleep(2000);
        template.sendBody("direct:start", 1000);
    }
}

路由将收到一个停止呼叫,但会一直运行,直到延迟停止:

2015-12-15 16:02:08,472 | INFO  | aultCamelContext | Apache Camel 2.15.1 (CamelContext: camel-1) is starting
2015-12-15 16:02:08,472 | INFO  | nagementStrategy | JMX is enabled
2015-12-15 16:02:09,036 | INFO  | ultTypeConverter | Loaded 195 type converters
2015-12-15 16:02:09,239 | INFO  | aultCamelContext | AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2015-12-15 16:02:09,239 | INFO  | aultCamelContext | StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2015-12-15 16:02:09,317 | INFO  | aultCamelContext | Route: route1 started and consuming from: Endpoint[direct://start]
2015-12-15 16:02:09,317 | INFO  | aultCamelContext | Route: testroute started and consuming from: Endpoint[direct://b]
2015-12-15 16:02:09,317 | INFO  | aultCamelContext | Total 2 routes, of which 2 is started.
2015-12-15 16:02:09,317 | INFO  | aultCamelContext | Apache Camel 2.15.1 (CamelContext: camel-1) started in 0.845 seconds
2015-12-15 16:02:09,348 | INFO  | testroute        | a 1000
2015-12-15 16:02:09,489 | ERROR | StopRouteTest    | This flight is 141ms stuck (?) in process delay1 in route testroute. Exchange[Message: 1000]
2015-12-15 16:02:10,004 | ERROR | StopRouteTest    | This flight is 656ms stuck (?) in process delay1 in route testroute. Exchange[Message: 1000]
2015-12-15 16:02:10,363 | INFO  | testroute        | b 1000
2015-12-15 16:02:10,363 | INFO  | testroute        | a 10000
2015-12-15 16:02:10,504 | ERROR | StopRouteTest    | This flight is 141ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:11,019 | ERROR | StopRouteTest    | This flight is 656ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:11,519 | ERROR | StopRouteTest    | This flight is 1156ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:12,019 | ERROR | StopRouteTest    | This flight is 1656ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:12,534 | ERROR | StopRouteTest    | This flight is 2171ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:12,534 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:12,535 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:13,549 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:13,549 | WARN  | ShutdownStrategy | Interrupted while waiting during graceful shutdown, will force shutdown now.
2015-12-15 16:02:13,549 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:13,549 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:13,549 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:13,549 | ERROR | StopRouteTest    | This flight is 3186ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:13,549 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:13,549 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:14,564 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:14,564 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:14,564 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:14,564 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:14,564 | ERROR | StopRouteTest    | This flight is 4201ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:14,564 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:14,564 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:15,564 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 0 seconds.
2015-12-15 16:02:15,580 | WARN  | ShutdownStrategy | Interrupted while waiting during graceful shutdown, will force shutdown now.
2015-12-15 16:02:15,580 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:15,580 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:15,580 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:15,580 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:15,580 | ERROR | StopRouteTest    | This flight is 5217ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:15,580 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:15,580 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:16,581 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 0 seconds.
2015-12-15 16:02:16,582 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:16,582 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:16,582 | WARN  | ShutdownStrategy | Interrupted while waiting during graceful shutdown, will force shutdown now.
2015-12-15 16:02:16,582 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:16,582 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:16,582 | ERROR | StopRouteTest    | This flight is 6219ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:16,582 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:16,582 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:17,612 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:17,612 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:17,612 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:17,612 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:17,612 | ERROR | StopRouteTest    | This flight is 7249ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:17,612 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:17,612 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:18,627 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:18,627 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 0 seconds.
2015-12-15 16:02:18,627 | WARN  | ShutdownStrategy | Interrupted while waiting during graceful shutdown, will force shutdown now.
2015-12-15 16:02:18,627 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:18,627 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:18,627 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:18,627 | ERROR | StopRouteTest    | This flight is 8264ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:18,627 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:18,627 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:19,627 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 0 seconds.
2015-12-15 16:02:19,643 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:19,643 | WARN  | ShutdownStrategy | Interrupted while waiting during graceful shutdown, will force shutdown now.
2015-12-15 16:02:19,643 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:19,643 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:19,643 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:19,643 | ERROR | StopRouteTest    | This flight is 9280ms stuck (?) in process delay1 in route testroute. Exchange[Message: 10000]
2015-12-15 16:02:19,643 | INFO  | ShutdownStrategy | Starting to graceful shutdown 1 routes (timeout 1 seconds)
2015-12-15 16:02:19,643 | INFO  | ShutdownStrategy | Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 1 seconds.
2015-12-15 16:02:20,363 | INFO  | route1           | stopped a route...
2015-12-15 16:02:20,644 | WARN  | ShutdownStrategy | Timeout occurred during graceful shutdown. Forcing the routes to be shutdown now. Notice: some resources may still be running as graceful shutdown did not complete successfully.
2015-12-15 16:02:20,644 | INFO  | ShutdownStrategy | Graceful shutdown of 1 routes completed in 1 seconds
2015-12-15 16:02:20,644 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:20,644 | INFO  | aultCamelContext | Route: testroute is stopped, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:22,379 | ERROR | aultErrorHandler | Failed delivery for (MessageId: ID-ITRDW7P059-51472-1450191728066-0-5 on ExchangeId: ID-ITRDW7P059-51472-1450191728066-0-6). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://b]. Exchange[Message: 1000]

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [direct://start                                                                ] [         0]
[route1            ] [to1               ] [direct:b                                                                      ] [         0]

Exchange
---------------------------------------------------------------------------------------------------------------------------------------
Exchange[
    Id                  ID-ITRDW7P059-51472-1450191728066-0-6
    ExchangePattern     InOnly
    Headers             {breadcrumbId=ID-ITRDW7P059-51472-1450191728066-0-5, CamelRedelivered=false, CamelRedeliveryCounter=0}
    BodyType            Integer
    Body                1000
]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://b]. Exchange[Message: 1000]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:47)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:129)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:51)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
    at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)
    at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:375)
    at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:343)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:233)
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:343)
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:184)
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:124)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:137)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:144)
    at nl.test.StopRouteTest.testRoute(StopRouteTest.java:95)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2015-12-15 16:02:22,519 | INFO  | StopRouteTest    | ********************************************************************************
2015-12-15 16:02:22,519 | INFO  | StopRouteTest    | Testing done: testRoute(nl.test.StopRouteTest)
2015-12-15 16:02:22,519 | INFO  | StopRouteTest    | Took: 14.546 seconds (14546 millis)
2015-12-15 16:02:22,519 | INFO  | StopRouteTest    | ********************************************************************************
2015-12-15 16:02:22,519 | INFO  | aultCamelContext | Apache Camel 2.15.1 (CamelContext: camel-1) is shutting down
2015-12-15 16:02:22,519 | INFO  | ShutdownStrategy | Starting to graceful shutdown 2 routes (timeout 1 seconds)
2015-12-15 16:02:22,519 | INFO  | ShutdownStrategy | Route: testroute shutdown complete, was consuming from: Endpoint[direct://b]
2015-12-15 16:02:22,519 | INFO  | ShutdownStrategy | Route: route1 shutdown complete, was consuming from: Endpoint[direct://start]
2015-12-15 16:02:22,614 | INFO  | ShutdownStrategy | Graceful shutdown of 2 routes completed in 0 seconds
2015-12-15 16:02:22,629 | INFO  | aultCamelContext | Apache Camel 2.15.1 (CamelContext: camel-1) uptime 14.157 seconds
2015-12-15 16:02:22,629 | INFO  | aultCamelContext | Apache Camel 2.15.1 (CamelContext: camel-1) is shutdown in 0.110 seconds

我怎样才能停止这个过程? 如您所见,在2015-12-15 16:02:10,363,10秒延迟消息开始,大约2秒后路线被“杀死”,恰好10秒后路线实际停止。 所以不是真正的停止,而是等到过程完成。

不过,这当然不是真正的用例,它与数据库有关,但我认为应该以类似的方式解决,因为代码的布局。

1 个答案:

答案 0 :(得分:0)

在Java中杀死线程并不容易,它不像在操作系统级别上杀死进程。

如果你尝试做这样的事情作为正常商业运作的一部分,这是一种难闻的气味。

只有在某种情况下卡住的胭脂线程时,才可以建议尝试杀死/终止它。

您可以做的是将交换标记为停止路由,请参阅StopProcessor的源代码。从机上存储库中,您可以获得该路线的交换,并将其标记为停止。