首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

JBoss ESB学习札记14——第十三个ESB应用Transform CSV to XML

2014-07-12 
JBoss ESB学习笔记14——第十三个ESB应用Transform CSV to XML续上篇介绍了第十二个ESB应用,本文介绍第十三

JBoss ESB学习笔记14——第十三个ESB应用Transform CSV to XML

续上篇介绍了第十二个ESB应用,本文介绍第十三个ESB应用——Transform CSV to XML。

?

说明:本文及后续文章虽非百分百的原创,但毕竟包含本人的努力和付出,所以希望大家转载时务请注明出处:http://yarafa.iteye.com/,谢谢合作。

?

?

1 概述
本实例主要演示了如何将一个CSV文件(逗号分隔的值文件)转换成XML格式字符串,本实例的转换是通过配置Smooks实现的,这里的转换过程分为两步,一是将CSV转换成中间XML格式,然后再转换成目标XML格式。

?

2 新建ESB工程
操作过程略。

?

3 ESB配置
3.1 创建消息队列

<?xml version="1.0" encoding="UTF-8"?><server><mbean code="org.jboss.jms.server.destination.QueueService"name="jboss.esb.quickstart.destination:service=Queue,name=transformCSV2XMLGw" xmbean-dd="xmdesc/Queue-xmbean.xml"><depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends><depends>jboss.messaging:service=PostOffice</depends></mbean><mbean code="org.jboss.jms.server.destination.QueueService"name="jboss.esb.quickstart.destination:service=Queue,name=transformCSV2XMLEsb" xmbean-dd="xmdesc/Queue-xmbean.xml"><depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends><depends>jboss.messaging:service=PostOffice</depends></mbean></server>

?

3.2 定义Provider
这里将定义一个JMS Provider,并定义2个消息通道,内容如下:

<jms-provider connection-factory="ConnectionFactory" name="JBossMQ"><jms-bus busid="gwChanel"><jms-message-filter dest-name="queue/transformCSV2XMLGw"dest-type="QUEUE" /></jms-bus><jms-bus busid="esbChanel"><jms-message-filter dest-name="queue/transformCSV2XMLEsb"dest-type="QUEUE" /></jms-bus></jms-provider>

?

3.3 定义转换映射文件
在src目录下新建文件smooks-res.xml,内容如下:

<?xml version='1.0' encoding='UTF-8'?><smooks-resource-list xmlns=http://www.milyn.org/xsd/smooks-1.1.xsdxmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.1.xsd">    <profiles><profile base-profile="source-csv" /><profile base-profile="canonical-xml" /></profiles>    <csv:reader targetProfile="source-csv" fields="orderId,orderDate,statusCode,netAmount,totalAmount,tax,userName,firstName,lastName,state,orderLine1Position,orderLine1Quantity,orderLine1ProductId,orderLine1ProductTitle,orderLine1ProductPrice,orderLine2Position,orderLine2Quantity,orderLine2ProductId,orderLine2ProductTitle,orderLine2ProductPrice" /><resource-config target-profile="canonical-xml" selector="csv-set"><resource type="xsl">            <![CDATA[<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                xmlns:smooks-bean="xalan://org.milyn.templating.xslt.XalanJavabeanExtension"                extension-element-prefixes="smooks-bean"                version="1.0"><xsl:output method="xml" encoding="UTF-8" /><xsl:template match="*" name="templatelet"><orders><xsl:for-each select="csv-record"><Order orderId="{orderId}"                                orderDate="{orderDate}"                                statusCode="{statusCode}"                                netAmount="{netAmount}"                                totalAmount="{totalAmount}"                                tax="{tax}" ><Customer userName="{userName}" firstName="{firstName}" lastName="{lastName}" state="{state}" /><OrderLines><OrderLine posistion="{orderLine1Position}" quantity="{orderLine1Quantity}"><Product  productId="{orderLine1ProductId}" title="{orderLine1ProductTitle}" price="{orderLine1ProductPrice}"/></OrderLine><OrderLine posistion="{orderLine2Position}" quantity="{orderLine2Quantity}"><Product  productId="{orderLine2ProductId}" title="{orderLine2ProductTitle}" price="{orderLine2ProductPrice}"/></OrderLine></OrderLines></Order></xsl:for-each></orders>                </xsl:template></xsl:stylesheet>]]>        </resource>        <param name="is-xslt-templatelet">false</param>    </resource-config></smooks-resource-list>

?

3.4 定义Service

<service category="Transform" description="Transform CSV 2 XML"name="TransformService"><listeners /><actions mep="OneWay" /></service>

?

3.5 定义Listener

<listeners><jms-listener busidref="gwChanel" is-gateway="true"name="gwListener" /><jms-listener busidref="esbChanel" name="esbListener" /></listeners>

?

3.6 定义Action

<action value="Message before CVS to XMLtransformation" /></action><action value="/smooks-res.xml" /><property name="messageProfile" value="source-csv" /></action><action value="Message after CVS to XMLtransformation" /></action><action value="/smooks-res.xml" /><property name="messageProfile" value="canonical-xml" /></action><action /></action>

?

3.6 配置部署文件
部署依赖文件deployment.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?><jbossesb-deployment><depends>jboss.esb:deployment=smooks.esb</depends><depends>jboss.esb.quickstart.destination:service=Queue,name=transformCSV2XMLGw</depends><depends>jboss.esb.quickstart.destination:service=Queue,name=transformCSV2XMLEsb</depends></jbossesb-deployment>

?

3.7 部署ESB
将整个工程导出成一个ESB文件,并保存至JBoss ESB Server的部署目录下,启动JBoss ESB Server即可。

?

4 ESB客户端
4.1 新建Java工程

这里略去操作过程以及添加所需要的Jar包,具体操作过程可参考第一个ESB实例说明。

?

4.2 发送消息的客户端

/*********************************************************************** * <p>Project Name: helloworldclient</p> * <p>File Name: com.thu.afa.esb.jbossesb.client.TransformCSV2XMLClient.java</p> * <p>Copyright: Copyright (c) 2010</p> * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p> ***********************************************************************/package com.thu.afa.esb.jbossesb.client;import java.io.File;import java.util.Properties;import javax.jms.ObjectMessage;import javax.jms.Queue;import javax.jms.QueueConnection;import javax.jms.QueueConnectionFactory;import javax.jms.QueueSender;import javax.jms.QueueSession;import javax.naming.Context;import javax.naming.InitialContext;import org.jboss.soa.esb.util.FileUtil;/** * <p>Class Name: TransformCSV2XMLClient</p> * <p>Description: </p> * @author Afa * @date 2010-9-17 * @version 1.0 */public class TransformCSV2XMLClient{private QueueConnection connection;private QueueSession session;private Queue queue;public void setupConnection() throws Exception{Properties properties = new Properties();properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");   properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");   properties.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");InitialContext context = new InitialContext(properties);QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");connection = factory.createQueueConnection();queue = (Queue) context.lookup("queue/transformCSV2XMLGw");session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);connection.start();System.out.println("Connection Started");}public void stop() throws Exception{if(connection != null) connection.stop();if(session != null) session.close();if(connection != null) connection.close();}public void sendAMessage(String fileName) throws Exception{QueueSender sender = session.createSender(queue);ObjectMessage objectMessage;File file = new File(fileName);if(!file.exists()){throw new Exception("Input message file [" + file.getAbsolutePath() + "] not found.");}String message = FileUtil.readTextFile(file);objectMessage = session.createObjectMessage(message);objectMessage.setStringProperty("jbesbfilename", "transformedmessageCSV2XML.log");sender.send(objectMessage);sender.close();}public static void main(String[] args) throws Exception{TransformCSV2XMLClient client = new TransformCSV2XMLClient();client.setupConnection();client.sendAMessage("SampleOrder.csv");client.stop();}}

?

在客户端工程下创建SampleOrder.csv文件,并用文本编辑器编辑,输入以下内容:

1,Wed Nov 15 13:45:28 EST 2006,0,59.97,64.92,4.95,user1,Harry,Fletcher,SD,1,1,364,The 40-Year-Old Virgin,29.98,2,1,299,Pulp Fiction,29.992,Wed Nov 15 13:45:28 EST 2007,0,60.00,64.92,4.95,user2,Kalle,Anka,SD,1,1,364,Fletch,29.98,2,1,299,Fletch Lives,29.993,Wed Nov 15 13:45:28 EST 2007,0,60.00,64.92,4.95,user3,Donald,Duck,SD,1,1,364,The Goonies,29.98,2,1,4,Happy Gilmor,29.99

?

运行客户端程序即可看到以下输出结果:

JBoss ESB学习札记14——第十三个ESB应用Transform CSV to XML????????

?

?

今天阳光明媚,可心里却乌云密布……

天又黑了,可为什么总感觉天还没亮,肚子好饿……

?

-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Apr 9th, 2011
-----------------------------------------------------

?

1 楼 hesihua 2011-08-17   我今天也是心里乌云密布 希望你的急救  可是你老不上线 2 楼 yarafa 2011-08-30   哈哈,抱歉,1楼

热点排行