Singleton Startup Mule Component/Flow

故事扮演 提交于 2019-12-06 06:35:14

问题


I have a running Mule application I am using in my project. What I would like to do is add some component that would clear some database table everytime the Mule server is started up.

What would be the component to use in this matter? Preferably I'd like it to happen from XML and not some Java component I have to write (JDBC and such)

Thanks!


回答1:


Done with:

  • A notification listener to be informed when Mule is initialized,
  • Implemented in Groovy so all code is in the XML config,
  • A JDBC endpoint to purge the data so no JDBC is needed.

Here is the config:

<?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>


来源:https://stackoverflow.com/questions/9280908/singleton-startup-mule-component-flow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!