apache-camel mina2 thread pool shutdown takes 30 seconds

267 views Asked by At

Apache-Camel version: 2.15.2 I have a very simple Spring Boot application that starts, creates a CamelContext and uses a ProducerTemplate to hit a camel-mina2 endpoint. If I don't hit the camel-mina2 endpoint, the application exists immediately. When I hit the camel-mina2 endpoint, the app takes 30 seconds to shutdown waiting on the pool-3-thread-1 thread to exit. How do I get the app to exit immediately? The CamelContext and SpringContext shutdown fine, then the app waits for the pool-3-thread-1 to exit before exiting.

package stuff;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class GetVersion {

    @Autowired
    CamelContext camelContext;

    @Bean
    MyFactory myCodec() {
        return new MyFactory();
    }

    public static void main(String[] args) throws JsonProcessingException, Exception {
        String apiEndpoint = "mina2:tcp://localhost:5003?sync=true&codec=#myCodec&disconnect=true&timeout=3000";
        ConfigurableApplicationContext ctx = null;
        CamelContext camelContext = null;
        try {
            ctx = SpringApplication.run(GetVersion.class, args);
            ctx.registerShutdownHook();
            camelContext = ctx.getBean(CamelContext.class);
            // Try to tell Camel to shutdown within 1 second
            camelContext.getShutdownStrategy().setTimeout(1);

            // Generated POJOs from JSON
            MyIfaceSchema cmd = new MyIfaceSchema();
            cmd.setGetVersion(new GetVersion());
            List<MyIfaceSchema> cmdList = new ArrayList();
            // Create list of commands
            cmdList.add(cmd);

            ObjectMapper mapper = new ObjectMapper();
            // Marshall command array to JSON to send to mina2 endpoint
            String versionRequest = mapper.writeValueAsString(cmdList);

            // Send getVersion command to external TCP endpoint using camel-mina2
            ProducerTemplate producer = ctx.getBean(ProducerTemplate.class);
            String marshalled = producer.requestBody(apiEndpoint, versionRequest, String.class);
            // Print the JSON response
            System.out.println("Version: \n" + marshalled);

        } catch (Exception e) { /* no-op */    
        } finally {
            camelContext.stop();
        }
    }
}

Thread pool that gets instantiated. You can see the app waiting for this thread to exit in the debugger.

[ pool-3-thread-1] Mina2Producer DEBUG Message received: [{"response":{"version":"1.0.0"}}]

Total time: 33.625 secs

0

There are 0 answers