Apache Camel用法1

import org.apache.camel.CamelContext;
import org.apache.camel.Predicate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.processor.idempotent.FileIdempotentRepository;
import org.apache.camel.spi.IdempotentRepository;

public void execute(String f,String to,long min) throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				IdempotentRepository ir = FileIdempotentRepository.fileIdempotentRepository(new File("log/aqi24.dat"), 250, 512000);
				onException(GenericFileOperationFailedException.class).to(
						"file://errors/GenericFileExceptions");
				Predicate imgPredicate = header("CamelFileName").regex(
						"([\\S]+(\\.(?i)(jpg|png|gif))$)");
				from(f)
						.filter(header("CamelFileName").startsWith("空气质量预报"))
						.filter(header("CamelFileName").endsWith(".txt"))
						.convertBodyTo(String.class,"GBK")
						.idempotentConsumer(header("CamelFileName"), ir)
						.choice()
						.when(imgPredicate)
						.process(exchange->{
							String str = exchange.getIn().getBody(String.class);
							String name = exchange.getIn().getHeader("CamelFileName").toString();
							String year = name.substring(name.lastIndexOf(".")-10,name.lastIndexOf("."));
							long time = DateUtils.getTime(year, DateUtils.HOUR_FORMAT);
//							List ls = parseAQI24(str,time);
							log.info("AQIForecast24Process-name:"+name);
//							process(ls);
						})
				.to(to);
			}
		});
		context.start();
		Thread.sleep(min*60*1000);
		context.stop();
	}

解析word(含表格)

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.processor.idempotent.FileIdempotentRepository;
import org.apache.camel.spi.IdempotentRepository;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public void execute(String f,String to,long min) throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				IdempotentRepository ir = FileIdempotentRepository.fileIdempotentRepository(new File("log/fog.dat"), 250, 512000);
				from(f)
						.filter(header("CamelFileName").contains("霾预报"))
						.filter(header("CamelFileName").startsWith("20"))
						.idempotentConsumer(header("CamelFileName"), ir)
						.process(exchange->{
							String name = exchange.getIn().getHeader("CamelFileName").toString();
							log.info("FogProcess-name="+name);
							ByteArrayOutputStream byteArrayOutputStream = exchange.getIn().getBody(ByteArrayOutputStream.class);
							Map>> maps = FogWord.getAllMaps(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
							List fs = null;
							if(name.contains("中午")){
							.....
							}
							if (fs!=null && !fs.isEmpty()) {
								process(fs);
							}
						});
			}
		});
		context.start();
		Thread.sleep(min*60*1000);
		context.stop();
	}

你可能感兴趣的:(Camel)