Importing components into Mule

Bootstrapping component technologies into Mule can be accomplished using Spring beans. In the example below we demonstrates how to pull in Mule flows from a common component and changing the behavior of database caching to utilize C3P0.

The import-resource XML stanza looks on the classpath for a resource named common-flows.xml. In our case, the common flows are a set of Mule flows which are used across a number of different projects. They’re built separately and pushed to Nexus in a Jar file, which is included by other Mule projects that have a need for the common functions.

The C3p0 bean Id demonstrates an example of how we would override the default database pooling strategy, replacing it with C3p0 which offers better reliability and control. Like the earlier common jar example, the Jar file is pulled into our project by the Java class loader and the Bean is pulled into our project.

  <spring:import resource="classpath:common-flows.xml"/>

  <spring:bean id="C3p0_snf_metadataPooledDataSource" 
	<spring:property name="driverClass" value="com.mysql.jdbc.Driver"/>
	<!-- Pooling -->
	<spring:property name="minPoolSize" value="${mysql.minPoolSize}"/>
	<spring:property name="maxPoolSize" value="${mysql.maxPoolSize}"/>
	<spring:property name="maxIdleTimeExcessConnections" value="600"/>
	<!-- Administrative -->
	<spring:property name="dataSourceName" value="my_dsn"/>
	<spring:property name="user" value="${mysql.username}"/>
	<spring:property name="password" value="${mysql.password}"/>
	<spring:property name="jdbcUrl" value="${mysql.url}"/>
	<!-- How long to wait for a connection -->
	<spring:property name="checkoutTimeout" value="10000"/>
	<!-- These setting control what to do if/when 
	     we get a database connection failure -->
	<spring:property name="acquireRetryAttempts" value="30"/>
	<spring:property name="acquireRetryDelay" value="10000"/>
	<spring:property name="breakAfterAcquireFailure" value="false"/>
	<!-- These settings control how to test existing connections -->
	<spring:property name="preferredTestQuery" value="select 1"/>
	<spring:property name="testConnectionOnCheckout" value="true"/>

To add a custom function to Mule expressions you would define them in the global functions tag. The example below shows how you might incorporate a Base 64 decoder.

<configuration doc:name="Configuration">
	  <global-functions file="other_functions.mvel">
		def decode(value) {
			return java.util.Base64.getDecoder().decode(value);

Having defined the decode function we can now use it in our flows like this:

<set-variable variableName="CLAIM_DECODED" value="#[new String(decode(flowVars.JWT_CLAIMS))]" doc:name="Decode"/> 

In the snippet above, we show how the decode statement invokes our custom expression which converts the Base 64 encoded field to String.

Leave a comment

Your email address will not be published. Required fields are marked *