请问camel中的 routingSlip 和 recipientList 有什么区别?

孤好梦中X人 发布于 2015/09/06 10:01
阅读 445
收藏 0

@滄海一夢 你好,想跟你请教个问题:

看到你在OSC上回答了很多关于camel的问题,感觉你很强大。

想问你一下

请问camel中的 routingSlip 和 recipientList 有什么区别?

<route>
	<from uri="direct:a" />
	<recipientList delimiter="," ignoreInvalidEndpoints="true">
		<header>myHeader</header>
	</recipientList>
</route>

<route>
	<from uri="direct:a" />
	<routingSlip uriDelimiter="," ignoreInvalidEndpoints="true">
		<header>myHeader</header>
	</routingSlip>  
</route>



貌似都是分解出多个to,请问,有什么区别?

谢谢

加载中
0
vidy_tu
vidy_tu

引用来自“滄海一夢”的评论

routingSlip 是单线程,顺序执行的,recipientList 可以在线程池中并发执行,参考如下例子

package local.camel;

import java.util.concurrent.Executors;

import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.language.MethodCallExpression;

public class RoteTest {
	public static int cnt = 0;

	public static void main(String[] args) throws Exception {
		DefaultCamelContext context = new DefaultCamelContext();
		RouteBuilder rb = new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct://a").routingSlip(new MethodCallExpression(RoteTest.class, "slip"));
				from("direct://b").bean(RoteTest.class, "log");
				from("direct://c").bean(RoteTest.class, "log");
				from("direct://d").recipientList(new MethodCallExpression(RoteTest.class, "slip")).executorService(Executors.newFixedThreadPool(2));
			}
		};
		rb.addRoutesToCamelContext(context);
		context.start();
		ProducerTemplate template = context.createProducerTemplate(1);
		template.sendBody("direct://a", "Hello");
		template.sendBody("direct://d", "Hello");
		context.shutdown();

	}

	public void log(String str) {
		System.out.println(Thread.currentThread().getId()+":"+str);
	}

	public String slip(Exchange exchage) {
		String str = exchage.getIn().getBody(String.class);
		if ("Hello".equals(str)) {
			return "direct://b,direct://c";
		} else {
			return "direct://c";
		}
	}
}



引用来自“刀下流人”的评论

既然这样的话, recipientList 完全可以代替routingSlip了,是不是

还是不太一样,routeSliping是将消息依次传入后面的节点进行处理,不会复制,后面的节点修改的是同一个消息,recipientList 会将消息复制多份发送到后面的节点,后面每个节点处理的消息不是同一份.

早晨说的不太对,不好意思


孤好梦中X人
孤好梦中X人
试了一下,的确是这样的。楼主果然很精通camel
0
vidy_tu
vidy_tu

routingSlip 是单线程,顺序执行的,recipientList 可以在线程池中并发执行,参考如下例子

package local.camel;

import java.util.concurrent.Executors;

import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.language.MethodCallExpression;

public class RoteTest {
	public static int cnt = 0;

	public static void main(String[] args) throws Exception {
		DefaultCamelContext context = new DefaultCamelContext();
		RouteBuilder rb = new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct://a").routingSlip(new MethodCallExpression(RoteTest.class, "slip"));
				from("direct://b").bean(RoteTest.class, "log");
				from("direct://c").bean(RoteTest.class, "log");
				from("direct://d").recipientList(new MethodCallExpression(RoteTest.class, "slip")).executorService(Executors.newFixedThreadPool(2));
			}
		};
		rb.addRoutesToCamelContext(context);
		context.start();
		ProducerTemplate template = context.createProducerTemplate(1);
		template.sendBody("direct://a", "Hello");
		template.sendBody("direct://d", "Hello");
		context.shutdown();

	}

	public void log(String str) {
		System.out.println(Thread.currentThread().getId()+":"+str);
	}

	public String slip(Exchange exchage) {
		String str = exchage.getIn().getBody(String.class);
		if ("Hello".equals(str)) {
			return "direct://b,direct://c";
		} else {
			return "direct://c";
		}
	}
}



0
孤好梦中X人
孤好梦中X人

引用来自“滄海一夢”的评论

routingSlip 是单线程,顺序执行的,recipientList 可以在线程池中并发执行,参考如下例子

package local.camel;

import java.util.concurrent.Executors;

import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.language.MethodCallExpression;

public class RoteTest {
	public static int cnt = 0;

	public static void main(String[] args) throws Exception {
		DefaultCamelContext context = new DefaultCamelContext();
		RouteBuilder rb = new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct://a").routingSlip(new MethodCallExpression(RoteTest.class, "slip"));
				from("direct://b").bean(RoteTest.class, "log");
				from("direct://c").bean(RoteTest.class, "log");
				from("direct://d").recipientList(new MethodCallExpression(RoteTest.class, "slip")).executorService(Executors.newFixedThreadPool(2));
			}
		};
		rb.addRoutesToCamelContext(context);
		context.start();
		ProducerTemplate template = context.createProducerTemplate(1);
		template.sendBody("direct://a", "Hello");
		template.sendBody("direct://d", "Hello");
		context.shutdown();

	}

	public void log(String str) {
		System.out.println(Thread.currentThread().getId()+":"+str);
	}

	public String slip(Exchange exchage) {
		String str = exchage.getIn().getBody(String.class);
		if ("Hello".equals(str)) {
			return "direct://b,direct://c";
		} else {
			return "direct://c";
		}
	}
}



既然这样的话, recipientList 完全可以代替routingSlip了,是不是
0
以善
以善

在使用routingslip的时候,camel抛了个异常:

routingslip用法如下:

from("direct:useDiscount")
    .routingSlip(new MethodCallExpression(UseDiscountName.class, "useDiscountMethod"))

异常如下:

org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: OrderContext()
    at org.apache.camel.impl.engine.AbstractCamelContext.normalizeUri(AbstractCamelContext.java:819) ~[camel-base-engine-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.RoutingSlip.prepareRecipient(RoutingSlip.java:339) ~[camel-core-processor-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.RoutingSlip.doRoutingSlipWithExpression(RoutingSlip.java:251) ~[camel-core-processor-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.RoutingSlip.process(RoutingSlip.java:197) ~[camel-core-processor-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:395) ~[camel-core-processor-3.8.0.jar:3.8.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148) [camel-base-engine-3.8.0.jar:3.8.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60) [camel-base-engine-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:147) [camel-core-processor-3.8.0.jar:3.8.0]
 

是我的用法有问题吗?

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部