Versions Compared

Key

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

...

Code Block
/**
 * a processor used to validate the message
 */
public class ValidatingProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        ObjectString valuebody = exchange.getIn().getHeader("expiredDategetBody().toString();
        String[] tmp = body.split("<expiredDate>|</expiredDate>");
        if (tmp.length < 3) {
            throw new ValidationException(exchange, "The message has no expired date!");
        }
        
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date expiredDate = sdf.parse((String)value)tmp[1]);
        if (new Date().after(expiredDate)) {
            throw new ValidationException(exchange, "The message has been expired!");
        }
    }
}

...

  • 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!");
            }
        }
    }
    // 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 we want to show you how to do it through Web Console.

...

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 specified in the message, so the validating processor can filter the expired messages.

Source code