Hi,

I'm very new in apache camel.

I have integration route. 

It uses pollEnrich to poll from file, and than aggregation strategy. I want,
that, if file is not available, than FileNotFoundException would be thrown
and handled by camel context.

Here is the piece of the route:

                 onException(RuntimeException.class).process(new
Processor(){
                    @Override
                    public void process(Exchange exchange) throws Exception
{
                        System.out.println("Finally here");
                    }}).end()

....
.pollEnrich().constant(file(exportDirectory) +
"?noop=true&recursive=true&fileName=Download-" + reference  +
".zip&maxMessagesPerPoll=1&consumer.bridgeErrorHandler=true
&pollStrategy=#exportPollStrategy&consumer.exceptionHandler=#exportExceptionHandler")
                 
.aggregationStrategy(new GwarParamAggregationStrategy()).timeout(10000)
.split(new ZipSplitter()).streaming()
.process(new UnzipProcessor()).end()
.delay(10000).process(new MergePdfProcessor())

Here is code of my customized pollprocessor:

public class ExportPollingStrategy extends
LimitedPollingConsumerPollStrategy {
    private final AtomicInteger retryCounter = new AtomicInteger();
    private final Logger logger =
Logger.getLogger(ExportPollingStrategy.class);

    @Override
    public boolean begin(Consumer consumer, Endpoint endpoint) {
        retryCounter.incrementAndGet();
        FileEndpoint fileEndpoint = (FileEndpoint)endpoint;
        List<Exchange> list = fileEndpoint.getExchanges();
        boolean found = list.size() > 0;
        if (!found) {
            throw new RuntimeCamelException(new FileNotFoundException("File
" + fileEndpoint.getFileName()
                                                                      + "
couldn't be found in "
                                                                      +
fileEndpoint.getFile()));
        }
        return found;
    }

    @Override
    public void commit(Consumer consumer, Endpoint endpoint, int paramInt) {

    }

    @Override
    public boolean rollback(Consumer consumer, Endpoint endpoint, int
paramInt, Exception exception)
        throws Exception {
        if (retryCounter.intValue() > 3) {
            FileEndpoint fileEndpoint = (FileEndpoint)endpoint;
            Exchange exchange = fileEndpoint.createExchange();

            if (exception != null) {
                exchange.setException(exception);
            }
            onSuspend(consumer, endpoint);
        }
        return false;
    }
}

And here is code of exception handler

public class ExportDownloadExceptionHandler implements ExceptionHandler {
    private Logger logger =
Logger.getLogger(ExportDownloadExceptionHandler.class);
    private CamelContext context;

    public ExportDownloadExceptionHandler(CamelContext context) {
        this.context = context;
    }

    @Override
    public void handleException(Throwable ex) {
        handleException(ex.getMessage(), null, ex);
    }

    @Override
    public void handleException(final String message, Exchange exchange,
final Throwable ex) {
        //logger.error("Exception has occurred by export download " +
ex.getMessage());
        throw new RuntimeException("exception has occured");
    }

    @Override
    public void handleException(String message, Throwable ex) {
        handleException(message, null, ex);
    }
}

When I was debuggin, it would go in custom polling processor and in custom
exception handler, but than it would proceed to aggregation strategy, with 2
Exchanges. One existing exchange initialized, but exchange from polling is
null.

How can I prevent, that it wouldn't even make to aggregation, but proceed
straight to route exception handler?

Thank you,

Nadiia








--
View this message in context: 
http://camel.465427.n5.nabble.com/How-to-throw-FileNotFoundException-from-FileComponent-tp5786692.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to