mule進階之jdbc transport:

iteye_6308發表於2009-03-17

繼續在mule的xml汪洋中遨遊.
簡單需求如下:向一個vm:queue傳送map訊息, mule根據map資訊, 動態執行sql, 並返回資料.

select 的查詢mule預設返回map資料.
配置檔案:my-mule-jdbc-config.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.mulesource.com/schema/mule/jdbc/2.1"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.1"
    xsi:schemaLocation="
          http://www.mulesource.com/schema/mule/jdbc/2.1 http://www.mulesource.com/schema/mule/jdbc/2.1/mule-jdbc-ee.xsd
          http://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.mulesource.org/schema/mule/vm/2.1 http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd">


    <spring:bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <spring:property name="driverClassName"
            value="com.mysql.jdbc.Driver" />
        <spring:property name="url"
            value="jdbc:mysql://192.168.10.120/sand_res" />
        <spring:property name="username" value="username" />
        <spring:property name="password" value="888" />
        <spring:property name="maxActive" value="30" />
        <spring:property name="maxIdle" value="10" />
        <spring:property name="maxWait" value="1000" />
        <spring:property name="defaultAutoCommit" value="true" />
    </spring:bean>
    <jdbc:connector name="jdbcConnector" dataSource-ref="dataSource">
        <jdbc:query key="selectUser"
            value="SELECT first_name,last_name FROM app_user where first_name=#[map-payload:firstName]" />

        <jdbc:query key="insertUser"
            value="insert into app_user
            (id,first_name,last_name ) values(#[map-payload:id], #[map-payload:firstName], #[map-payload:lastName])" />
    </jdbc:connector>
   

    <!--
        The Mule model initialises and manages your UMO components
    -->
    <model name="databaseModel">
        <service name="insertUMO">
            <!-- any number of endpoints can be added to an inbound router -->
            <inbound>
                <vm:inbound-endpoint path="query"/>
            </inbound>

            <!--
                An outbound router can have one or more router configurations that can be
                invoked depending on business rules, message contents, headers or any other
                criteria. The pass-through-router is a router that automatically passes
                on every message it receives
            -->
            <outbound>
                <pass-through-router>
                    <jdbc:outbound-endpoint queryKey="selectUser" synchronous="true"/>
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>
 



注意: 如果mule採用2.1, jdbc transport的namespase字尾為com, 而不是org, 如果寫錯,IDE不會提示,程式異常也很奇怪,讓我折騰了一個下午:(
測試程式:

public class MyMuleClientTest
{
    public static void main(String[] args) throws MuleException
    {
        // create mule
        MuleContext muleContext;
        String config = "my-mule-jdbc-config.xml";
        muleContext = new DefaultMuleContextFactory().createMuleContext(config);
        muleContext.start();
        // creat mule client
        MuleClient client = new MuleClient();
        Map map = new HashMap();
        map.put("firstName", "feng");
        MuleMessage response = client.send("vm://query", map, null);       
        System.out.println("response = " + response.getPayload());
    }

}
 


執行的sql為:
SELECT first_name,last_name FROM app_user where first_name="feng"

insert的執行類似,只需修改如下:

            <outbound>
                <pass-through-router>
                    <jdbc:outbound-endpoint queryKey="insertUser" synchronous="true"/>
                </pass-through-router>
            </outbound>
 



感覺mule的jdbc transtort, 就是微縮版的ibatis.呵呵.


相關文章