Apache Camel 自定义服务和关闭

Apache Camel custom Service and shutdown

我已经实现了 Camel 服务,但是当我尝试关闭我的路由时,这是不可能的....我必须终止进程。我错过了什么?

首先我创建了一个 class 来实现 camel.Service :

@Service("myService")
public class MyService implements org.apache.camel.Service {

...
public WebSocket ws = null;
private Boolean isRunning=true;

public void mainCall() {

        try {

        .....
        ws = connect();

        while(isRunning) {
            .....

        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (WebSocketException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Override
public void start() throws Exception {
    isRunning = true;
    mainCall();
    
}

@Override
public void stop() throws Exception {
    isRunning = false;
    ws.disconnect();
    
}

我将我的服务添加到我的 Camel 上下文中,如下所示:

@Autowired
    private MyService myService;
@Autowired
   private CamelContext context;

    @PostConstruct
    public void setupCamelContext() throws Exception {
        
        ....
        context.addService(myService);

     
    }

最后我开始了我的路线:

from("timer://runOnce?repeatCount=1&delay=5000")
        .serviceCall("myService");

如果您想 stop/start 手动路由,请为 CAMEL 使用 HAWTIO。

这是 link:http://hawtio.github.io/hawtio/plugins/camel/

我通过将我的服务一分为二来解决我的问题:

  • 实施者org.apache.camel.Service
  • 第二个实现启动功能但带有 @Async 注释的人

我的主要问题是我的无限循环块卡住了启动函数,Asunc 方法解决了这个问题