'How do I set custom tracer in ModelCamelContext?

I am implementing simple camel application using camel 3.14.0.

I start using modelcamelcontext. But there is below exception during setting custom tracer.

2022-04-29 17:04:18.474  INFO 10264 --- [           main] target.atom.engine.EngineApplication     : Started EngineApplication in 14.158 seconds (JVM running for 16.177)
2022-04-29 17:04:18.540 ERROR 10264 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Cannot set tracer on a started CamelContext
    at org.apache.camel.impl.engine.AbstractCamelContext.setTracer(AbstractCamelContext.java:4601) ~[camel-base-engine-3.14.0.jar:3.14.0]
    at target.atom.common.config.TargetAtomConfig.reloadRoutes(TargetAtomConfig.java:54) ~[classes/:na]
    at target.atom.common.config.TargetAtomConfig$$FastClassBySpringCGLIB$$7b88810d.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.1.jar:5.3.1]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.3.1.jar:5.3.1]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.1.jar:5.3.1]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.3.1.jar:5.3.1]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89) ~[spring-aop-5.3.1.jar:5.3.1]

How do I set tracer in modelCamelContext?

source code

ModelCamelContext modelCamelContext = camelContext.adapt(ModelCamelContext.class);
TraceFormatter formatter = new TraceFormatter();
modelCamelContext.setTracer(formatter.getTracer(modelCamelContext));
modelCamelContext.setTracing(true);
…


Solution 1:[1]

Assuming that you are using Camel Spring Boot, the CamelContext can be customized thanks to a bean of type CamelContextConfiguration that is meant to be used to add custom logic before and after Spring Boot and the CamelContext have been fully started.

In your case, the idea is to add custom logic before the start up, so your code should look like this:

@Bean
CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
        @Override
        public void beforeApplicationStart(CamelContext context) {
            // Configure the tracer before starting up the camel context
            TraceFormatter formatter = new TraceFormatter();
            context.setTracer(formatter.getTracer(modelCamelContext));
            context.setTracing(true);
        }

        @Override
        public void afterApplicationStart(CamelContext camelContext) {
            // Nothing to do
        }
    };
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicolas Filotto