How to write an Interceptor?
Interceptors are simple POJO's so how complex would writing an interceptor be ? All that the interceptor developer needs to do is write a simple Java class which has the interceptor method.
- Is annotated with the Intercept Annotation
- Has a single parameter. Based on the parameter type the method intercepts : Messages, Message Exchanges or Message Payloads. For instance if the parameter type is javax.jbi.messaging.MessageExchange then the method intercepts the exchange if the type is javax.jbi.messaging.NormalizedMessage the method intercepts a Message. If the parameter type does not match any of the message exchange content, then the interceptor method will not be invoked.
- Interceptor method details :
- The interceptor method can throw a java.lang.Exception, in which case the MessageExchange is flagged as an error and sent back to the sender of the exchange.
- There can be more than one interceptor method in a java class.
- A single class can implement more than one method to intercept messages and message exchanges.
- If a method does not have the @Intercept annotation it is not considered to be an interceptor method.
- An interceptor method may modify the parameter being passed in
- Should have "public" accessibility
- If the method intercepts the Message Exchange and the exchange is to be "shorted" i.e. sent back to the sender then the method should return a boolean value of "false". The Fuji runtime shorts the exchange and sends it back to the sender.
Example Interceptor methods
By varying the parameter type of the method, it can intercept Message Exchanges or Messages
Message Exchange Interceptor
A simple message exchange interceptor that displays the exchange contents.
@Intercept
public void myExchangeInterceptor(MessageExchange exchange){
// -- Print the message exchange content
System.out.println(exchange.toString());
}
Shorting Message Exchange Interceptor
A message exchange interceptor which sets the exchange status to DONE and shorts it
@Intercept
public boolean anotherExchangeInterceptor(MessageExchange exchange){
// -- Print the message exchange content
exchange.setStatus(ExchangeStatus.DONE);
return false;
}
Note that any updates made to the Message Exchange must be within the limits of the Message Exchange Pattern of the exchange. For instance if an exchange interceptor intercepts an InOut exchange on the "in" message path and sets the status as DONE, the runtime will throw an exception indicating that this is an illegal operation.
Message Interceptor
A simple message interceptor where we set a new message property.
@Intercept
public boolean aMessageInterceptor(NormalizedMessage message){
if ( message != null ) {
msg.setProperty("Fuji", "Rocks!");
}
}
This page (revision-8) was last changed on
30-Jul-09 14:33 PM, -0700
by MarkWhite.
This page was created on
16-Sep-08 15:50 PM, -0700 by JBIWIKI.
More info...