In Mule 4 by default, the runtime engine uses the ordinary Java serialization. However, you can configure to use defaultObjectSerializer
in your Mule application which would specicy serialization mechanism, such as the Kryo serializer or any other custom serializer.
Using a custom serializer can improve functionality and performance when Mule executes any of the following processes:
- Read from or write to a persistent object store
- Read from or write to a persistent VM or JMS queue
- Distribute an object through a Mule cluster
- Read from or write to an object from a file
Customization does not affect the behavior of the Batch module, which always uses the Kryo serializer. Below is the Example of how to use a custom Serializer –
<?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:kryo="http://www.mulesoft.org/schema/mule/kryo"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/kryo http://www.mulesoft.org/schema/mule/kryo/current/mule-kryo.xsd">
<kryo:serializer name="kryo" />
<configuration defaultObjectSerializer-ref="kryo" />
</mule>
Configure a Custom Serializer
Configure the default ObjectSerializer
for your Mule application using the Mule <configuration>
tag:
<?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:spring="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd">
<spring:config name="Spring_Config" files="serializer.xml" />
<configuration defaultObjectSerializer-ref="customSerializer" />
</mule>
Then you have to configure the Bean in the object Serializer.xml file –
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="customSerializer" name="customSerializer"
class="com.my.CustomSerializer">
<!-- ... -->
</bean>
</beans>