Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In Load Balance for existing Messaging Service sample, route2 uses the Content Based Router. This sample will guide you to creating your processor, registering and using it in Web Console.

Build the sample

You can build a sample as that in Load Balance for existing Messaging Service.

Create a processor

Now we create a validating processor which will check whether the message has been expired. If expired, the processor will throw a validation exception. The processor can be defined as follows:

Code Block

/**
 * a processor used to validate the message
 */
public class ValidatingProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        Object value = exchange.getIn().getHeader("expiredDate");
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date expiredDate = sdf.parse((String)value);
        if (new Date().after(expiredDate)) {
            throw new ValidationException(exchange, "The message has been expired!");
        }
    }
}
{code]
h3. Register the processor
We can register the processor in the spring context in two steps.
* add a component-scan package in the Applicatioin-Context.xml:
{code:xml}
<!-- configure the spring component scan package -->
<context:component-scan base-package="org.apache.camel.web.example"/>
  • annotate the processor by adding one line code in the processor. Now the processor is as follows:
    Code Block
    
    package org.apache.camel.web.example;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    import org.apache.camel.ValidationException;
    import org.springframework.stereotype.Service;
    
    /**
     * a processor used to validate the message
     */
    @Service(value = "validatingProcessor")
    public class ValidatingProcessor implements Processor {
        public void process(Exchange exchange) throws Exception {
            Object value = exchange.getIn().getHeader("expiredDate");
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
            Date expiredDate = sdf.parse((String)value);
            if (new Date().after(expiredDate)) {
                throw new ValidationException(exchange, "The message has been expired!");
            }
        }
    }
    

    Use Content Based Router to orchestrate the processors

Source code