-
1. Re: How to read script from a file ?
davsclaus Oct 29, 2011 3:37 AM (in response to labo32_delaboe)Hi
This is currently not supported directly, something which we should possible add. I think this has been asked before in the Camel user list.
What you can do is to use the language component and provide the script as a header which is the file.
http://camel.apache.org/language
First set a header with the file name
.setHeader("CamelLanguageScript", new File("test.txt"))
And then call the language component
.to("language:groovy")
.to("log:xxx")
-
2. Re: How to read script from a file ?
labo32_delaboe Oct 31, 2011 3:08 PM (in response to davsclaus)Hi,
thanks for the response but
.setHeader("CamelLanguageScript", new File("test.txt"))
is not possible with my camel 2.8.2 route.
Code like
..... InputStream in = getContext().getApplicationContextClassLoader().getResourceAsStream("/test.txt"); from("quartz://TimerGroup/matchUpdateTimer?cron=0/5 * * * * ?") .setHeader("CamelLanguageScript",constant(convertStreamToString(in))) .to("language:groovy") .to("log:################### QUARTZ-log"); ...... ... public String convertStreamToString(InputStream is) { try { if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } else { return ""; } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; }
works fine but I think this is not the most elegant way.....
Can I use the file component after the quartz trigger and load the file which is located in my war file ?
Greetz
labo
-
3. Re: How to read script from a file ?
davsclaus Nov 1, 2011 3:59 AM (in response to labo32_delaboe)Ah the file is in the WAR file?
Then you need to load it from the classpath. Which is a bit more tricky. The language component in Camel doesn't support this, but we could add support for that.
I will create a ticket.
There is a API on CamelContext to load resources from the classpath, eg ClassResolver on CamelContext.
-
4. Re: How to read script from a file ?
davsclaus Nov 1, 2011 4:34 AM (in response to davsclaus)The ticket is http://fusesource.com/issues/browse/MR-542
-
5. Re: How to read script from a file ?
labo32_delaboe Nov 1, 2011 7:50 AM (in response to davsclaus)That's the way I do it at the moment.
Thanks a lot
labo