山东省城乡与建设厅网站首页,优化网站具体如何做,wordpress 请提供有效的用户名.,wordpress访问私密帖子apache camel最近几年#xff0c;ESB软件越来越受欢迎。 如果大多数人通常知道什么是ESB#xff0c;那么他们很少会清楚地了解这种体系结构的不同组件的确切作用。 例如#xff0c;Apache ServiceMix由三个主要组件组成#xff1a;Apache Karaf#xff08;OSGI容器#… apache camel 最近几年ESB软件越来越受欢迎。 如果大多数人通常知道什么是ESB那么他们很少会清楚地了解这种体系结构的不同组件的确切作用。 例如Apache ServiceMix由三个主要组件组成Apache KarafOSGI容器Apache ActiveMQ消息代理和Apache Camel。 顺便问一下骆驼到底是什么 什么是“ routing and mediation engine ” 有什么用 我已经与Camel一起工作了大约一年我认为-尽管根本不是Camel专家但我现在有足够的后见之明可以使用一些非常具体的示例让您发现Camel的兴趣和力量。为了清楚起见在本文的其余部分中我将使用Spring DSL –假设读者熟悉Spring语法。 用例 让我们想象一下我们想使用Camel实现以下场景。 产品信息请求将以平面文件CSV格式的形式发送到特定文件夹中。 该文件的每一行都包含特定客户关于特定汽车型号的单个请求。 我们要向这些客户发送有关他们感兴趣的汽车的电子邮件。为此我们首先需要调用Web服务以获取其他客户数据例如他们的电子邮件。 然后我们必须从数据库中获取汽车特性让我们说一个文本。 由于我们希望邮件看起来像样例如HTML因此也需要进行小的文本转换。 当然我们不希望仅对请求进行顺序处理而是希望引入一些并行性。 同样我们也不想多次将完全相同的邮件发送给不同的客户而是将相同的唯一邮件发送给多个收件人。 利用我们后端的集群功能来平衡对Web服务的调用也将是一件很不错的事情。 最后在处理请求失败的情况下我们希望以某种方式跟踪原始请求以便例如可以通过邮政发送。 一个可能的骆驼实现 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://camel.apache.org/schema/springhttp://camel.apache.org/schema/spring/camel-spring.xsd
camelContext xmlnshttp://camel.apache.org/schema/spring errorHandlerRefmyDLQ!-- 2 redeliveries max before failed message is placed into a DLQ --errorHandler idmyDLQ typeDeadLetterChannel deadLetterUriactivemq:queue:errors useOriginalMessagetrueredeliveryPolicy maximumRedeliveries2//errorHandler!-- The polling of a specific folder every 30 sec --route idroute1from urifile:///Users/bli/folderToPoll?delay30000deletetrue/unmarshalcsv//unmarshalsplitsimple${body}/simplesetHeader headerNamecustomerIdsimple${body[1]}/simple/setHeadersetHeader headerNamecarModelIdsimple${body[2]}/simple/setHeadersetBodysimple${body[0]}/simple/setBodyto uriactivemq:queue:individualRequests?disableReplyTotrue//split/route!-- The consumption of individual (jms) mailing requests --route idroute2from uriactivemq:queue:individualRequests?maxConcurrentConsumers5/pipelineto uridirect:getCustomerEmail/to uridirect:sendMail//pipeline/route!-- Obtain customer email by parsing the XML response of a REST web service --route idroute3from uridirect:getCustomerEmail/setBodyconstant//setBodyloadBalanceroundRobin/to urihttp://backend1.mycompany.com/ws/customers?id{customerId}authMethodBasicauthUsernamegeekauthPasswordsecret/to urihttp://backend2.mycompany.com/ws/customers?id{customerId}authMethodBasicauthUsernamegeekauthPasswordsecret//loadBalancesetBodyxpath resultTypejava.lang.String/customer/general/email/xpath/setBody/route!-- Group individual sendings by car model --route idroute4from uridirect:sendMail/aggregate strategyRefmyAggregator completionSize10correlationExpressionsimpleheader.carModelId/simple/correlationExpressioncompletionTimeoutconstant60000/constant/completionTimeoutsetHeader headerNamerecipientssimple${body}/simple/setHeaderpipelineto uridirect:prepareMail/to uridirect:sendMailToMany//pipeline/aggregate/route!-- Prepare the mail content --route idroute5from uridirect:prepareMail/setBodysimpleheader.carModelId/simple/setBodypipelineto urisql:SELECT xml_text FROM template WHERE template_id # ?dataSourceRefmyDS/to urixslt:META-INF/xsl/email-formatter.xsl//pipeline/route!-- Send a mail to multiple recipients --route idroute6from uridirect:sendMailToMany/to urismtp://mail.mycompany.com:25?usernamegeekpasswordsecretfromno-replymycompany.comto{recipients}subjectYour requestcontentTypetext/html/log messageMail ${body} successfully sent to ${headers.recipients}//route/camelContext!-- Pure Spring beans referenced in the various Camel routes --!-- The ActiveMQ broker --bean idactivemq classorg.apache.activemq.camel.component.ActiveMQComponentproperty namebrokerURL valuetcp://localhost:61616//bean!-- A datasource to our database --bean idmyDS classorg.apache.commons.dbcp.BasicDataSourceproperty namedriverClassName valueorg.h2.Driver/property nameurl valuejdbc:h2:file:/Users/bli/db/MyDatabase;AUTO_SERVERTRUE;TRACE_LEVEL_FILE0/property nameusername valuesa/property namepassword valuesa//bean!-- An aggregator implementation --bean idmyAggregator classcom.mycompany.camel.ConcatBody//beans 和仅Java类的代码 public class ConcatBody implements AggregationStrategy {public static final String SEPARATOR , ;public Exchange aggregate(Exchange aggregate, Exchange newExchange) {if (aggregate null) {// The aggregation for the very exchange item is the exchange itselfreturn newExchange;} else {// Otherwise, we augment the body of current aggregate with new incoming exchangeString originalBody aggregate.getIn().getBody(String.class);String bodyToAdd newExchange.getIn().getBody(String.class);aggregate.getIn().setBody(originalBody SEPARATOR bodyToAdd);return aggregate;} }}一些解释 “ route1 ”处理传入的平面文件。 首先将文件内容解组使用CSV格式然后将其拆分为行/记录。 每行都将变成发送到JMS队列的单独通知。 “ route2 ”正在使用这些通知。 基本上完成一个请求意味着依次执行两件事“管道”获取客户电子邮件route3并向他发送邮件route4。 请注意“ maxConcurrentConsumers”参数该参数用于轻松满足我们的并行性要求。 “ route3 ”对如何获取客户电子邮件进行建模只需通过解析使用XPath两个后端节点上可用的安全的REST Web服务的XML响应即可。 “ route4 ”包含发送大量邮件的逻辑。 每次收集到10个类似的发送请求在我们的示例中是对同一辆汽车的10个请求并且我们不准备等待超过1分钟我们希望整个过程以新消息继续进行或骆驼术语中的“交换”是10条组合消息的串联。 继续该过程意味着首先准备邮件正文路由5然后将其发送到组路由6。 在“ route5 ”中发出SQL查询以便根据汽车型号获得适当的文本。 在该结果上我们应用了一个小的XSL-T转换它将用xsl转换的输出替换当前交换主体。 当输入“ route6 ”时交换包含我们需要的一切。 我们有收件人列表作为标题还有正文中要发送的html文本。 因此我们现在可以继续使用SMTP协议进行实际发送。 如果出现错误例如临时网络问题–在整个过程中的任何地方Camel都会在放弃之前最多进行两次其他尝试。 在后一种情况下始发消息将由Camel自动放置到JMS死信队列中。 结论 骆驼确实是一个很棒的框架-并非完美但仍然很棒。 您会惊讶地看到只需几行代码即可对复杂的场景或路线进行建模。 您也可能很高兴看到您的代码多么清晰同事们能够多快地理解您的路线逻辑。 但这当然不是主要优势。 使用Camel主要是邀请您考虑企业集成模式又称“ EIP” 它可以帮助您使用众所周知的成熟技术将原始复杂性分解为不太复杂可能是并发的子路由从而实现更模块化更灵活的实现。 特别是使用去耦技术可以简化解决方案中单个零件或组件的替换或重构。 参考从我们的W4G合作伙伴 Bernard Ligny中 发现Apache Camel的功能 。 翻译自: https://www.javacodegeeks.com/2012/12/discovering-the-power-of-apache-camel.htmlapache camel