servicemix 实例 -- 参考open source ESBs in action这本书
1. 项目结构
2. bean服务处理单元
1)Person类
package esb.chapter3; import java.io.StringWriter; import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult; import org.jibx.runtime.JiBXException; import esb.util.framework.JiBXUtil; public class Person { private String customerNumber;
private String firstName;
private String lastName;
private String street;
private String city;
private String state;
private String zip;
private String phone; public String toString(){
return "[custNum=" + customerNumber + ", firstName=" + firstName
+ ", lastName=" + lastName + ", city=" + city + " ]";
} //省略setter,getter方法
public static void main(String[] args) throws JiBXException, TransformerException { Person person = new Person(); person.setCustomerNumber("0001");
person.setFirstName("wentang");
person.setLastName("xu");
person.setStreet("street-1");
person.setCity("city-1");
person.setState("state-1");
person.setZip("111111");
person.setPhone("11111111111"); Source source = JiBXUtil.marshalDocument(person, "utf-8"); /*
* Source => XML
*/
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(); StringWriter sw = new StringWriter();
transformer.transform(source, new StreamResult(sw)); System.out.println("result:\n" + sw.toString());
}
}
2) SimpleTransformerBean类
package esb.chapter3; import javax.annotation.Resource;
import javax.jbi.component.ComponentContext;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.jbi.servicedesc.ServiceEndpoint;
import javax.xml.namespace.QName; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.servicemix.MessageExchangeListener;
import org.jibx.runtime.JiBXException; import esb.util.framework.JiBXUtil; public class SimpleTransformerBean implements MessageExchangeListener { private static final Log LOGGER = LogFactory.getLog(SimpleTransformerBean.class); @Resource
private DeliveryChannel channel; @Resource
private ComponentContext compContext; @Override
public void onMessageExchange(MessageExchange exchange) throws MessagingException { if (exchange.getStatus() != ExchangeStatus.ACTIVE) return; try {
/*
* 从in中解析出Person对象
*/
Person p = (Person) JiBXUtil.unmarshalDocument(exchange.getMessage("in").getContent(), Person.class);
LOGGER.info("receive person " + p.getFirstName() + " " + p.getLastName());
p.setFirstName("John"); exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange); ServiceEndpoint targetEndpoint = compContext.getEndpoint(
new QName("http://opensourceesb/chapter3/", "JMSProviderService"),
"outQueueWriter");
MessageExchange exch = channel.createExchangeFactory(targetEndpoint).createInOnlyExchange();
NormalizedMessage normalizedMsg = exch.createMessage();
normalizedMsg.setContent(JiBXUtil.marshalDocument(p, "UTF-8"));
exch.setMessage(normalizedMsg, "in");
channel.send(exch);
} catch (JiBXException e) {
LOGGER.error("JBI bean exception: ", e);
throw new MessagingException("Error transforming object to or from XML");
}
}
}
3) Jibx工具转换类
package esb.util.framework; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.io.StringWriter; import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import org.w3c.dom.Node; public class JiBXUtil { public static Object unmarshalDocument(Node node, Class targetClass) throws JiBXException {
return unmarshalDocument(new DOMSource(node), targetClass);
} /**
* 从源source中解封出目标类targetClass对象
* @param source
* @param targetClass
* @return
* @throws JiBXException
*/
public static Object unmarshalDocument(Source source, Class targetClass) throws JiBXException {
Object result = null;
try {
IUnmarshallingContext ctx = BindingDirectory.getFactory(targetClass).createUnmarshallingContext();
//toString(source), 把Source对象转换为XML字符串形式
result = ctx.unmarshalDocument(new StringReader(toString(source)));
} catch (Exception e) {
throw new JiBXException("Error unmarshalling XML to Object", e);
}
return result;
} /**
* 把对象src根据encoding编码封送到Source对象中
* @param src
* @param encoding -- utf-8
* @return
* @throws JiBXException
*/
public static Source marshalDocument(Object src, String encoding) throws JiBXException {
Source result = null; try {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
IMarshallingContext ctx = BindingDirectory.getFactory(src.getClass()).createMarshallingContext();
ctx.marshalDocument(src, encoding, null, bOut);
result = new StreamSource(new ByteArrayInputStream(bOut.toByteArray()));
} catch (Exception e) {
throw new JiBXException("Error marshalling XML to Object", e);
}
return result;
} /**
* 把Source对象转换为XML字符串形式
* @param source
* @return
* @throws TransformerException
*/
private static String toString(Source source) throws TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
StringWriter sw = new StringWriter();
Transformer trans = tf.newTransformer();
trans.transform(source, new StreamResult(sw));
String result = sw.toString();
System.out.println("result " + result);
return result;
}
}
4) Person => XML 映射文件, mapping.xml
<binding>
<mapping name="person" class="esb.chapter3.Person">
<value name="customer-number" field="customerNumber" />
<value name="first-name" field="firstName" />
<value name="last-name" field="lastName" />
<value name="street" field="street" />
<value name="city" field="city" />
<value name="state" field="state" />
<value name="zip" field="zip" />
<value name="phone" field="phone" />
</mapping>
</binding>
5) 对eclipse项目z_servicemix/bin目录下的.class文件,进行绑定再编译
<?xml version="1.0" encoding="UTF-8"?> <project name="JiBX-compiler" default="bind-compile" basedir="."> <property name="jibx-lib" value="D:/osesbinaction/libraries/jibx/lib" /> <taskdef name="bind" classname="org.jibx.binding.ant.CompileTask"
classpath="D:/osesbinaction/libraries/jibx/lib/jibx-bind.jar" /> <target name="bind-compile">
<bind verbose="true" load="true" binding="resources/chapter3/mapping.xml">
<classpath>
<pathelement path="bin"/>
<pathelement location="${jibx-lib}/jibx-run.jar"/>
</classpath>
</bind>
</target>
</project>
6) 运行Person类main方法。查看转换效果
7) xbean.xml
<beans xmlns:bean="http://servicemix.apache.org/bean/1.0"
xmlns:esb="http://opensourceesb/chapter3/"> <classpath>
<location>.</location>
<location>bcel.jar</location>
<location>jibx-bind.jar</location>
<location>jibx-extras.jar</location>
<location>jibx-run.jar</location>
<location>qdox-1.6.1.jar</location>
<location>stax-api.jar</location>
<location>wstx-asl.jar</location>
<location>xmlpull_1_1_4.jar</location>
<location>xpp3.jar</location>
</classpath> <bean:endpoint service="esb:beanService" endpoint="beanEndpoint"
bean="#SimpleTransformer" /> <bean id="SimpleTransformer" class="esb.chapter3.SimpleTransformerBean" />
</beans>
8)chapter3-bean-su服务单元目录结构
打包:jar cvf chapter3-bean-su.zip .
2 文件轮询绑定组件配置 chapter3-file-su
xbean.xml
<beans xmlns="http://xbean.org/schemas/spring/1.0"
xmlns:file="http://servicemix.apache.org/file/1.0"
xmlns:esb="http://opensourceesb/chapter3/"> <file:sender service="esb:fileSender"
endpoint="simpleFromJMSSender"
directory="chapter3/out">
</file:sender> <file:poller service="esb:filePoller"
endpoint="simpleToJMSPoller"
targetService="esb:JMSProviderService"
targetEndpoint="inQueueWriter"
file="chapter3/in"
period="2000">
</file:poller>
</beans>
目录结构:
打包: jar cvf chapter3-file-su.zip .
3. JMS绑定组件 chapter3-jms-su
xbean.xml
<beans xmlns:jms="http://servicemix.apache.org/jms/1.0"
xmlns:esb="http://opensourceesb/chapter3/"> <!-- esb:filePoller[simpleFromJMSSender]
=> esb:JMSProviderService[inQueueWriter]
=> inQueue队列
=> esb:JMSConsumerService[inQueueReader]
=> esb:beanService[beanEndpoint] 目录chapter/in => 文件轮询绑定组件
=> 消息提供者 => 队列 => 消息消费者 ###通过JMS连接了filepoller文件轮询组件和bean服务处理单元
=> 转到bean服务处理单元
-->
<jms:provider service="esb:JMSProviderService"
endpoint="inQueueWriter"
destinationName="inQueue"
connectionFactory="#connectionFactory" />
<jms:consumer service="esb:JMSConsumerService"
endpoint="inQueueReader"
targetService="esb:beanService"
targetEndpoint="beanEndpoint"
destinationName="inQueue"
connectionFactory="#connectionFactory" /> <!-- esb:beanService[beanEndpoint]
=> esb:JMSProviderService[outQueueWriter] 消息提供者
=> outQueue队列
=> esb:JMSConsumerService[inQueueReader2] 消息消费者
=> esb:fileSender[simpleFromJMSSender] bean服务处理单元
=> 消息提供者 => outQueue队列 => 消息消费者 ####通过JMS使服务处理单元与文件绑定组件间能够通信
=> 文件发送绑定组件 => chapter3/out目录
-->
<jms:provider service="esb:JMSProviderService"
endpoint="outQueueWriter"
destinationName="outQueue"
connectionFactory="#connectionFactory" />
<jms:consumer service="esb:JMSConsumerService"
endpoint="inQueueReader2"
targetService="esb:fileSender"
targetEndpoint="simpleFromJMSSender"
destinationName="outQueue"
connectionFactory="#connectionFactory"/> <bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
</beans>
打包: jar chapter3-jms-su.zip .
4. 服务装配(装配服务单元和绑定组件)
装配描述文件 META-INF/jbi.xml
<?xml version="1.0" encoding="UTF-8"?>
<jbi xmlns="http://java.sun.com/xml/ns/jbi" version="1.0">
<service-assembly> <identification>
<name>Chapter3-JMSBindingService</name>
<description>Example showing the jms binding component</description>
</identification> <service-unit>
<identification>
<name>SU-BEAN</name>
<description>The bean component</description>
</identification>
<target>
<artifacts-zip>chapter3-bean-su.zip</artifacts-zip>
<component-name>servicemix-bean</component-name>
</target>
</service-unit> <service-unit>
<identification>
<name>SU-JMS-Queue</name>
<description>A number of ftp pollers and senders</description>
</identification>
<target>
<artifacts-zip>chapter3-jms-su.zip</artifacts-zip>
<component-name>servicemix-jms</component-name>
</target>
</service-unit> <service-unit>
<identification>
<name>SU-JMS-File</name>
<description>A number of file pollers and senders files
</description>
</identification>
<target>
<artifacts-zip>chapter3-file-su.zip</artifacts-zip>
<component-name>servicemix-file</component-name>
</target>
</service-unit>
</service-assembly>
</jbi>
打包装配文件 jar cvf chapter3-jms-sa.zip META-INF chapter3-*-su.zip
部署装配文件chapter3-jms-sa.zip,把其拷贝到servicemix-3.2.1/hotdeploy目录下
启动 servicemix ESB
D:\osesbinaction\esb\apache-servicemix-3.2.1>bin\servicemix
Starting Apache ServiceMix ESB: 3.2.1 Loading Apache ServiceMix from servicemix.xml on the CLASSPATH
INFO - ConnectorServerFactoryBean - JMX connector available at: service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi INFO - JBIContainer - ServiceMix 3.2.1 JBI Container (ServiceMix) is starting
INFO - JBIContainer - For help or more informations please see: http://incubator.apache.org/servicemix/ INFO - ComponentMBeanImpl - Initializing component: #SubscriptionManager# INFO - jetty - Logging to org.apache.servicemix.http.jetty.JCLLogger@72f3a4a1 via org.apache.servicemix.http.jetty.JCLLogger INFO - DeploymentService - Restoring service assemblies ######
## 设置组件运行状态,并实例化组件
#####
INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-bean to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-bean INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-camel to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-camel INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-cxf-bc to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-cxf-bc INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-cxf-se to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-cxf-se INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-drools to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-drools INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-eip to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-eip INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-file to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-file INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-ftp to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-ftp INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-http to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-http INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-jms to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-jms INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-jsr181 to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-jsr181 INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-lwcontainer to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-lwcontainer INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-quartz to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-quartz
1 name = quartz.properties INFO - SimpleThreadPool - Job execution threads will use class loader of thread: main INFO - QuartzScheduler - Quartz Scheduler v.1.5.2 created. INFO - RAMJobStore - RAMJobStore initialized.
INFO - StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource fil
e in Quartz package: 'quartz.properties'
INFO - StdSchedulerFactory - Quartz scheduler version: 1.5.2
INFO - QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-saxon to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-saxon INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-script to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-script INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-truezip to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-truezip INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-wsn2005 to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-wsn2005 INFO - ComponentMBeanImpl - Setting running state for Component: servicemix-xmpp to Started
INFO - ComponentMBeanImpl - Initializing component: servicemix-xmpp ## 启动部署的服务装配组件
INFO - ServiceAssemblyLifeCycle - Starting service assembly: Chapter3-JMSBindingService INFO - ServiceUnitLifeCycle - Initializing service unit: SU-BEAN
INFO - ServiceUnitLifeCycle - Initializing service unit: SU-JMS-Queue
INFO - ServiceUnitLifeCycle - Initializing service unit: SU-JMS-File INFO - ServiceUnitLifeCycle - Starting service unit: SU-BEAN
INFO - ServiceUnitLifeCycle - Starting service unit: SU-JMS-Queue
INFO - ServiceUnitLifeCycle - Starting service unit: SU-JMS-File INFO - JBIContainer - ServiceMix JBI Container (ServiceMix) started
INFO - JDBCAdapterFactory - Database driver recognized: [apache_derby_embedded_jdbc_driver]
INFO - LogTask - Logging system reconfigured using file: file:/D:/osesbinaction/esb/apache-servicemix-3.2.1/conf/log4j.xml 在chapter3/in目录下拖入一个person.xml文件,控制台相关输出:
result <?xml version="1.0" encoding="UTF-8"?><person>
<customer-number>123</customer-number>
<first-name>John</first-name>
<last-name>Doe</last-name>
<street>1st Street</street>
<city>New York</city>
<state>NY</state>
<zip>567898</zip>
<phone>1768768768</phone>
</person>
INFO - SimpleTransformerBean - receivee person John Doe
servicemix 实例 -- 参考open source ESBs in action这本书的更多相关文章
- SQL/T-SQL实例参考
,D.[Score] B_Score ,'Distince'= CASE WHEN C.Score > D.Score THEN C.[Score] - D.[Score] WHEN C.Sco ...
- [AS3]as3用ByteArray来对SWF文件编码加密实例参考
[AS3]as3用ByteArray来对SWF文件编码加密实例参考,简单来说,就是将 swf 以 binary 的方式读入,并对 ByteArray 做些改变,再重新存成 swf 档.这个作业当然也可 ...
- SQL/T-SQL实例参考-2
对多关联查询,查询多中的记录,但是返回一的结果集 子查询语法 --一对多关联查询,查询多中的记录,但是返回一的结果集 SELECT C.* FROM ( SELECT A.BasicID FROM [ ...
- SQL/T-SQL实例参考-1
CASE ,D.[Score] B_Score ,'Distince'= CASE WHEN C.Score > D.Score THEN C.[Score] - D.[Score] WHEN ...
- Centos7 Firewall 防火墙配置应用实例参考(转)
时间:2016-06-02 02:40来源:linux.it.net.cn 作者:IT 简单的配置,参考学习:--permanent 当设定永久状态时 在命令开头或者结尾处加入此参数,否则重载或 ...
- ncl 实例参考
NCL中绘制中国任意省份的精确地图 NCL学习笔记(实战篇) 用NCL画垂直风场剖面图实例 NCL学习笔记(天气分析图)
- PlantUML的实例参考
project: blog target: plant-uml-instances.md date: 2015-12-24 status: publish tags: - PlantUML - UML ...
- R语言画图实例-参考R语言实战
dose <- c(, , , ,) drugA <- c(, , , , ) drugB <- c(, , , , ) # 数据准备 opar <- par(no.reado ...
- python搭建简易服务器实例参考
有关python搭建简易服务器的方法. 需求分析: 省油宝用户数 已经破了6000,原有的静态报表 已经变得臃肿不堪, 每次打开都要缓上半天,甚至浏览器直接挂掉 采用python搭建一个最最简易的 w ...
随机推荐
- win10上修改docker的镜像文件存储位置
记住:修改的是docker从服务器上拉下来的镜像文件存储位置(本地),是不是镜像源地址(服务器) 首先 win10下的docker有可视化操作界面和命令行操作,下载了docker-ce.exe双击后就 ...
- XPath 获取两个node中间的HTML Nodes
XPath 获取两个node中间的HTML Nodes 2015-06-01 16:42 972人阅读 评论(0) 收藏 举报 //div[@id="Recipe"]//h5[co ...
- HTML5八大特性助力移动WebApp开发
http://www.cocoachina.com/webapp/20150906/13344.html WebApp的实现基础就是HMTL5+JS+CSS3,但是WebApp还是基于浏览器的微网站开 ...
- iOS7之后JavaScript与Objective-C之间的通信
http://www.cocoachina.com/ios/20150906/13320.html 最近公司用Ping++集成了第三方支付,并且微信端也集成了这个功能,而微信付款时需要调用原生的支付宝 ...
- oracle连接命令
(1)conn[ect] 用法:conn 用户名/密码@网络服务器名 [as sysdba/sysoper] 当用特权用户身份连接时,必须带上as sysdba或是as sysoper 该命令常用 ...
- BZOJ1878 洛谷1972 HH的项链题解
洛谷链接 BZOJ链接 看到这样不用修改的题目,应该佷容易就联想到了离线来处理. 我们发现若将询问按照r来排序,排完后每次对答案有贡献的仅是每个颜色最后出现的位置 我们用next[i]表示i处颜色之前 ...
- 《mysql必知必会》4笔记(存储过程、游标、触发器、事务、全球化本地化、权限、数据库维护、性能)
二十三:使用存储过程: 1:mysql 5添加了对存储过程的支持.很多时候,一个完整的操作需要多条语句才能完成.存储过程简单来说,就是为以后的使用而保存的一条或多条mysql语句的集合,可将其视为批文 ...
- shell学习(17)- shell中2>&1的解释及输入输出重定向
大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...
- sql查询报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题
报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题 我的查询sql是 String sql2 =& ...
- @atcoder - AGC035D@ Add and Remove
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 N 张排成一行的卡片,第 i 张卡片上面写着 Ai. 重复 ...