-->

Singleton Startup Mule Component/Flow

2020-07-18 09:22发布

问题:

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>


标签: esb mule