Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop/Remove Route Dynamically/Programmatically Doesn't Remove the Corresponding Thread

Tags:

apache-camel

During the processing of an Exchange received from JMS I'm creating dynamically a route that fetches a file from FTP to the file system and when the batch is done I need to remove that same route. The following code fragment shows how I do this:

public void execute() {
    try {
        context.addRoutes(createFetchIndexRoute(routeId()));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

private RouteBuilder createFetchIndexRoute(final String routeId) {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("ftp://" + getRemoteQuarterDirectory() +
                "?fileName=" + location.getFileName() +
                "&binary=true" +
                "&localWorkDirectory=" + localWorkDirectory)
                .to("file://" + getLocalQuarterDirectory())
                .process(new Processor() {
                    RouteTerminator terminator;

                    @Override
                    public void process(Exchange exchange) throws Exception {
                        if (camelBatchComplete(exchange)) {
                            terminator = new RouteTerminator(routeId, 
                                    exchange.getContext());
                            terminator.start();
                        }
                    }
                })
                .routeId(routeId);
            }
        };
}

I'm Using a thread to stop a route from a route, which is an approach recommended in the Camel Documentation - How can I stop a route from a route

public class RouteTerminator extends Thread {
    private String routeId;
    private CamelContext camelContext;

    public RouteTerminator(String routeId, CamelContext camelContext) {
        this.routeId = routeId;
        this.camelContext = camelContext;
    }

    @Override
    public void run() {
        try {
            camelContext.stopRoute(routeId);
            camelContext.removeRoute(routeId);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }
}

In result the route does stop. But what I see in the jconsole is that the thread that corresponds to the route isn't removed. Thus in time these abandoned threads just keep accumulating.

Is there a way to properly stop/remove a route dynamically/programmatically and also to release the route's thread, so that they don't accumulate through time?

like image 901
Vladimir Tsvetkov Avatar asked Apr 09 '12 10:04

Vladimir Tsvetkov


1 Answers

This is fixed in the next Camel release 2.9.2 and 2.10. Fixed by this ticket: https://issues.apache.org/jira/browse/CAMEL-5072

like image 122
Claus Ibsen Avatar answered Oct 31 '22 18:10

Claus Ibsen