Apache Camel中的multicast,没什么效果?

西夏一品堂 发布于 2015/09/05 21:45
阅读 520
收藏 0
<from uri="direct:multicast" />
<multicast>
<to uri="direct:s1" />
<to uri="direct:s2" />
<to uri="direct:s3" />
</multicast>





<from uri="direct:multicast" />
<to uri="direct:s1" />
<to uri="direct:s2" />

<to uri="direct:s3" />


感觉是一样的,既然如此,multicast是干啥的?

加载中
0
vidy_tu
vidy_tu

multicast 后面的节点是并发执行的,运行下看下结果一样不

package local.camel;

import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

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").multicast()
				.to("bean:local.camel.RoteTest?method=m1")
				.to("bean:local.camel.RoteTest?method=m2")
				.to("bean:local.camel.RoteTest?method=m3")				
				.end().bean(RoteTest.class , "end");
				
				from("direct://b")
				.to("bean:local.camel.RoteTest?method=m1")
				.to("bean:local.camel.RoteTest?method=m2")
				.to("bean:local.camel.RoteTest?method=m3")				
				.bean(RoteTest.class , "end");
			}
		};
		rb.addRoutesToCamelContext(context);
		context.start();
		ProducerTemplate template =context.createProducerTemplate(1);
		//template.sendBody("direct://a", "Hello");
		template.sendBody("direct://b", "Hello");
		context.shutdown();
		
	}
	public String m1(String str){
		System.out.println(str);
		return "m1:"+str+";";
	}
	public String m2(String str){
		System.out.println(str);
		return "m2:"+str+";";
	}
	public String m3(String str){
		System.out.println(str);
		return "m3:"+str+";";
	}
	public String end(String str){
		System.out.println(str);
		return "end"+str+";";
	}
	
}



西夏一品堂
西夏一品堂
谢谢。一看就知道楼主精通camel.
0
西夏一品堂
西夏一品堂

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

multicast 后面的节点是并发执行的,运行下看下结果一样不

package local.camel;

import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

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").multicast()
				.to("bean:local.camel.RoteTest?method=m1")
				.to("bean:local.camel.RoteTest?method=m2")
				.to("bean:local.camel.RoteTest?method=m3")				
				.end().bean(RoteTest.class , "end");
				
				from("direct://b")
				.to("bean:local.camel.RoteTest?method=m1")
				.to("bean:local.camel.RoteTest?method=m2")
				.to("bean:local.camel.RoteTest?method=m3")				
				.bean(RoteTest.class , "end");
			}
		};
		rb.addRoutesToCamelContext(context);
		context.start();
		ProducerTemplate template =context.createProducerTemplate(1);
		//template.sendBody("direct://a", "Hello");
		template.sendBody("direct://b", "Hello");
		context.shutdown();
		
	}
	public String m1(String str){
		System.out.println(str);
		return "m1:"+str+";";
	}
	public String m2(String str){
		System.out.println(str);
		return "m2:"+str+";";
	}
	public String m3(String str){
		System.out.println(str);
		return "m3:"+str+";";
	}
	public String end(String str){
		System.out.println(str);
		return "end"+str+";";
	}
	
}



<pipeline>
	<bean ref="foo" />
	<bean ref="bar" />
	<to uri="activemq:OutputQueue" />
</pipeline>

<multicast>
	<bean ref="foo" />
	<bean ref="bar" />
	<to uri="activemq:OutputQueue" />
</multicast>




顺便再问一个,这两种方式有什么区别?
vidy_tu
vidy_tu
pipeline 加不加都一样的,即使不加那三个to内部也是会用pipeline串起来 要做到上一个的输出是下一个的输入,这本身就是pipeline 关于camel 支持的各种模式请参考: http://camel.apache.org/enterprise-integration-patterns.html
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部