首页 > 解决方案 > Apache Camel 自定义服务和关闭

问题描述

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

首先,我创建了一个实现 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");

标签: javaspringapache-camel

解决方案


如果您想手动停止/启动路线,请使用 HAWTIO for CAMEL。

这是链接:http ://hawtio.github.io/hawtio/plugins/camel/


推荐阅读