Apache Camel custom Service and shutdown

Tags: , ,



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");

Answer

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



Source: stackoverflow