Apache-Camel版本:2.15.2 我有一个非常简单的Spring Boot应用程序启动,创建一个CamelContext并使用ProducerTemplate来命中camel-mina2端点。如果我没有点击camel-mina2端点,则应用程序立即存在。当我点击camel-mina2端点时,应用程序需要30秒才能关闭等待pool-3-thread-1线程退出。如何立即退出应用程序? CamelContext和SpringContext关闭正常,然后应用程序在退出之前等待pool-3-thread-1退出。
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();
}
}
}
获取实例化的线程池。您可以看到等待此线程的应用程序在调试器中退出。
[pool-3-thread-1] Mina2Producer DEBUG 收到的消息:[{“response”:{“version”:“1.0.0”}}]
总时间:33.625秒