OSGi容器中Bundle之间Asynchronous Communication

     Fuse ESB Enterprise Container中Application之间Asynchronous Communication有两种方式:ActiveMQ和

     NMR    Channel.

1. JMS Broker

     Fuse ESB Enterprise支持通过activemq feature部署JMS Broker, 在安装这个Feature之后,部署JMS Broker

     只需要将 broker配置文件Copy至热部署目录下。

    (1) The Default Broker

    Fuse ESB Enterprise启动时安装一个默认的Broker Instance,监听的端口是:61616,配置文件位于:

    etc/activemq-broker.xml,通过下面的命令查看安装ActiveMQ Broker:

    features:list  | grep activemq

    osgi:list | grep activemq

    Broker默认的数据目录:/data/activemq/default

    (2) Hot Deploymnet

    Copy ActiveMQ的配置文件至deploy目录,Fuse ESB Enterprise将自动创建一个数据目录位于:

    InstallDir/activemq-data/BrokerName

    ActiveMQ的配置文件有两中方式:

    A. Spring configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd">

	<broker xmlns="http://activemq.apache.org/schema/core"
		brokerName="simple-spring">
		<transportConnectors>
			<transportConnector name="openwire" uri="tcp://localhost:61000" />
		</transportConnectors>
	</broker>

</beans>

     B. Blueprint configuration file

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd
						http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

	<broker xmlns="http://activemq.apache.org/schema/core" brokerName="simple-blueprint">
		<transportConnectors>
			<transportConnector name="openwire" uri="tcp://localhost:61001" />
		</transportConnectors>
	</broker>

</blueprint>

     (3) Manage Brokers from the Console

     activemq:query, activemq:list

     (4) JMS Endpoints in a Router Application:test-timer.xml

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:camel="http://camel.apache.org/schema/spring" 
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/util
						http://www.springframework.org/schema/util/spring-util.xsd
						http://www.springframework.org/schema/osgi
						http://www.springframework.org/schema/osgi/spring-osgi.xsd
						http://www.springframework.org/schema/osgi-compendium
						http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
						http://camel.apache.org/schema/spring
						http://camel.apache.org/schema/spring/camel-spring.xsd
						http://www.springframework.org/schema/osgi
						http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
		<property name="brokerURL" value="tcp://localhost:61610" />
	</bean>

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<route>
			<from uri="timer://MyTimer?fixedRate=true&amp;period=4000" />
			<setBody>
				<constant>Hello World!</constant>
			</setBody>
			<to uri="activemq:camel.timer" />
		</route>
		<route>
			<from uri="activemq:camel.timer" />
			<to uri="file:/home/fdc/temp/sandpit/timer" />
		</route>
	</camelContext>
</beans>

     运行上述Broker步骤:

     A. 确保Feature activemq-camel已安装:features:install camel-activemq

     B. 确保activemq-broker Feature没安装:features:uninstall activemq-broker

     C. Copy 上述test-timer.xml文件至deploy目录.

2. Inter-Bundle Communication with the NMR

     (1)  Fuse ESB Enterpris提供了一个非标准的asynchronous messaging,为:Normalized Message Router

     (NMR),是JBI标准中定义的;NMR可用于在OSGi Container和JBI Container中进行asynchronous

     communication.

     A. Normalized messages in the JBI container

     JBI 标准定义了normalized message,是基于WSDL1.1和2.0的,一个完整的normalized message有下面的

     spect:

     Contet: 严格的XML格式

     Attachments:如果发送的是二进制的Content,可以添加附件;

     Properties: 键值对;

     Security Subject: 验证发送者。

     B. Normalized messages in the OSGi container

     Contet: 可以是任何的格式

     Attachments:可发送的二进制的附件;

     Properties: 键值对;

     Security Subject: 验证发送者。

     (2) The Apache Camel NMR Component

     The Apache Camel NMR Component是NMR的一个适配器,使Camle Application发送消息给OSGi Container

     中的Bundle或 JBI Container 中的Component。

     安装NMR Feature: features:install nmr

     实例化NMR Component: 在Bundle的Spring 配置文件中定义下面的Bean,并需要Import-Package:

     org.apache.servicemix.camel.nmr, org.apache.servicemix.nmr.api

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xmlns:camel-osgi="http://camel.apache.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
						http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
						http://camel.apache.org/schema/spring http://camel.apache.org/camel/schema/spring/camel-spring.xsd
						http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd">

	<bean id="nmr" class="org.apache.servicemix.camel.nmr.ServiceMixComponent">
		<property name="nmr">
			<osgi:reference interface="org.apache.servicemix.nmr.api.NMR" />
		</property>
	</bean>

</beans>

     Spring XML Defining a Route with an NMR Endpoint:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xmlns:camel-osgi="http://camel.apache.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
						http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
						http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
						http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd">

	<import resource="classpath:org/apache/servicemix/camel/nmr/camel-nmr.xml" />

	<camel-osgi:camelContext xmlns="http://camel.apache.org/schema/spring">
		<!-- Route periodically sent events into the NMR -->
		<route>
			<from uri="timer://myTimer?fixedRate=true&amp;period=2000" />
			<to uri="nmr:ExampleRouter" />
		</route>
		<!-- Route exchange from the NMR endpoint to a log endpoint -->
		<route>
			<from uri="nmr:ExampleRouter" />
			<bean ref="myTransform" method="transform" />
			<to uri="log:ExampleRouter" />
		</route>
	</camel-osgi:camelContext>

	<bean id="myTransform" class="org.apache.servicemix.examples.camel.MyTransform">
		<property name="prefix" value="MyTransform" />
	</bean>

</beans> 

相关推荐