【发布时间】:2012-03-06 01:23:59
【问题描述】:
我在我的项目中使用了一个正在运行的 Mule 应用程序。我想做的是添加一些组件,每次启动 Mule 服务器时都会清除一些数据库表。
在这个问题上要使用什么组件?最好我希望它发生在 XML 中,而不是我必须编写的一些 Java 组件(JDBC 等)
谢谢!
【问题讨论】:
我在我的项目中使用了一个正在运行的 Mule 应用程序。我想做的是添加一些组件,每次启动 Mule 服务器时都会清除一些数据库表。
在这个问题上要使用什么组件?最好我希望它发生在 XML 中,而不是我必须编写的一些 Java 组件(JDBC 等)
谢谢!
【问题讨论】:
完成:
这里是配置:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/3.2/mule-jdbc.xsd
">
<spring:beans>
<spring:bean id="jdbcDataSource" class="org.hsqldb.jdbc.JDBCDataSource">
<spring:property name="url" value="jdbc:hsqldb:mem:test-db" />
</spring:bean>
<lang:groovy id="dataInitializer">
<lang:inline-script><![CDATA[
import org.mule.api.context.notification.*;
import org.mule.context.notification.*;
import org.mule.module.client.MuleClient;
class DataInitializer implements MuleContextNotificationListener<MuleContextNotification> {
public void onNotification(MuleContextNotification notification) {
if (notification.action == MuleContextNotification.CONTEXT_STARTED)
new MuleClient(notification.muleContext).dispatch("jdbc://initialDataPurge", null, null)
}
}
]]></lang:inline-script>
</lang:groovy>
</spring:beans>
<notifications>
<notification event="CONTEXT"/>
<notification-listener ref="dataInitializer"/>
</notifications>
<jdbc:connector name="jdbcConnector" dataSource-ref="jdbcDataSource">
<jdbc:query key="initialDataPurge" value="DELETE FROM test;" />
</jdbc:connector>
</mule>
【讨论】: