apache camel_Apache Camel简介

apache camel

apache camel

Apache Camel是著名的企业集成模式的开源实现 Camel是一个路由和中介引擎,可帮助开发人员以各种领域特定语言(DSL)(例如Java,Spring / XML,scala等)创建路由和中介规则。

骆驼用途广泛

Camel使用URI来支持大量的传输和消息传递模型,例如HTTP,JMS,JBI,Mina,SCA,CXF,它还可以与外部组件和数据格式很好地兼容。 要了解Camel的多功能性,您可以在下面的链接中浏览其支持的组件和URI列表。 http://camel.apache.org/components.html

骆驼易于使用

骆驼允许我们使用同一组API来创建路由并在各个组件之间传递消息。 这使得它非常容易使用

单元测试骆驼轻而易举

单元测试对于编写任何质量代码至关重要。 骆驼使软件开发的基础变得非常容易。 它提供了许多现成的组件,例如CamelContextSupport,camel-guice,camel-test-blueprint,可轻松测试代码。 在以后的文章中会更多。

骆驼的术语/类/接口

端点是交换消息的地方。 它可以引用地址,POJO,电子邮件地址,Web服务uri,队列uri,文件等。在骆驼中,端点是由实现的端点接口实现的。 端点被称为路由的东西包装。

CamelContext是所有骆驼应用程序的核心,它代表了Camel运行时系统。

  1. 创建camelcontext。
  2. 添加端点或组件。
  3. 添加路由以连接端点。
  4. 调用camelcontext.start()–这将启动所有负责在端点中接收,发送和处理消息的骆驼内部线程。
  5. 最后,在交换和处理所有消息时调用camelcontext.stop()。 这将优雅地停止所有骆驼内部线程和端点。

这是围绕CamelContext对象的薄包装,它负责将交换或消息发送到端点。

组件实际上是一个端点工厂。 由于骆驼支持许多不同种类的资源,因此这些资源中的每一个都有不同种类的端点。 在实际情况下,应用程序不要直接使用组件创建端点。 相反,CamelContext决定实例化哪个组件,然后使用该组件实例创建端点。 因此,在应用程序中,我们将拥有。 CamelContext.getEndpoint(“ pop3://john.smith@mailserv.example.com?password = myPassword”); 现在,在这种情况下,pop3是组件的名称。 CamelContext将所有组件名称与组件类映射,并使用其实例化实例的名称。 一旦拥有了组件的句柄,它就会通过调用实例化端点。 Component.createInstance()方法。

消息表示单个具体消息,即请求,答复或异常。 所有具体的消息类都会影响消息接口,例如JmsMessage类。

交换是消息的容器。 当消费者在路由过程中收到消息时创建。

处理器接口表示处理消息的类。 它包含单个方法public void process(Exchange交换)引发异常。应用程序开发人员可以实现此接口,以便在消费者收到消息时对消息执行业务逻辑。

路由是通过过滤器或路由器通过任意类型的决策,将消息从源一步一步地移动到目的地。 它们通过DSL(特定于域的语言)的帮助进行配置。 Java DSL是通过实现routebuilder接口创建的。 它具有称为configure()的单个方法,该方法定义了消息的整个路由。 也可以使用spring通过xml文件配置路由。

骆驼代码的一个小例子。

让我们以一个小例子来说明一下Camel可以做什么。 在此示例中,我们将文件夹中存在的文件组移动到另一个文件夹。 在此过程中,我们将执行以下操作

  1. 签出Camel的依赖项。
  2. 创建一个简单的RouterBuilder。
  3. 在spring文件中注册CamelContext。
  4. 在CamelContext Bean中注入routerbuilder
  5. 通过启动Camelcontext来执行该类,并在执行完成后最终将其停止。

1.依赖关系–在pom.xml中添加以下依赖关系

<dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel-version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>${camel-version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-aws</artifactId>
            <version>${camel-version}</version>
        </dependency>

2.创建RouterBuilder –可以通过扩展org.apache.camel.builder.RouterBuilder类并覆盖configure()方法来创建RouterBuilder。 这是一个例子

import org.apache.camel.builder.RouteBuilder;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 7/28/13
 * Time: 10:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class MyFirstRouterBuilder extends RouteBuilder {
     @Override
    public void configure() throws Exception {
        try{
            from( "file:d:/vids").to("file:d:/temp");
        }catch(Exception e){

        }
     }
}
  1. From()是源端点,包含骆驼将要轮询的文件或目录的uri。
  2. to()代表目标端点,并包含目标文件或目录的名称。
  3. 文件组件uri的格式为“ file:// nameOfFileOrDirectory ”。

3.在Spring注册CamelContext并在Spring注入RouterBuilder。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <camelContext id="sqsContext" xmlns="http://camel.apache.org/schema/spring">
         <routeBuilder ref="myFirstRouter" />
    </camelContext>

    <bean id="myFirstRouter" class="com.aranin.aws.sqs.MyFirstRouterBuilder"/>

</beans>

4.启动骆驼上下文并执行代码,然后停止骆驼上下文。

import org.apache.camel.CamelContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 4/16/13
 * Time: 11:21 AM
 * To change this template use File | Settings | File Templates.
 */
public class CamelHello {
    public static void main(String args[]) throws Exception {
        try {
                ApplicationContext springcontext = new FileSystemXmlApplicationContext("D:/samayik/awsdemo/src/main/resources/hellocamel.xml");
                CamelContext context = springcontext.getBean("firstCamelContext", CamelContext.class);
                context.start();
                Thread.sleep(10000);
                context.stop();

            } catch ( Exception e ) {
                System.out.println(e);
            }

    }
}

如果您运行此类,则首先我们从spring配置文件中加载camelcontext。 在其中注入路由器生成器。 上下文启动后,然后将源目录中的所有文件复制到目标目录。 复制所有文件后,尝试将新文件复制到源目录,在这种情况下,上下文运行10000 ms之前,该文件也将复制到目标文件。

我没有更多关于骆驼的高级教程。 也许您会发现它们很有用。 参考部分列出了这些链接。

参考文献

  1. http://camel.apache.org/
  2. http://camel.apache.org/enterprise-integration-patterns.html
  3. http://architects.dzone.com/articles/enterprise-integration
  4. http://weblog4j.com/2013/05/14/amazon-sqs-listening-to-sqs-using-apache-camel-the-spring-dsl-way/
  5. http://weblog4j.com/2013/04/17/amazon-sqs-listening-to-amazon-sqs-queue-using-apache-came l /

就这些了。 尽管没有人会写评论,但是我还是要坚持不懈,如果您喜欢本教程,仍然请大家排一两行。

温暖的问候

尼拉吉

参考: Weblog4j博客上的JCG合作伙伴Niraj Singh对Apache Camel的介绍

翻译自: https://www.javacodegeeks.com/2013/08/introduction-to-apache-camel.html

apache camel

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值