java Camel:从直接路由到处理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27244323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:28:36  来源:igfitidea点击:

Camel: Route from direct to processor

javaspringjmsapache-camel

提问by user_1234567_java

I have a Spring DSL route like:

我有一个 Spring DSL 路由,如:

<bean id="sendMsgProc" class="com.tc.infrastructure.utils.jms.SendMessageProcessor"/>   

   <camel:camelContext id="folder-jms" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">
    <camel:propertyPlaceholder id="jmsProps" location="classpath:jms-jndi.properties"/>
        <route id="folder-jms-route" autoStartup="true">
           <!-- <from uri="{{jms.output.folder}}"/> -->
           <from uri="direct:start"/>  
          <!--  <to uri="bean:camelMsgBean"/> -->
           <camel:process ref="sendMsgProc"/>
           <to uri="{{jms.in.send}}"/> 
        </route>
    </camel:camelContext> 

And my main class which starts context like:

我的主类开始上下文,如:

SpringCamelContext conetx = (SpringCamelContext)camel.initContextCamel("camel-context.xml", "folder-jms");
            Exchange ex = new DefaultExchange(conetx);
            ex.getIn().setBody(executionTasks.entrySet().iterator().next().getValue(), CamelMessage.class);
            conetx.start();
            conetx.startRoute("folder-jms-route");

            Thread.sleep(10000);
            conetx.stopRoute("folder-jms-route");
            conetx.stop();

And I have a processor to get my object form exchange like:

我有一个处理器来让我的对象形式交换,如:

public class SendMessageProcessor implements Processor {


    //This processor exist for set headers into sending message
    public void process(Exchange exchange) throws Exception 
    {
        System.out.println("adasdasd");
        CamelMessage message = (CamelMessage)exchange.getIn().getBody(CamelMessage.class);
        System.out.println("Message with correlationId get for exchange " + message.getMsgCorrelationId());
        System.out.println("Body" + message.getBody());
        }
}

I do set to Exchange in Camel the object from Map like:

我确实将 Map 中的对象设置为在 Camel 中交换,例如:

public class CamelMessage extends Message {


    private Map<String, Object> headersMap;
    private StringBuffer body;
    private String msgCorrelationId;

                public CamelMessage(File msgPath, String msgCorrelationId)
        {
            super.setMsgPath(msgPath);
            this.msgCorrelationId = msgCorrelationId;
        }

         public CamelMessage(String correlationID, Map<String, Object> headers, String body)
        {
            setMsgCorrelationId(correlationID);
            setHeadersMap(headers);
            setBody(body);
        }

    public Map<String, Object> getHeadersMap() {
        return headersMap;
    }
    protected void setHeadersMap(Map<String, Object> headersMap) {

        if(headersMap == null)
               headersMap = new HashMap<String, Object>();

        this.headersMap = headersMap;
    }


    public String getBody() {
        return body.toString();
    }
    protected void setBody(String body) {
        if(this.body == null)
            this.body = new StringBuffer();

        this.body.append(body);
    }



    public String getMsgCorrelationId() {
        return msgCorrelationId;
    }
    private void setMsgCorrelationId(String msgCorrelationId) {
        this.msgCorrelationId = msgCorrelationId;
    }
}

I can't understand why my Camel Processor doesnt work(doesn't trigger automaticaly). And I expected to get my Object which I setted in exchange camel with all field filled up.

我不明白为什么我的 Camel 处理器不起作用(不会自动触发)。我希望得到我设置的对象,我用骆驼交换所有字段都填满了。

Please help.

请帮忙。

回答by Milan Baran

I would add after your conetx.startRoute("folder-jms-route");

我会在你之后添加 conetx.startRoute("folder-jms-route");

ProducerTemplate pt = conetx.createProducerTemplate();
pt.send("direct:start", ex);

-- Look at http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultProducerTemplate.htmlfor more info

-- 查看http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultProducerTemplate.html了解更多信息

send(String endpointUri, Exchange exchange)
Sends the exchange to the given endpoint 

Notice: that if the processing of the exchange failed with an Exception it is not thrown from this method, but you can access it from the returned exchange using Exchange.getException().

回答by selva

Consumer End point always expecting some Input data.
For Example You have declared JMS Consumer Endpoint whenever the JMS Queues receive the message then route get activated and its calls next processor which is defined next to consumer end point.
But In your case , you have declared direct consumer endpoint so some producer endpoint should send message to Direct endpoint.
So Here we are using ProducerTemplate in java DSL to send message to Direct endpoint.
In Spring DSL example :


<route id="parent">
<from uri="file:location"/>
<to uri="direct:start"/>
<route/ >

<route id="child">
<from uri="direct:start"/>
<to uri="bean:textProcessor"/>
<route/ >

Here we have parent and child route once parent route get the file form defined location it send it to the direct endpoint in the parent route.
child route consumer end point is direct so now message will come to child route.

消费者端点总是期待一些输入数据。
例如,您已经声明了 JMS 消费者端点,只要 JMS 队列接收到消息,然后路由就会被激活,并且它调用在消费者端点旁边定义的下一个处理器。
但是在您的情况下,您已经声明了直接消费者端点,因此某些生产者端点应该向 Direct 端点发送消息。
所以在这里我们使用 Java DSL 中的 ProducerTemplate 将消息发送到 Direct 端点。
在 Spring DSL 示例中:


<route id="parent">
<from uri="file:location"/>
<to uri="direct:start"/>
<route/>

<route id="child">
<from uri ="direct:start"/>
<to uri="bean:


这里我们有父路由和子路由,一旦父路由获取文件形式定义的位置,它将它发送到父路由中的直接端点。
子路由消费者端点是直接的,所以现在消息将到达子路由。