Trouble filtering in Camel

孤者浪人 提交于 2019-12-08 03:40:13

问题


I am attempting to filter out the null bodies from my route. The route polls from the function every half second or so. So, I get this error in my console also every half-second. Here is the stack trace:

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                            Elapsed (ms)
[route1            ] [route1            ] [browserBean                                                                       ] [         0]
[route1            ] [filter1           ] [filter[simple{(${body} == null)}]                                             ] [         0]

Exchange
---------------------------------------------------------------------------------------   ------------------------------------------------
Exchange[
Id                  ID-CO183LTCS06-54352-1403798372606-0-1058
ExchangePattern     InOnly
Headers             {breadcrumbId=ID-CO183LTCS06-54352-1403798372606-0-1057,     CamelRedelivered=false, CamelRedeliveryCounter=0}
BodyType            null
Body                [Body is null]
]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------

java.lang.NullPointerException: null
at com.hello.integration.GreetingController.greeting(GreetingController.java:15)
at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:407)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:278)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:251)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:166)
at     org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:67)
at     org.apache.camel.impl.ProcessorPollingConsumer.receive(ProcessorPollingConsumer.java:58)
at     org.apache.camel.impl.ProcessorPollingConsumer.receiveNoWait(ProcessorPollingConsumer.java:    66)
at org.apache.camel.impl.DefaultScheduledPollConsumer.poll(DefaultScheduledPollConsumer.java:48)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:187)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:114)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at     java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown     Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown     Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Any Ideas? I feel like it's simple but I've been looking all over the web for solutions and can't find anything that solves my problem.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd

    http://camel.apache.org/schema/osgi
    http://camel.apache.org/schema/osgi/camel-osgi.xsd

    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd

    http://camel.apache.org/schema/spring
    http://camel.apache.org/schema/spring/camel-spring.xsd">

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
    <from uri="browserBean"/>
        <filter>
            <simple>${body} == null</simple>
            <stop/>
        </filter>
    <to uri="jms:queue:testQSource"/>
    <to uri="myBean"/>
    <log message="Routing message from testQSource to testQDestination queue with data ${body}"/>
    <to uri="jms:queue:testQDestination"/>
    <to uri="finalBean"/>
    <log message="message: ${body}"/>
</route>
</camelContext>

<camel:camelContext id="camel-client">
<camel:template id="camelTemplate" />
</camel:camelContext>

<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="brokerURL" value="tcp://localhost:61616" />
</bean>\

<bean id="myBean" class="com.example.integration.modifier"/>
<bean id="finalBean" class="com.example.integration.ActionApp"/>
<bean id="browserBean" class="com.hello.integration.GreetingController"/>

</beans>

EDIT: The null body is coming from the following function:

package com.hello.integration;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

@Controller
public class GreetingController {


    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        //Thread.sleep(3000); // simulated delay
        return new Greeting("Hello, " + message.getName() + "!");
    }

}

Even though I attempt filter out the null bodies, I am still getting the error. Am I filtering them out too late?


回答1:


You cannot put the filter inside of from tag. The below route should work :)

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
    <from uri="browserBean"/>

        <filter>
            <simple>${body} == null</simple>
            <stop/>
        </filter>

    <to uri="jms:queue:testQSource"/>
    <to uri="myBean"/>
    <log message="Routing message from testQSource to testQDestination queue with data ${body}"/>
    <to uri="jms:queue:testQDestination"/>
    <to uri="finalBean"/>
    <log message="message: ${body}"/>
</route>



回答2:


You can also use a choice clause, as another way to do it. Here is an example:

      <route>
          <from uri="direct:routePublishedResponse"/>
          <bean ref="caseHearingHandler"/>
          <choice>
             <when>
                <simple>${body} != null</simple>
                <to uri="direct:resultSubRoute">
             </when>
             <otherwise>
                <log message="skipping due to NULL body" loggingLevel="DEBUG" />
             </otherwise>
          </choice>
       </route>


来源:https://stackoverflow.com/questions/24420660/trouble-filtering-in-camel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!