今天写了一个组件,用HttpClient向HttpBinding发送XML文档。HttpBC接收到后,发送给Static Rout Slip组件,由这具路由器,把XML分别发送到两个组件中:echo组件显示该消息,response组件在接收到这个消息后,经过简单的处理后,生成一个返回消息。
HttpClient代码如下所示:
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/**
* @version $Revision: 356269 $
*/
public class HttpClient {
public static void main(String[] args) throws Exception {
URLConnection connection = new URL("http://localhost:8192/Service/").openConnection();
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
// Post the request file.
FileInputStream fis = new FileInputStream("D://servicemix//httptest//request.xml");
//Buffer
byte[] buf = new byte[256];
for (int c = fis.read(buf); c != -1; c = fis.read(buf)) {
os.write(buf,0,c);
}
os.close();
fis.close();
// Read the response.
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}
而由Http BC组件与router 组件,还有echo组件,response组件的配置如下:
Assemble配置:
<?xml version="1.0" encoding="UTF-8"?>
<jbi xmlns="http://java.sun.com/xml/ns/jbi" version="1.0">
<service-assembly>
<identification>
<name>http-staticrout-demo</name>
<description>http-staticrout-demo</description>
</identification>
<service-unit>
<identification>
<name>engine-ldt-su</name>
<description>Contains staticrout service</description>
</identification>
<target>
<artifacts-zip>engine-su.zip</artifacts-zip>
<component-name>servicemix-eip</component-name>
</target>
</service-unit>
<service-unit>
<identification>
<name>binding-ldt-su</name>
<description>Contains the binding</description>
</identification>
<target>
<artifacts-zip>binding-su.zip</artifacts-zip>
<component-name>servicemix-http</component-name>
</target>
</service-unit>
<service-unit>
<identification>
<name>component-ldt-su</name>
<description>component created by ldt</description>
</identification>
<target>
<artifacts-zip>lwechoreponse-su.zip</artifacts-zip>
<component-name>servicemix-lwcontainer</component-name>
</target>
</service-unit>
</service-assembly>
</jbi>
HttpBinding 组件的SU如下所示:
<?xml version="1.0"?>
<beans xmlns:http="http://servicemix.apache.org/http/1.0"
xmlns:demo="urn:servicemix:http-binding">
<http:endpoint service="demo:simple-service"
endpoint="simple-service"
role="consumer"
locationURI="http://localhost:8192/Service/"
defaultMep="http://www.w3.org/2004/08/wsdl/in-out"
/>
</beans>
EIP中的router组件的SU如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:eip="http://servicemix.apache.org/eip/1.0"
xmlns:demo="urn:servicemix:http-binding"
xmlns:test="http://ldt.com/test">
<eip:static-routing-slip service="demo:simple-service" endpoint="simple-service">
<eip:targets>
<eip:exchange-target service="test:echo" />
<eip:exchange-target service="test:response" />
</eip:targets>
</eip:static-routing-slip>
</beans>
而echo组件与response组件以POJO组件的形式,部署到LWContainer中,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: servicemix -->
<beans xmlns:my="http://ldt.com/test"
xmlns:sm="http://servicemix.apache.org/config/1.0">
<classpath>
<location>.</location>
</classpath>
<sm:serviceunit id="jbi">
<sm:activationSpecs>
<sm:activationSpec service="my:echo">
<sm:component>
<bean class="org.apache.servicemix.components.util.EchoComponent" />
</sm:component>
</sm:activationSpec>
<sm:activationSpec service="my:response">
<sm:component>
<bean class="ldt.com.staticrouter.ReceiverComponent" />
</sm:component>
</sm:activationSpec>
</sm:activationSpecs>
</sm:serviceunit>
</beans>
这个模型得以成功的意义在于:很容易把一个消息发送给多个组件,当业务流程改变时,要向多个组件发送消息时,只需要向router组件的配置文件进行修改,然后把相关的组件部署到ESB中,就完成了业务流程的改变。在我们项目中的应用有:当凤凰公司产品对一部份等级的用户进行下调时,就能用到。比如只向VIP与会员发送降价消息。则在发送消息之前,在router组件中设定VIP接收组件与会员接收组件就完成了业务流程的调整。