You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by henricao <he...@outlook.com> on 2016/05/19 15:26:31 UTC

Manually trigger a scheduled route for testing

Hi all,

  I have a route that reads files from a directory and ftp them in a server,
something like this:

  from("file: ...").to("sftp: ...");

  In the file component I defined a schedule for polling the directory every
hour.

  In my tests, I would like to trigger manually this route, ignoring the
scheduler, to make sure that it is working fine.

  Is there a recommended way to do that?

Thanks all,
Henrique



--
View this message in context: http://camel.465427.n5.nabble.com/Manually-trigger-a-scheduled-route-for-testing-tp5782791.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: Manually trigger a scheduled route for testing

Posted by Hans Orbaan <ha...@docdata.eu>.
HI,

Using a direct and not a quartz schedule just for testing is your best option.
You could however use this to kick of a route that uses a quartz schedule: ftp://bla?cron.scheduler=quartz2
I could however not configure a triggerId on a quartz2:// route (did also not look too thoroughly I must admit), so that might not work, and I do not know if it works during testing.
But this is how you would kick off a scheduler:

{code}

import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

    private String trigger(Exchange exchange, String triggerId) {
        try {
            TriggerKey triggerKey = new TriggerKey(triggerId, "QuartzScheduledPollConsumerScheduler");
            Trigger trigger = this.getTrigger(exchange, triggerKey);
            JobKey jobKey = trigger.getJobKey();
            this.scheduler.triggerJob(jobKey);
            LOGGER.warn("Manually executed trigger: " + triggerId);
        } catch (Exception e) {
            LOGGER.error("Could not manually executed trigger: " + triggerId);
            return "FAIL";
        }
        return "OK";
    }

    private Trigger getTrigger(Exchange exchange, TriggerKey triggerKey) throws Exception {
        try {
            Trigger result = this.getScheduler(exchange).getTrigger(triggerKey);
            if (null != result) {
                return result;
            }
        } catch (Exception e) {
            throw new ConfigurationException("Could not find trigger: " + triggerKey.getName(), e);
        }
        throw new ConfigurationException("Could not find trigger: " + triggerKey.getName());
    }

    private Scheduler getScheduler(Exchange exchange) {
        if (null == this.scheduler) {
            QuartzComponent quartz = exchange.getContext().getComponent("quartz2", QuartzComponent.class);
            this.scheduler = quartz.getScheduler();
        }
        return this.scheduler;
    }
{code}



Met vriendelijke groet,
Docdata Commerce B.V.
Hans Orbaan
Developer
Docdata Commerce
Energieweg 2
5145 NW Waalwijk
t
+31 (0)416 - 674 169
m
+31 (0)6 - 469 543 23
 
 
e
hans.orbaan@docdata.eu
i
www.docdata.nl
Docdata Commerce B.V. is part of DOCDATA N.V., listed at the NYSE Euronext Amsterdam (ticker symbol DOCD).
All information contained within this e-mail, including any attachments, is confidential. If you have received this e-mail in error, please delete it immediately and remove the attachments from your system. Do not use, disclose or spread the information in any way and notify the sender immediately. Any views and opinions expressed in this e-mail may not represent those of Docdata.

-----Oorspronkelijk bericht-----
Van: henricao [mailto:henrique.limao@outlook.com] 
Verzonden: Friday 20 May 2016 12:11
Aan: users@camel.apache.org
Onderwerp: Re: Manually trigger a scheduled route for testing

Hi Claus,

  First of all, thanks for the response.

  I'm afraid the advice option won't work for me, since the original from part is part of the scope of my test.  And to clarify, it is not really a unit test, but more of a integration test.

  The property place holder however works for me and I started using a similar approach before your answer (concatenating the cron expression instead of a proper placeholder, but the same principle).

  Ideally, my idea was to have something like this:

  from("direct: realroute").from("fille...").to("sftp:...") 

  from("quartz2:...").to("direct: realroute") //the real scheduled route used in production

  from("http:...").to("direct: realroute") // a http entrypoint that you could trigger from the test or manually runs

  I googled about this and couldn't find any evidence that this could work.
It seems that: 
  - the file component has a internal scheduler than can be disabled
  - I can't have a route with two from parts (like the real route above)

  Are these assumptions correct?

  I will use the property placeholder solution if I can't implement this idea, but I can see this last approach providing some extra benefits that I really would like to have.

Thanks again for the support,
Henrique

  



--
View this message in context: http://camel.465427.n5.nabble.com/Manually-trigger-a-scheduled-route-for-testing-tp5782791p5782822.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Manually trigger a scheduled route for testing

Posted by henricao <he...@outlook.com>.
Hi Claus,

  First of all, thanks for the response.

  I'm afraid the advice option won't work for me, since the original from
part is part of the scope of my test.  And to clarify, it is not really a
unit test, but more of a integration test.

  The property place holder however works for me and I started using a
similar approach before your answer (concatenating the cron expression
instead of a proper placeholder, but the same principle).

  Ideally, my idea was to have something like this:

  from("direct: realroute").from("fille...").to("sftp:...") 

  from("quartz2:...").to("direct: realroute") //the real scheduled route
used in production

  from("http:...").to("direct: realroute") // a http entrypoint that you
could trigger from the test or manually runs

  I googled about this and couldn't find any evidence that this could work.
It seems that: 
  - the file component has a internal scheduler than can be disabled
  - I can't have a route with two from parts (like the real route above)

  Are these assumptions correct?

  I will use the property placeholder solution if I can't implement this
idea, but I can see this last approach providing some extra benefits that I
really would like to have.

Thanks again for the support,
Henrique

  



--
View this message in context: http://camel.465427.n5.nabble.com/Manually-trigger-a-scheduled-route-for-testing-tp5782791p5782822.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Manually trigger a scheduled route for testing

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

You could use property placeholders and then in an unit test replace
the trigger to run faster. Or you can use advice with and then replace
the from uri with something what you want instead.

http://camel.apache.org/using-propertyplaceholder.html
http://camel.apache.org/advicewith.html


On Thu, May 19, 2016 at 5:26 PM, henricao <he...@outlook.com> wrote:
> Hi all,
>
>   I have a route that reads files from a directory and ftp them in a server,
> something like this:
>
>   from("file: ...").to("sftp: ...");
>
>   In the file component I defined a schedule for polling the directory every
> hour.
>
>   In my tests, I would like to trigger manually this route, ignoring the
> scheduler, to make sure that it is working fine.
>
>   Is there a recommended way to do that?
>
> Thanks all,
> Henrique
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Manually-trigger-a-scheduled-route-for-testing-tp5782791.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