mule3.X + spring3.X

本文介绍了一个Mule 3.X与Spring 3.X整合的示例,展示了如何利用Spring的JDBCTemplate进行数据库操作,并在Mule.xml中将Spring的Bean作为组件使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对Mule3.X有了一些理解和铺垫以后,马上上一个非常有营养的例子,mule3.X结合spring3.X的例子。

原理:在数据库中存放一张user表,使用spring的JDBCTamplate去进行增删改查,然后再mule.xml中把spring的bean当做它自己的component来使用。

这么做符合我们的需求,即mule完成多渠道接入以及服务编排,比如外部服务通过tcp接入,mule配置endpoint接入,然后调用spring的service层的bean处理,返回,然后接出。这样做的目的是,保持了程序分层的完整性。

不多说,先上mule配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"         xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc"  xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="CE-3.3.0" xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.mulesoft.org/schema/mule/stdio
http://www.mulesoft.org/schema/mule/stdio/current/mule-stdio.xsd
http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd ">
		<context:property-placeholder location="classpath:jdbc.properties"/>
	<spring:beans>
	     <spring:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	        <spring:property name="driverClassName" value="${jdbc.driverClassName}"/>
	        <spring:property name="url" value="${jdbc.url}"/>
	        <spring:property name="username" value="${jdbc.username}"/>
	        <spring:property name="password" value="${jdbc.password}"/>
	        <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>
    
    	<spring:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<spring:property name="dataSource" ref="dataSource"></spring:property>
    	</spring:bean>
		<spring:bean id="userDao" scope="prototype" class="com.yucheng.dao.UserDaoImpl">
			<spring:property name="jdbcTemplate" ref="jdbcTemplate"></spring:property>
		</spring:bean>
	</spring:beans>
	<!--
		The Mule model initialises and manages your UMO components
	-->
	<flow name="insertfff">
			<vm:inbound-endpoint path="insert123" />
			<pooled-component>
				<method-entry-point-resolver>
					<include-entry-point method="insert"/>
				</method-entry-point-resolver>
				<spring-object bean="userDao"></spring-object>
			</pooled-component>
	</flow>
</mule>
mule的配置文件跟spring很相似,容易看懂,flow是mule3.X中的新概念,也就是常说的服务编排。注意在mule的user guide中说明mule与spring有三种接入方式:

1 Sending and Receiving Mule Events in Spring 

这种方式将mule的endpoint等组件配置进spring,使用spring的事件机制运行,这种很spring的方式,比较难以学习与驾驭,个人不推荐使用。

2 Spring Application Contexts 
3 Using Spring Beans as Service Components .

后两种貌似没什么区别(也许我太笨了?),第二种主要讲了spring配置文件与mule配置文件共存的问题。关于第三种方式,好像在3.X中不支持?反正我没有用成功service Components,再说mule 3.X中也出现新的flow概念来替代service Components,推举使用flow。

component中通过:

<method-entry-point-resolver>
<include-entry-point method="insert"/>
</method-entry-point-resolver>

来指定component中的具体方法,如果只有一个方法,可以自动调用。



亲测上述流程,增删改查都能成功:

	@Test
	public void testInsert() throws Exception {
		String config="mule-database-config.xml";
		MuleContext	muleContext = new DefaultMuleContextFactory().createMuleContext(config);
		muleContext.start();
		MuleClient client = new MuleClient(muleContext);
		MuleMessage response =  client.send("vm://insert123",new User("dd","fsgsfdg",5),null);
		System.out.println("response : "+response.getPayload());
	}



下次继续讲述muleContext与MuleClient的使用,另外还有mule的异常处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值