Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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

...

We can register the processor in the spring context in two steps.

  • add Add a component-scan package in the applicationContext.xml: . Spring will scan the package for service registry when starting.
    Code Block
    xml
    xml
    <!-- configure the spring component scan package -->
    <context:component-scan base-package="org.apache.camel.web.example"/>
    
  • annotate 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 {
        // ommision
    }
    

Now you can run the sample, but it won't do the validation because we haven't configured the route to pass through the processor. You can complete it by editing the applicationContext.xml before running it, but now here we want to show you how to do it through Web Console in groovy language.

Use Content Based Router to orchestrate the processors

...

Code Block
from("file:src/data?noop=true").convertBodyTo(java.lang.String.class)
.processRef("validatingProcessor").to("stream:out").to("activemq:personnel.records")

Now a expired date is can be specified in the message, so the validating processor can filter the expired messages. You can add message files with content:

Code Block
xml
xml

<person user="xueqiang">
  <expiredDate>9-1-2009</expiredDate>
  <firstName>Xueqiang</firstName>
  <lastName>Mi</lastName>
  <city>Shanghai</city>
</person>

When the the message is out of date, a validation exception is thrown. To make it a little elegant, you may use doTry...doCatch...doFinally to wrap it up and specify a queue to store the invalid messages.

...

Code Block

from("file:src/data?noop=true").convertBodyTo(java.lang.String.class)
.doTry().processRef("validatingProcessor").to("stream:out").to("activemq:personnel.records")
.doCatch(ValidationException.class).to("mock:invalid").end()

You can define custom exceptions before running, even in Web Console at runtime. However, We don't encourage to create new classes in Web Console since that involves non-treatable inner-classes for Groovy Renderer and you will find it hard to deal with through Web Console.

Simpify your test by using Web Console

To run a test, you can create a file holding the message above and throw it into the directory. However, Web Console can help to simplify your operations. Go to the endpoints page by directing your browser to: http://localhost:8080/endpoints and you can see the file://src/data?noop=true endpoint. Just open it and then select the Send to this endpoint link for inputing your message. Web Console will save your message as a file in the directory: src/data, and camel will process the message file then.