camel开源框架简介

camel是实现企业应用集成的开源免费框架,网址:

http://camel.apache.org/

用camel可以很容易实现自动的消息路由和消息转换。基于camel可以将原来复杂和需求经常变化的应用,分解为多个消息生产者和消息消费者程序,这样每个程序更小,更容易设计和部署,需求的改动也不影响其他程序。

 

以下是一个最简单的示例。

这个示例可以特别简单。需要的准备依赖的类库(包括activemq),用maven的配置如下:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jms</artifactId>
    <version>1.6.0</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>5.2.0</version>
</dependency>

安装基于web管理的activemq,参见tomcat下部署activemq

编写代码:

 
 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.jms.JmsComponent; import org.apache.camel.impl.DefaultCamelContext; public class Helloworld { public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes( new RouteBuilder() { public void configure() { from( " jms:test.a " ).to( " file://e:/jms.test " ); } }); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( " tcp://localhost:61616 " ); context.addComponent( " jms " , JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); context.start(); } }

这样只要在test.a队列中发送消息,就会被此程序(守护线程)复制该消息到e盘的jms.test目录。因为注册转换器,所以会有警告:

2009-7-18 12:06:10 org.apache.camel.impl.converter.DefaultTypeConverter addTypeConverter
警告: Overriding type converter from: InstanceMethodTypeConverter: public java.lang.String org.apache.camel.converter.jaxp.XmlConverter.toString(javax.xml.transform.Source) throws javax.xml.transform.TransformerException to: StaticMethodTypeConverter: public static java.lang.String org.apache.camel.converter.IOConverter.toString(javax.xml.transform.Source) throws javax.xml.transform.TransformerException,java.io.IOException

如果想将该消息转发到另外的队列,并作一些处理,可以这样:

 
 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory; 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.component.jms.JmsComponent; import org.apache.camel.impl.DefaultCamelContext; public class Helloworld { public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes( new RouteBuilder() { public void configure() { from( " jms:test.a " ).to( " jms:test.b " ); from( " jms:test.b " ).process( new Processor() { public void process(Exchange e) { System.out.println( " Received exchange: " + e.getIn()); } }); } }); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( " tcp://localhost:61616 " ); context.addComponent( " jms " , JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); context.start(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值