启动文件路由 package com.file.ProcessWithCamel;import org.apache.camel.CamelContext;import org.apache.camel.Processor;import org.apache.camel.builder.RouteBuilder;import org.apache.camel.impl.DefaultCamelContext;public class ProcessFi
package com.file.ProcessWithCamel; import org.apache.camel.CamelContext; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; public class ProcessFileWithCamel { public static void main(String[] args) { // TODO Auto-generated method stub CamelContext context = new DefaultCamelContext(); final String sfrom = "file:/run/media/root/81e2b3a4-2d4f-48e8-8737-dbf1c7a5c94c1/JavaCode/test_in"; final String sto = "file:/run/media/root/81e2b3a4-2d4f-48e8-8737-dbf1c7a5c94c1/JavaCode/test_out"; final FIleConvertProcessor pro = new FIleConvertProcessor(); try { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { // TODO Auto-generated method stub from(sfrom).process(pro).to(sto); } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { context.start(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean loop = true; while(loop){ try { Thread.sleep(25000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { context.stop(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }制定操作流程
package com.file.ProcessWithCamel; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.camel.Exchange; import org.apache.camel.Processor; public class FIleConvertProcessor implements Processor { public void process(Exchange exchange){ try{ InputStream body = exchange.getIn().getBody(InputStream.class); System.out.println(body); BufferedReader in = new BufferedReader(new InputStreamReader(body)); StringBuffer strbf = new StringBuffer(""); String str = null; str = in.readLine(); while(str != null){ System.out.println(str); strbf.append(str + ""); str = in.readLine(); } exchange.getOut().setHeader(Exchange.FILE_NAME, "convert.txt"); exchange.getOut().setBody(strbf.toString()); }catch(IOException e){ e.printStackTrace(); } } }