营销手段:mule的jdbc transport

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 02:45:51
2009-03-17

mule进阶之jdbc transport:

文章分类:Java编程 关键字: mule esb jdbc

继续在mule的xml汪洋中遨游.
简单需求如下:向一个vm:queue发送map消息, mule根据map信息, 动态执行sql, 并返回数据.

select 的查询mule默认返回map数据.
配置文件:my-mule-jdbc-config.xml如下:

Xml代码
  1.  version="1.0" encoding="UTF-8"?>  
  2.  xmlns="http://www.mulesource.org/schema/mule/core/2.1"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jdbc="http://www.mulesource.com/schema/mule/jdbc/2.1"  
  5.     xmlns:spring="http://www.springframework.org/schema/beans"  
  6.     xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.1"  
  7.     xsi:schemaLocation="   
  8.           http://www.mulesource.com/schema/mule/jdbc/2.1 http://www.mulesource.com/schema/mule/jdbc/2.1/mule-jdbc-ee.xsd   
  9.           http://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsd   
  10.           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  11.              http://www.mulesource.org/schema/mule/vm/2.1 http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd">  
  12.   
  13.   
  14.      id="dataSource"  
  15.         class="org.apache.commons.dbcp.BasicDataSource"  
  16.         destroy-method="close">  
  17.          name="driverClassName"  
  18.             value="com.mysql.jdbc.Driver" />  
  19.          name="url"  
  20.             value="jdbc:mysql://192.168.10.120/sand_res" />  
  21.          name="username" value="username" />  
  22.          name="password" value="888" />  
  23.          name="maxActive" value="30" />  
  24.          name="maxIdle" value="10" />  
  25.          name="maxWait" value="1000" />  
  26.          name="defaultAutoCommit" value="true" />  
  27.       
  28.      name="jdbcConnector" dataSource-ref="dataSource">  
  29.          key="selectUser"  
  30.             value="SELECT first_name,last_name FROM app_user where first_name=#[map-payload:firstName]" />  
  31.   
  32.          key="insertUser"  
  33.             value="insert into app_user   
  34.             (id,first_name,last_name ) values(#[map-payload:id], #[map-payload:firstName], #[map-payload:lastName])" />  
  35.       
  36.       
  37.   
  38.       
  39.      name="databaseModel">  
  40.          name="insertUMO">  
  41.               
  42.               
  43.                  path="query"/>  
  44.               
  45.   
  46.              



    注意: 如果mule采用2.1, jdbc transport的namespase后缀为com, 而不是org, 如果写错,IDE不会提示,程序异常也很奇怪,让我折腾了一个下午:(
    测试程序:

    Java代码
    1. public class MyMuleClientTest   
    2. {   
    3.     public static void main(String[] args) throws MuleException   
    4.     {   
    5.         // create mule   
    6.         MuleContext muleContext;   
    7.         String config = "my-mule-jdbc-config.xml";   
    8.         muleContext = new DefaultMuleContextFactory().createMuleContext(config);   
    9.         muleContext.start();   
    10.         // creat mule client   
    11.         MuleClient client = new MuleClient();   
    12.         Map map = new HashMap();   
    13.         map.put("firstName", "feng");   
    14.         MuleMessage response = client.send("vm://query", map, null);          
    15.         System.out.println("response = " + response.getPayload());   
    16.     }   
    17.   
    18. }  
     


    执行的sql为:
    SELECT first_name,last_name FROM app_user where first_name="feng"

    insert的执行类似,只需修改如下:

    Xml代码
    1.   
    2.       
    3.          queryKey="insertUser" synchronous="true"/>  
    4.       
    5.   
     



    感觉mule的jdbc transtort, 就是微缩版的ibatis.呵呵.