Apache Camel的multicast简单示例

这里以一个问题开始:

<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后,消息会复制多份,发送到后面所有的to节点,multicast可以指定线程池来并发,发送消息 没加的话,消息会依次流转到后面的节点

<bean id="bean1" class="com.pp.bean.Bean2">
	<property name="str" value="123"></property>
</bean>
<bean id="bean2" class="com.pp.bean.Bean2">
	<property name="str" value="456"></property>
</bean>

<bean id="changeInfo" class="com.pp.bean.Bean3" />

<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
	<template id="camelTemplate" />

	<route autoStartup="true">
		<from uri="timer://aaa?fixedRate=true&period=60000" />
		<multicast>
			<to uri="bean1" />
			<to uri="changeInfo" />
			<to uri="bean2" />
		</multicast>
	</route>

	<route autoStartup="false">
		<from uri="timer://aaa?fixedRate=true&period=60000" />
		<to uri="bean1" />
		<to uri="changeInfo" />
		<to uri="bean2" />
	</route>

</camelContext>

package com.pp.bean;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class Bean2 implements Processor 
{
	private String str;
	public void process(Exchange exchange) throws Exception 
	{
		System.out.println("+++++++++++++"+str+"+++++++++++++");
		System.out.println(exchange.getIn().getHeaders());
		System.out.println(exchange.getProperties());
		System.out.println("+++++++++++++"+str+"+++++++++++++");
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
}

package com.pp.bean;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class Bean3 implements Processor 
{
	public void process(Exchange exch) throws Exception
	{
		exch.getIn().setHeader("add_header", "111");
		exch.setProperty("add_property", "HTTPS");
	}
}

分别运行,这2个route,即可发现效果

第一个route中的changeInfo后,并没有反应到下面的节点中去,第二个就反应到下面的节点中去了

你可能感兴趣的:(camel)