哪有做网站推广,出口电商平台,企业网站建设要多少钱,永嘉县住房和城乡建设局网站开始之前 前一段时间#xff0c;我写了一篇关于Spring Integration的教程#xff0c;以演示如何在受现实发票处理系统启发的示例应用程序中使用Spring Integration。 我对此非常满意#xff0c;因此我决定向您展示如何使用Apache Camel#xff08;Spring Integration的最大… 开始之前 前一段时间我写了一篇关于Spring Integration的教程以演示如何在受现实发票处理系统启发的示例应用程序中使用Spring Integration。 我对此非常满意因此我决定向您展示如何使用Apache CamelSpring Integration的最大竞争对手构建完全相同的应用程序。 我们将要构建的应用程序几乎相同因此本教程中的很多文字也将如此。 新部分将主要集中在Apache Camel及其用法上。 您可以逐步按照本教程操作并从头开始创建应用程序也可以继续从github获取代码在这里下载源 https : //github.com/vrto/apache-camel-invoices 请注意还有更多方法来编写骆驼“路线” – Java DSLSpring XMLScala DSL…在本教程中我将使用Java DSL 但对于那些感兴趣的人–您也可以在此处找到通过Spring XML配置的相同项目。 。 无论您喜欢哪种方式都该开始了 发票处理申请书-功能说明 想象一下您在某家公司工作该公司会定期从各种承包商那里收到大量发票。 我们将建立一个能够接收发票过滤掉相关发票创建付款本地或国外并将其发送到某些银行服务的系统。 即使该系统非常幼稚当然也不适合企业使用我们仍将尝试在头脑中以良好的可伸缩性灵活性和分离的设计来构建它。 Apache Camel是一个集成框架–这意味着它为要集成的复杂系统提供了有意义的抽象。 本教程有很多重要的概念需要理解。 让我为您总结一下 骆驼上下文 将所有部分保持在一起的运行时系统。 消息 基本实体–消息传递的核心原理。 它由标题 正文和附件组成 。 Exchange 消息的容器摘要实际通过系统发送的内容。 它包含In消息还可以包含Out消息。 路线 处理器链。 如果您更喜欢“学术”的解释则路由是一个图形其中“节点”由某个处理器表示“线”由某个通道表示。 处理器 使用/修改传入的交换。 一个处理器的输出连接到另一处理器的输入。 端点 建模通道末端。 使用URI进行配置。 例 file:data/inbox?delay5000 组件 端点工厂。 用前缀引用jmsfile等。 如果您想了解有关骆驼核心概念的更多详细信息我强烈建议您阅读非常好的《 骆驼在行动》一书。 现在我们对基本概念有了一些了解让我们看一下下面的图片它是系统的摘要并遍历了重要的部分 在图片上您可以看到一条路线它说明了我们的消息传递结构和系统的核心组件-它们用红色数字标记。 让我们来看一下稍后我们将更详细地介绍每个组件 发票网关 –这是我们放置新发票以便它们可以进入消息传递层的地方。 拆分器 -该系统旨在接受发票的集合但是我们将需要单独处理每个发票。 更具体地说具有“收集”类型正文的邮件将被拆分为多个邮件其中每个邮件将具有单独的发票作为正文。 筛选器 -我们的系统旨在仅自动处理开具少于$ 10,000的那些发票。 基于内容的路由器 –有些发票使用IBAN帐号我们有两个不同的帐号-一个用于本地交易一个用于国外交易。 路由器组件的工作是将带有发票的消息发送到正确的通道-无论是本地发票还是国外发票。 变形金刚 –当我们在系统中接受发票时我们的银行API可以与其他类型一起使用–付款。 转换器组件的工作是根据提供的逻辑获取一些消息并将其转换为另一条消息。 我们希望将原始消息发票的有效负载转换为新的有效负载-付款。 Banking Service Activator –处理发票并生成一些实际付款后我们准备与外部银行系统进行对话。 我们已经公开了此类系统的服务当携带付款的消息进入正确的银行渠道时我们想激活一些逻辑–将付款传递给银行然后让银行进行进一步处理。 创建项目 到目前为止您应该对系统的功能以及其结构有一个较高的概述。 在开始编码之前您将需要一个实际的Maven项目并设置结构和所需的依赖关系。 如果您熟悉Maven请参见下面的pom.xml文件否则如果您想节省一些时间欢迎使用我为您创建的项目模板 下载Maven项目模板 。 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdapache-camel-invoices/groupIdartifactIdapache-camel-invoices/artifactIdversion1.0-SNAPSHOT/versionpropertiescamel-version2.12.0/camel-version/propertiesdependenciesdependencygroupIdorg.apache.camel/groupIdartifactIdcamel-core/artifactIdversion${camel-version}/version/dependencydependencygroupIdorg.apache.camel/groupIdartifactIdcamel-spring/artifactIdversion${camel-version}/version/dependencydependencygroupIdorg.apache.camel/groupIdartifactIdcamel-quartz/artifactIdversion2.9.5/version/dependencydependencygroupIdorg.apache.camel/groupIdartifactIdcamel-test-spring/artifactIdversion${camel-version}/versionscopetest/scope/dependencydependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.16/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.5/version/dependencydependencygroupIdcom.google.guava/groupIdartifactIdguava/artifactIdversion14.0.1/version/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource1.7/sourcetarget1.7/target/configuration/plugin!--- uncomment when InvoicesApplication class will existplugingroupIdorg.codehaus.mojo/groupIdartifactIdexec-maven-plugin/artifactIdversion1.2.1/versionconfigurationmainClasscom.vrtoonjava.invoices.InvoicesApplication/mainClass/configuration/plugin--/plugins/build/project自举骆驼 在使用Apache Camel之前我们需要对其进行一些引导。 首先我们将创建包com.vrtoonjava.routes 在其中放置路由配置。 其次我们将创建InvoicesRouteBuilder类该类将是配置路由的地方。 因此像这样创建类 package com.vrtoonjava.routes;import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;Component
public class InvoicesRouteBuilder extends RouteBuilder {Overridepublic void configure() throws Exception {//TODO configure route}
} 最后要做的是将Camel插入SpringCamel将使用Spring作为bean的注册表。 将文件camel-config.xml添加到您的src / main / resources文件夹中其中包含以下内容 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:camelhttp://camel.apache.org/schema/springxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsdcamelContext xmlnshttp://camel.apache.org/schema/springrouteBuilder refinvoicesRouteBuilder//camelContext/beans 现在让我们更详细地介绍系统的六个主要组件并获得实际的代码。 1.发票网关 首先让我们看一下Invoice的代码-这将是系统中的核心类之一。 我将使用com.vrtoonjava软件包作为根软件包使用invoices和banking作为子软件包 package com.vrtoonjava.invoices;import com.google.common.base.Objects;import java.math.BigDecimal;public class Invoice {private final String iban;private final String address;private final String account;private final BigDecimal dollars;public Invoice(String iban, String address, String account, BigDecimal dollars) {this.iban iban;this.address address;this.account account;this.dollars dollars;}public boolean isForeign() {return null ! iban !iban.isEmpty();}public String getAddress() {return address;}public String getAccount() {return account;}public BigDecimal getDollars() {return dollars;}public String getIban() {return iban;}Overridepublic String toString() {return Objects.toStringHelper(this).add(iban, iban).add(address, address).add(account, account).add(dollars, dollars).toString();}} 想象一下我们从另一个系统数据库Web服务或其他系统获取发票但是我们不想将此部分耦合到集成层。 我们将为此使用网关组件。 Gateway引入了一个协议 该协议将客户端代码与集成层分离在我们的示例中为Apache Camel依赖项。 让我们看一下InvoiceCollectorGateway的代码 package com.vrtoonjava.invoices;import java.util.Collection;/*** Defines a contract that decouples client from the Apache Camel framework.*/
public interface InvoiceCollectorGateway {void collectInvoices(CollectionInvoice invoices);} 我们定义了此接口以使客户仅依赖合同。 实际的实现将是特定于骆驼的我们可以这样创建它 package com.vrtoonjava.invoices;import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.springframework.stereotype.Component;import java.util.Collection;Component
public class CamelInvoiceCollectorGateway implements InvoiceCollectorGateway {Produce(uri seda:newInvoicesChannel)ProducerTemplate producerTemplate;Overridepublic void collectInvoices(CollectionInvoice invoices) {producerTemplate.sendBody(invoices);}} 请注意Produce批注。 该注释告诉Camel字段producerTemplate是端点seda:newInvoicesChannel 。 当客户端调用collectInvoices方法时网关将向seda:newInvoicesChannel通道发送一条新消息包含列表主体。 这使客户端与消息传递工具脱钩但是让我们将结果放置到实际的消息传递通道中。 为什么选择SEDA 通常在用Java构建消息传递系统时最终会使用诸如JMS之类的东西它提供了许多有用的功能例如高可靠性或消息持久性。 在本教程中我们将使用基于SEDA组件的更轻松的替代方法-它使我们能够利用几乎零配置的异步内存中消息传递。 2.发票分割器 从网关我们向包含发票集合的系统发送一条大消息-换句话说消息具有“ Collection类型的正文。 当我们要单独处理发票时我们将从seda:newInvoicesChannel获得结果并使用分离器组件该组件将创建多个消息。 这些新消息中的每一个都有一个“发票”类型的正文。 然后我们将消息放置到新渠道– seda:singleInvoicesChannel 。 这就是我们定义拆分器的方式将以下代码添加到InvoicesRouteBuilder的configure方法中 from(seda:newInvoicesChannel).log(LoggingLevel.INFO, Invoices processing STARTED).split(body()).to(seda:singleInvoicesChannel);3.过滤一些发票 我们系统的业务用例要求我们仅自动处理发出少于$ 10,000的发票。 为此我们将介绍一个过滤器组件。 我们将从seda:singleInvoicesChannel抓取消息对它们应用过滤逻辑然后将匹配的结果写入新的seda:filteredInvoicesChannel通道。 在Apache Camel中您可以插入自己的包含过滤逻辑的谓词 。 首先让我们定义这样的谓词通过扩展Camel的org.apache.camel.Predicate package com.vrtoonjava.invoices;import org.apache.camel.Exchange;
import org.apache.camel.Predicate;public class LowEnoughAmountPredicate implements Predicate {public static final int LOW_ENOUGH_THRESHOLD 10_000;Overridepublic boolean matches(Exchange exchange) {Invoice invoice exchange.getIn().getBody(Invoice.class);boolean lowEnough invoice.getDollars().intValue() LOW_ENOUGH_THRESHOLD;System.out.println(Amount of $ invoice.getDollars() (lowEnough ? can : can not) be automatically processed by system);return lowEnough;}} 为了简洁起见我不在本教程中粘贴单元测试–但是如果您有兴趣 请继续下载github项目并亲自查看测试 。 现在我们需要将此谓词连接到我们的路由因此将以下代码添加到您的configure方法中 from(seda:singleInvoicesChannel).filter(new LowEnoughAmountPredicate()).to(seda:filteredInvoicesChannel);4.路由发票 到目前为止我们已经拆分并过滤了一些发票。 现在是时候更仔细地检查每个发票的内容并决定是从当前国家本地还是从另一个国家外国发行的发票了。 Apache Camel允许使用Java DSL中的choice()方法来定义基于内容的路由器 。 我们甚至可以直接在Java DSL中访问消息的主体并执行一些简单的评估。 将以下代码添加到您的configure方法中并注意我们如何使用${body.isForeign}标准表达式来访问body from(seda:filteredInvoicesChannel).choice().when().simple(${body.isForeign}).to(seda:foreignInvoicesChannel).otherwise().to(seda:localInvoicesChannel); 我们将在本教程的第二部分中继续开发此应用程序。 参考 Apache Camel –在vrtoonjava博客上从JCG合作伙伴 Michal Vrtiak 从头开始开发应用程序第1部分/第2部分 。 翻译自: https://www.javacodegeeks.com/2013/10/apache-camel-developing-application-from-the-scratch-part-1-2.html