You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by princessMiracle <na...@gmail.com> on 2016/08/25 00:09:55 UTC

How to throw FileNotFoundException from FileComponent

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.

Re: How to throw FileNotFoundException from FileComponent

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

The code seems too complicated.

The pollEnrich has a timeout option you can use to set to a value, and
if there is no file then the timeout is hit, and the message body is
empty. You can then check for that and throw an exception. The
pollEnrich allows also to use an AggregationStrategy where you can
merge the data, and so if the body is empty you can throw an exception
from there.



On Thu, Aug 25, 2016 at 2:09 AM, princessMiracle <na...@gmail.com> wrote:
> 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.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2