Logging and Monitoring Exception
Logging and monitoring the exceptions which occur in the enterprise applications is one of the critical parts to the enterprise management.
Mule supports such exception strategies.
From our example, we will take a look that when the exception occurs, all the exception messages will be delivered into the exception service where the details of the exception is monitored and could be logged into the file system.
With the help of mule's exception strategies, logging and monitoring the exceptions in the enterprise applications can be accomplished easily, for which error channel can be used to transport exception messages.
However massive transactions for exception messages should be considered, in which case spaces can be used as error channel to deliver the exception messages.
The next example from mule-exception-strategy.zip is originated from the mule example hello world. For the intention of our example, some modification of it has been made.
Logging and Monitoring Exception Overview
- When the exception occurs in the individual application, the exception message is written to the queue with the address of 'jms://queue.exception'.
- Monitoring service will consume the exception messages from the exception queue and monitor the exception details.
- For message history, the exception messages could be saved into the file system via logging service.
Exception Strategy Configuration
In mule, to set the exception strategy for the service, just simple configuration in the spring xml remains.
Before stepping into the next, please download and unzip mule-exception-strategy.zip.
After unzipping it, open <mule-exception-strategy>/src/main/resources/META-INF/spring/spring-service-context.xml.
The following shows the configuration for the exception strategy:
... <mule:service name="GreeterUMO"> ... <mule:outbound> <mule:filtering-router> <vm:outbound-endpoint path="chitchatter" /> <mule:payload-type-filter expectedType="com.ktf.khub.samples.mule.hello.space.domain.NameString" /> </mule:filtering-router> <mule:filtering-router> <vm:outbound-endpoint path="userErrorHandler" /> <mule:payload-type-filter expectedType="java.lang.Exception" /> </mule:filtering-router> </mule:outbound> <mule:default-service-exception-strategy> <vm:outbound-endpoint path="systemErrorHandler" /> </mule:default-service-exception-strategy> </mule:service> ... <mule:service name="UserErrorHandler"> <mule:inbound> <vm:inbound-endpoint path="userErrorHandler" synchronous="true" /> </mule:inbound> <mule:outbound> <mule:outbound-pass-through-router> <mule:outbound-endpoint ref="exceptionQueue" transformer-refs="ObjectToJMSMessage" /> </mule:outbound-pass-through-router> </mule:outbound> </mule:service> <mule:service name="SystemErrorHandler"> <mule:inbound> <vm:inbound-endpoint path="systemErrorHandler" synchronous="true" /> </mule:inbound> <mule:outbound> <mule:outbound-pass-through-router> <mule:outbound-endpoint ref="exceptionQueue" transformer-refs="ObjectToJMSMessage" /> </mule:outbound-pass-through-router> </mule:outbound> </mule:service> <mule:service name="exceptionService"> <mule:inbound> <mule:inbound-endpoint ref="exceptionQueue" transformer-refs="JMSMessageToObject" /> </mule:inbound> <mule:component class="com.ktf.khub.samples.mule.hello.space.service.ExceptionService" /> </mule:service> ...
In the above mule model, there are some services shown. In service GreeterUMO, default service exception strategy is set.
In outbound of it, when the message payload type is java.lang.Exception, then the message is forwarded to userErrorHandler via vm connector.
In service UserErrorHandler, the inbound has a vm inbound endpoint with the path of userErrorHandler, and the incoming exception message is transformed to the JMS message which will be written into the queue exceptionQueue.
From the service exceptionService, the exception message wrapped with JMS message will be consumed and the details of it will be monitored.
Exception Service
In our example, the exception service is just a POJO.
Let's see some codes of it:
package com.ktf.khub.samples.mule.hello.space.service; import org.mule.message.ExceptionMessage; public class ExceptionService { public void onException(ExceptionMessage e) { System.err.println("EXCEPTION MESSAGE:::: " + e.getException().getMessage()); } }
As shown above, ExceptionService does just print the exception message from error queue.
But we could also extend the exception service to the real time monitoring service or to the logging service with the further processing of exception messages.
AppRunner
To achieve our goal, AppRunner below does send the correct message first, and second it sends the incorrect message type to throw the exception in the service:
package com.ktf.khub.samples.mule.hello.space.service; ... import java.util.Iterator; public class AppRunner { private final static String[] SPRING_XML = { "META-INF/spring/spring-service-context.xml", "META-INF/spring/spring-space-context.xml", "META-INF/spring/spring-tx-context.xml" }; public static void main(String[] args) throws Exception { MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); MuleContext context = muleContextFactory .createMuleContext(new SpringXmlConfigurationBuilder( SPRING_XML)); context.start(); MuleClient client = new MuleClient(); MuleMessage response = client.send("vm://greeter", "Kidong", null); System.out.println("response = " + response.getPayload()); System.out.println("try to throw exception..."); // DO LET EXCEPTION OCCUR! MuleMessage response2 = client.send("vm://greeter", "Kidong".getBytes(), null); System.out.println("error = " + response2.getPayload()); } }
As we will see the details of exception message thrown from the service, the message type should be String, not byte[] of which the second message is.
Required Softwares
The following softwares are required:
Run example
Make sure that all the required libraries of Mule, GigaSpaces, and Spring are copied into the individual directory of <mule-exception-strategy>/lib.
Open <mule-exception-strategy>/bin/set-env.bat, modify the following lines:
rem ===================================================
rem set the GigaSpaces home.
rem ===================================================
@set JSHOMEDIR="D:\project\action-khub-samples\apps\3rd-parties\giga-spaces\GigaSpacesXAP6.5M8"
set the correct GigaSpaces installation directory.
To run the example, the next steps is to follow:
- change the directory to <mule-exception-strategy> in the command and type 'ant' to build the example.
- change to <mule-exception-strategy>/bin and run start-part-*.bat to start the partitioned spaces.
- run start-jini-tx.bat to start the jini transaction manager.
- change the directory <mule-exception-strategy> in the command and type 'ant run-app' in the command.
And then in the console, you will see the similar message like this:
...
[java] response = Hello Kidong, how are you?
[java] try to throw exception...
[java] EXCEPTION MESSAGE:::: Component that caused exception is: GreeterUMO. Message payload is of type: byte[]
[java] error = {NullPayload}
...
From the exception service, the detailed exception message is printed like Component that caused exception is: GreeterUMO. Message payload is of type: byte[].
Conclusion
We have seen that with some simple setting of mule exception strategy, the exception messages can be caught.
The exception message can be also delivered via space error channel to the exception service which could be extended to logging and monitoring service.
Attachment
- Example Source Codes: mule-exception-strategy.zip





