Adding components to the Apache Camel main class

Basically you don’t need the Spring Framework to use Apache Camel. With pure Java DSL one way is to rely on the provided Camel main class to start your application. This approach works fine with most of the available components. But some need to be added to the Camel context of the main class (e.g. the Slack component). Basically the main class offers a method called.getOrCreateCamelContext() which should return you the default context where you can register the component. This however does not work and you keep running into exceptions during the execution of your application. (Problem occurred with camel-core 2.17.0 and camel-slack 2.17.0)
1 answer

Adding components to the Apache Camel main class

The main issue is that Camels main class does not handle its context appropriately. Although its method .getOrCreateCamelContext() returns a context and allows you to add the desired component, the context may not be as unique as one thinks when reading the corresponding description. Furthermore, when it comes to the execution of the code (main.run()), a post-processing method tends to override the created context with an empty one. To still be able to work with Camel and pure Java DSL, you can create a new main class as an extension of Camels main. Within this new class you start introducing your own instance of CamelContext and handle it in an uniquely manner. To do so the methods .getOrCreateCamelContext() and .postProcessContext() have to be overridden as shown within the provided code snippet.