向camel发送消息及消息处理过程跟踪

编程技术  /  houtizong 发布于 3年前   69
package hello; import java.util.HashMap;import java.util.List;import java.util.Map; import org.apache.camel.CamelContext;import org.apache.camel.Endpoint;import org.apache.camel.Exchange;import org.apache.camel.Message;import org.apache.camel.Processor;import org.apache.camel.ProducerTemplate;import org.apache.camel.Route;import org.apache.camel.builder.RouteBuilder;import org.apache.camel.impl.DefaultCamelContext;import org.apache.camel.processor.interceptor.Tracer;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration; @Configuration@ComponentScanpublic class Application { @BeanMessageService mockMessageService() {return new MessageService() {public String getMessage() {return "Hello World!";}};} private static class ConsoleLog implements Processor { public void process(Exchange exchange) throws Exception { Message in = exchange.getIn();Map<String, Object> headers = in.getHeaders();for(String k:headers.keySet())System.out.println(k+":"+headers.get(k)); } public String toString() {return "ConsoleLog";} } public static void main(String[] args) throws Exception {ApplicationContext context = new AnnotationConfigApplicationContext(Application.class); MessagePrinter printer = context.getBean(MessagePrinter.class);printer.printMessage();Tracer tracer = new Tracer();CamelContext cc = new DefaultCamelContext(); cc.addRoutes(new RouteBuilder() {public void configure() {from("file://d:/home/in?noop=true").to("file:d:/home/out");}}); cc.addRoutes(new RouteBuilder() {public void configure() {from("direct:bar").to("log:org.springframework?level=ERROR&showHeaders=true").process(new ConsoleLog());}}); cc.addInterceptStrategy(tracer);cc.start();cc.setTracing(true);Thread.sleep(1000); ProducerTemplate template = cc.createProducerTemplate(); HashMap<String, Object> map = new HashMap<String, Object>();template.setDefaultEndpointUri("direct:bar");map.put("sender", "1234");map.put("receiver", "4321");map.put("messageType", "order");Endpoint ep = cc.getEndpoint("direct:bar"); for (int i = 0; i < 5; i++) { template.sendBodyAndHeader(ep,""+Math.random(), map);} List<Route> routes = cc.getRoutes();for (Route r : routes) { System.out.println(r.getId());} Thread.sleep(100000);cc.stop(); }}

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客