FileCopierWithCamel



package camelinaction;

import java.util.Scanner;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

public class FileCopierWithCamel {
public static void main(String args[]) throws Exception {
// create CamelContext
CamelContext context = new DefaultCamelContext();
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
public void configure() {
final RouteDefinition inboxDef = from(
"file:data/inbox?noop=true").routeId("inboxRoute").noAutoStartup();
from("file:data/trigger").routeId("triggerRoute").process(
new Processor() {
@Override
public void process(Exchange exchange)
throws Exception {
System.out.println("begin trigger");
exchange.getContext().startRoute(
inboxDef.getId());
}
});
inboxDef.log("copy file: ${file:name}")
.to("file:data/outbox");
}

});

context.addRoutes(new RouteBuilder() {
public void configure() {
from("timer://monitor").routeId("monitor").process(
new Processor() {
@Override
public void process(Exchange exchange)
throws Exception {
System.out.println("key any to stop file route");
Scanner scan = new Scanner(System.in);
scan.next();

System.out.println("begin stop file route");
exchange.getContext().getShutdownStrategy()
.setTimeout(20);
exchange.getContext().getInflightRepository()
.remove(exchange);
exchange.getContext().stopRoute("inboxRoute");
System.out.println("file route stopped");
}
});
}
});

// start the route and let it do its work
context.start();
Thread.sleep(Integer.MAX_VALUE);
// Thread.sleep(10000);
// stop the CamelContext
// context.stop();
}
}





// String srcDir = "file:data/inbox?noop=true";
String srcDir = "ftp://zgq@localhost/?password=1234&disconnect=true&consumer.delay=99999999999"; //




final ProducerTemplate pt = context.createProducerTemplate();
Map<String, Object> map = new HashMap<String, Object>();
map.put("To", "[email protected]");
map.put("From", "[email protected]");
map.put("Subject", "Camel rocks " + new Date() );

String body = "Hello Claus.\nYes it does.\n\nRegards James.";
pt.sendBodyAndHeaders("smtp://[email protected]?password=xxxx", body, map);






/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package camelinaction;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

public class FileCopierWithCamel {
public static void main(String args[]) throws Exception {
// create CamelContext
CamelContext context = new DefaultCamelContext();
final ProducerTemplate pt = context.createProducerTemplate();
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
public void configure() {
onException(Exception.class).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("To", "[email protected]");
map.put("From", "[email protected]");
map.put("Subject", "Exception" + new Date() );

String body = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage();
pt.sendBodyAndHeaders("smtp://[email protected]?password=xxx", body, map);

}
});

String srcDir = "file:data/inbox?noop=true&idempotent=false&delay=99999"; //
// String srcDir = "ftp://zgq@localhost/?password=1234&disconnect=true&consumer.delay=99999999999"; //
String destDir = "ftp://zgq@localhost2/test?password=1234"; //
// String destDir = "file:data/outbox";
final RouteDefinition inboxDef = from(srcDir).routeId(
"inboxRoute").noAutoStartup(); //
from("file:data/trigger").routeId("triggerRoute").process(
new Processor() {
@Override
public void process(Exchange exchange)
throws Exception {
System.out.println("begin trigger");
exchange.getContext().startRoute(
inboxDef.getId());
}
});


inboxDef.log("copy file: ${file:name}").to(destDir)
// .setHeader("subject", constant("test mail"))
// .to("smtp://SMTP.126.com?password=xxxxx&username=hubeihube")
;
;
}

});

context.addRoutes(new RouteBuilder() {
public void configure() {
from("timer://monitor").routeId("monitor").process(
new Processor() {
@Override
public void process(Exchange exchange)
throws Exception {
System.out
.println("key any to stop file route");
Scanner scan = new Scanner(System.in);
scan.next();

System.out.println("begin stop file route");
exchange.getContext().getShutdownStrategy()
.setTimeout(20);
exchange.getContext().getInflightRepository()
.remove(exchange);
exchange.getContext().stopRoute("inboxRoute");
System.out.println("file route stopped");
}
});
}
});

// start the route and let it do its work
context.start();
Thread.sleep(Integer.MAX_VALUE);
// Thread.sleep(10000);
// stop the CamelContext
// context.stop();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章