继续在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.xsdhttp://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://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 beinvoked depending on business rules, message contents, headers or any othercriteria. The pass-through-router is a router that automatically passeson 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 muleMuleContext muleContext;String config = "my-mule-jdbc-config.xml";muleContext = new DefaultMuleContextFactory().createMuleContext(config);muleContext.start();// creat mule clientMuleClient 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.呵呵.
联系客服