Hi,

I am getting component level exception , I wanted to capture that in the
route, but the exception occurs at the component level so i created custom
exception handler :

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.spi.ExceptionHandler;

public class CamelExceptionHandler implements ExceptionHandler {
         private ProducerTemplate template;

            /**
             * We use a producer template to send a message to the Camel route
             */
            public void setTemplate(ProducerTemplate template) {
                this.template = template;
            }

        
        
        @Override
        public void handleException(Throwable exception) {
                // TODO Auto-generated method stub
                 handleException(exception.getMessage(), exception);

        }

        @Override
        public void handleException(String message, Throwable exception) {
                handleException(exception.getMessage(), null, exception);

        }

        @Override
        public void handleException(final String message, final Exchange
originalExchange, final Throwable exception) {
                 // send the message to the special direct:file-error endpoint, 
which will
trigger exception handling
           template.send("seda:GenericFileError", new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                // set an exception on the message from the start so the
error handling is triggered
                exchange.setException(exception);
                exchange.getIn().setBody(message);
            }
        });

        }

}


my route which uses that exception handler is :

/**
 * 
 */
package gov.ma.itd.ic.route;

import gov.ma.itd.ic.processor.CommandResponseProcessor;
import gov.ma.itd.ic.processor.ErrorHandlerProcessor;
import gov.ma.itd.ic.processor.GenericFileErrorProcessor;
import gov.ma.itd.ic.processor.GenericFileInProcessor;
import gov.ma.itd.ic.processor.MonitorProcessor;
import gov.ma.itd.ic.processor.OutProcessor;
import gov.ma.itd.ic.processor.PickupFileProcessor;
import gov.ma.itd.ic.processor.ServiceProcessor;
import gov.ma.itd.ic.utils.Constants;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.OtherwiseDefinition;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Route for Generic File handling.
 * @author rjmathew
 *
 */
public class GenericFileRouteBuilder extends RouteBuilder {
        /**
         * Redelivery attempt count.
         */
        //private static final int REDELIVERY_COUNT = 4 ;
        
        /**
         * Redelivery delay in ms
         */
        //private static final long REDELIVERY_DELAY = 3000 ;
        
        /**
         * Configure the Camel Route.
         */
        public void configure() throws Exception {
                
                String genericFileSupport = 
ICEndPoints.getValue(GENERIC_FILE_SUPPORT);
                boolean isGenericFileSupport = 
Boolean.parseBoolean(genericFileSupport);
                String retryCount = 
resourceBundle.getString(Constants.RETRY_COUNT);
                int  redeliveryCount =  Integer.valueOf(retryCount).intValue() 
;                
                String retryDelay = 
resourceBundle.getString(Constants.RETRY_DELAY);
                String retryStack = 
resourceBundle.getString(Constants.STACKTRACE);
                boolean stackValue = Boolean.parseBoolean(retryStack);
                LOG.info("logRetryStackTrace is : " + stackValue);
                int redeliveryDelay = Integer.valueOf(retryDelay).intValue() ;
                
                 String pollingFrequency = 
ICEndPoints.getValue(Constants.POLL_FREQUENCY);
                 String pollDelayParameter = POLL_DELAY + "=" + 
pollingFrequency;
                 
                
                // Route starts if the generic file support is turned ON
                if (isGenericFileSupport) {
                
                        try {
                                
        PickupFileProcessor pickupProc = new PickupFileProcessor();             
                boolean
isGenericRoot = ICEndPoints.checkIfExists(Constants.GENERIC_ROOT_FOLDER);
                                // Main route starts here
                                if(isGenericRoot)
                                {
                                String sourceFolder = getGenericSourceURI() ;
                                from(sourceFolder)      
                                
                                        // the exception clause here
                                        .onException(Exception.class)   
                                        .maximumRedeliveries(redeliveryCount)
                                        .redeliveryDelay(redeliveryDelay)
                                        .logHandled(true)
                                    .logRetryStackTrace(stackValue)
                                    .retryAttemptedLogLevel(LoggingLevel.WARN)
                                    
.retriesExhaustedLogLevel(LoggingLevel.ERROR)
                                    .backOffMultiplier(5.0)
                                        .handled(true)
                                        .to(seda:GenericFileError)
                                        .end()
                                    
                                        .process(pickupProc)
                                        // main route continues and ends here 
                                        .process(new GenericFileInProcessor())
                                        
.recipientList(header(Constants.DESTINATION_URI)) 
                                        .process(new 
CommandResponseProcessor())// try using xpath here also
                                        
                                        .choice()
                   
.when(header(Constants.TRANSFER_TYPE).isEqualTo(Constants.SFTP))
                               .to(Constants.SFTP_TRANSFER_URI) .end()
                                        
                                    .process(new ServiceProcessor()) ;
                                
                                
                                // exception path continues and ends here
                                from(seda:GenericFileError)
                                        .process(new 
GenericFileErrorProcessor())
                                        .process(new ServiceProcessor());
                                        /*
                                        .filter().method("monitorBean", 
"getMonitorValue")
                                    .process(new MonitorProcessor()).end();
                                    */
                                }
                                else{
                                        LOG.error("If the generic file handling 
support is turned ON, the
generic.path.root should be given a valid directory path");
                                }
                                
                        } catch (MissingResourceException mr) {
                                LOG.error("If the generic file handling support 
is turned ON, the
generic.file.root should be specified.");
                        }
                }
        }
        
        /**
         * Get the Generic Folder Source URI
         * @return - source file URI - corresponding to the root generic file
folder.
         */
        private String getGenericSourceURI() {
                
                String genericRootPath =
resourceBundle.getString(Constants.GENERIC_ROOT_FOLDER);
                LOG.info("The generic file root is " + genericRootPath);
                
                String pollingFrequency = 
ICEndPoints.getValue(Constants.POLL_FREQUENCY);
                String pollDelayParameter = POLL_DELAY + "=" + pollingFrequency;
                
                StringBuffer str = new StringBuffer();
                str.append("file://").append(genericRootPath) ;
                str.append("?") .append(Constants.RECURSIVE_TRUE) ;
                str.append("&").append(pollDelayParameter) ;
                str.append("&").append(Constants.DELETE_TRUE) ;
                str.append("&").append(Constants.READLOCK_CHANGED) ;
            str.append("&").append(Constants.RDLOCK_TIMEOUT_MS) ;
                str.append("&").append(Constants.AUTOCREATE_FALSE);
        
str.append("&").append("consumer.exceptionHandler=#camelExceptionHandler");
                return str.toString() ;
                
        }
        
                
}


my sourceURI has ("consumer.exceptionHandler=#camelExceptionHandler") , I am
still not able to catch the component level exception.
Iam using camel 2.4
Can you please help me about this issue.

Thanks,
Ckesired



--
View this message in context: 
http://camel.465427.n5.nabble.com/camel-exception-handler-tp5723025.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to