Issue
I am trying to use Camel with rabbitMQ in Spring MVC the following way:
camel-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="routeBuilder" />
</camelContext>
</beans>
the routeBuilder class:
@Service(value = "routeBuilder")
public class CamelContextRouteBuilder extends RouteBuilder {
@Value("${flag:true}")
private boolean flag;
@Override
public void configure() throws Exception{
if (flag) {
createRoutes();
}
}
private void createRoutes() {
from(String.format("spring-amqp:...))
.throttle(...).timePeriodMillis(...)
.bean("msgProcessor");
}
}
and the processor class:
@Component(value = "msgProcessor")
public class MsgProcessor implements Processor {
public MsgProcessor() {}
@Override
public void process(final Exchange exchange) throws Exception {
Message message = exchange.getMessage();
...
}
}
the dependencies are:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
<version>2.25.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>2.25.0</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.25.0</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>jaxb-impl</artifactId>
<groupId>com.sun.xml.bind</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.bluelock</groupId>
<artifactId>camel-spring-amqp</artifactId>
<version>1.7.0</version>
<exclusions>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-rabbit</artifactId>
<groupId>org.springframework.amqp</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-mapper-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
</exclusions>
</dependency>
I manage to build the project and run it, I see the rabbit queue has consumers so the routes are created but when a message is processed (failed to get to the listener), the following exception is thrown:
CamelContext must be specified on: Message[]
{"filename":"CamelLogger.java","lineno":"213","stacktrace":"org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:1498)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1409)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1329)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.retry.interceptor.StatefulRetryOperationsInterceptor$MethodInvocationRetryCallback.doWithRetry(StatefulRetryOperationsInterceptor.java:229)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:286)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:210)
at org.springframework.retry.interceptor.StatefulRetryOperationsInterceptor.invoke(StatefulRetryOperationsInterceptor.java:173)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)\n\tat org.springframework.amqp.rabbit.listener.$Proxy342.invokeListener(Unknown Source)\n\tat org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1316)\n\tat org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1295)\n\tat org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:746)\n\tat org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:730)\n\tat org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$500(SimpleMessageListenerContainer.java:74)\n\tat org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:946)\n\tat amqp.spring.camel.component.SpringAMQPConsumer$SpringAMQPExecutorTask.run(SpringAMQPConsumer.java:316)\n\tat amqp.spring.camel.component.SpringAMQPConsumer$SpringAMQPExecutorTask.run(SpringAMQPConsumer.java:316)\n\tat java.lang.Thread.run(Thread.java:748)\nCaused by: java.lang.IllegalArgumentException: CamelContext must be specified on: Message[]\n\tat org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:340)\n\tat org.apache.camel.impl.DefaultMessage.createHeaders(DefaultMessage.java:255)\n\tat org.apache.camel.impl.DefaultMessage.getHeaders(DefaultMessage.java:216)\n\tat amqp.spring.camel.component.SpringAMQPHeader.setBasicPropertiesToHeaders(SpringAMQPHeader.java:61)\n\tat amqp.spring.camel.component.SpringAMQPMessage.fromAMQPMessage(SpringAMQPMessage.java:53)\n\tat amqp.spring.camel.component.SpringAMQPConsumer$RabbitMQMessageListener.onMessage(SpringAMQPConsumer.java:197)\n\tat org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1406)\n\t... 24 more","level":"WARN","exception_class":"org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException","logger_name":"amqp.spring.camel.component.SpringAMQPConsumer","thread":"SpringAMQPConsumer.SpringAMQPExecutor-5","message":"Caused by: [org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException
- Listener threw exception]","timestamp":"2020-10-30 14:29:48,876","exception_message":"Listener threw exception"}
Where does the CamelContext usage is missing?
Solution
Ok, using the following connection:
from("spring-amqp"...)
is a deprecated package:
<groupId>com.bluelock</groupId>
removing it and using the following instead:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rabbitmq</artifactId>
<version>${camel.version}</version>
</dependency>
from("rabbitmq:...")
solved the issue.
Answered By - Reshef
Answer Checked By - Marilyn (JavaFixing Volunteer)