首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Intercept进行Camel测试

使用Intercept进行Camel测试
EN

Stack Overflow用户
提问于 2012-11-30 22:18:24
回答 1查看 1.5K关注 0票数 2

我正在尝试截取一条消息,以跳过Http请求并继续我的路由。下面是你可以复制/粘贴来试用的类。

使用camel-test、camel-core、camel-http4 2.10.2和httpclient-osgi、httpcore-osgi 4.2.2

代码如下:

代码语言:javascript
复制
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

/**
 * Created with IntelliJ IDEA.
 * User: lleclerc
 * Date: 12-11-28
 * Time: 16:34
 * To change this template use File | Settings | File Templates.
 */
public class IsUseAdviceWithJUnit4Test extends CamelTestSupport {

    private String providerEndPointURI = "http://stackoverflow.com";
    private String timerEndPointURI = "timer://myTimer";
    private String mockEndPointURI = "mock:myMock";
    private String directEndPointURI = "direct:myDirect";
    private boolean messageIntercepted;

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                from(timerEndPointURI + "?fixedRate=true&delay=1000&period=1000")
                        .to(providerEndPointURI + "?throwExceptionOnFailure=false")
                        .to(mockEndPointURI);
            }
        };
    }

    @Test
    public void testIsUseAdviceWith() throws Exception {

        messageIntercepted = false;

        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {

                replaceFromWith(directEndPointURI);

                mockEndpoints();

                interceptSendToEndpoint(providerEndPointURI)
                        .skipSendToOriginalEndpoint()
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                messageIntercepted = true;
                                System.out.println("INTERCEPTED");
                            }
                        });
            }
        });

        // we must manually start when we are done with all the advice with
        context.start();

        getMockEndpoint(mockEndPointURI).expectedMessageCount(1);

        template.sendBody(directEndPointURI, "a trigger");

        assertMockEndpointsSatisfied();

        assertEquals(true, messageIntercepted);

        assertNotNull(context.hasEndpoint(directEndPointURI));
        assertNotNull(context.hasEndpoint("mock:" + directEndPointURI));

        assertNotNull(context.hasEndpoint(mockEndPointURI));
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Override
    public boolean isUseRouteBuilder() {
        return true;
    }
}

感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-11 22:48:07

camel-http4中有bug。

http://camel.465427.n5.nabble.com/Found-a-bug-with-camel-http4-td5723733.html

http://camel.465427.n5.nabble.com/Test-Intercept-with-adviceWith-and-http-td5723473.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13646956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档