Apache Camel -调用简单的语言而没有副作用

e1xvtsh3  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(118)

有没有可能直接在路由中用Simple调用一个对象而没有副作用?

.toD("${header.exchangeHelper.inc1()}") //works but fails if there is a return type from the called method
.bean(new Simple("${header.exchangeHelper.inc1()}")) //works but sets the body to false

这两种方法都没有给予理想的解。

bgibtngc

bgibtngc1#

你可以将结果存储到exchange属性或头文件中。这样你就可以保留原来的方法体,并从方法中获取结果,以备以后需要。或者你也可以使用处理器调用方法。
这些方法通常比使用简单语言更好地使用Java-DSL,因为它们受益于IDE的重构工具、错误突出显示和许多形式的删除。

package com.example;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.camel.Exchange;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class ExampleTest extends CamelTestSupport {

    ExchangeHelper exchangeHelper = mock(ExchangeHelper.class);

    @Test
    public void useSetPropertyTest() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:result");
        resultMockEndpoint.expectedMessageCount(1);
        resultMockEndpoint.message(0).body().isEqualTo("Hello");

        when(exchangeHelper.inc1()).thenReturn(true);

        template.sendBodyAndHeader("direct:useSetProperty", "Hello", 
            "exchangeHelper", exchangeHelper);

        verify(exchangeHelper, times(1)).inc1();
        resultMockEndpoint.assertIsSatisfied();
    }

    @Test
    public void justUseProcessorTest() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:result");
        resultMockEndpoint.expectedMessageCount(1);
        resultMockEndpoint.message(0).body().isEqualTo("Hello");

        when(exchangeHelper.inc1()).thenReturn(true);

        template.sendBody("direct:justUseProcessor", "Hello");

        verify(exchangeHelper, times(1)).inc1();
        resultMockEndpoint.assertIsSatisfied();
    }

    @Test
    public void useHeaderFromProcessorTest() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:result");
        resultMockEndpoint.expectedMessageCount(1);
        resultMockEndpoint.message(0).body().isEqualTo("Hello");

        when(exchangeHelper.inc1()).thenReturn(true);

        template.sendBodyAndHeader("direct:useHeaderFromProcessor", "Hello", 
            "exchangeHelper", exchangeHelper);

        verify(exchangeHelper, times(1)).inc1();
        resultMockEndpoint.assertIsSatisfied();
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder(){

            @Override
            public void configure() throws Exception {

                from("direct:useSetProperty")
                    .setProperty("result")
                        .simple("${header.exchangeHelper.inc1()}")
                    .log("Body: ${body} Result header: ${exchangeProperty.result}")
                    .to("mock:result")
                    .removeProperty("result");

                from("direct:justUseProcessor")
                    .process( ex -> { exchangeHelper.inc1(); })
                    .log("Body: ${body}")
                    .to("mock:result");

                from("direct:useHeaderFromProcessor")
                    .process( ex -> {  
                        ex.getMessage()
                            .getHeader("exchangeHelper", ExchangeHelper.class)
                            .inc1();
                    })
                    .log("Body: ${body}")
                    .to("mock:result");
            }
        };
    }

    interface ExchangeHelper {
        public boolean inc1();
    }
}
ego6inou

ego6inou2#

没有尝试过,但是为什么不使用wiretapEIP向您的requestHelper方法发出一个额外的(和分离的!)请求呢?
类似于:

from("direct:demo")
    .wireTap("bean:${header.exchangeHelper.inc1()}")
    .to("direct:doSomething");

相关问题