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<String> 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<AQI24> 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<String> 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<String,Map<String,List<String>>> maps = FogWord.getAllMaps(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
							List<Fog> fs = null;
							if(name.contains("中午")){
							.....
							}
							if (fs!=null && !fs.isEmpty()) {
								process(fs);
							}
						});
			}
		});
		context.start();
		Thread.sleep(min*60*1000);
		context.stop();
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值