Apache Camel自定义服务和关闭

huangapple go评论78阅读模式
英文:

Apache Camel custom Service and shutdown

问题

I have implemented a Camel Service, but when I try to shutdown my route, it's impossible.... I have to kill the process. What have I missed?

首先,我创建了一个实现camel.Service的类:

@Service("myService")
public class MyService implements org.apache.camel.Service {
    // ... 其他代码 ...
}

然后,我将我的服务添加到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");

请注意,这些是您提供的代码的翻译。

英文:

I have implemented a Camel Service , but when i try to shutdown my route , it's impossible .... I have to kill the process. What i have missed ?

First I create a class which implements 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();
	
}

I add my service on my Camel context like below :

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

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

     
	}

At the end i start my route :

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

答案1

得分: 0

使用HAWTIO来手动停止/启动CAMEL路由。

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

英文:

use HAWTIO for CAMEL if you want to stop/start routes manually.

Here's the link: http://hawtio.github.io/hawtio/plugins/camel/

答案2

得分: 0

我通过将我的服务拆分为两个部分来解决了我的问题:

  • 一个实现了org.apache.camel.Service
  • 第二个实现了start函数,但带有@Async注释

在我的情况下,主要问题是我的无限循环阻塞了start函数,@Async方法解决了这个问题。

英文:

I resolve my problem by splitting my Service in two :

  • One who implement org.apache.camel.Service
  • Second who implement start fucntion but with an @Async anotation

The main problem in my case was my infinite loop block stuck the start function, Asunc method resolve the problem

huangapple
  • 本文由 发表于 2020年7月28日 17:17:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63130839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定