当前位置: 首页 > news >正文

青岛网站建设哪个好搭建网站教程视频

青岛网站建设哪个好,搭建网站教程视频,%2enet网站开发,手机网站和电脑网站开发这是本教程的第二部分#xff0c;我们将使用Apache Camel创建发票处理应用程序。 如果您错过了它#xff0c;一定要看一下第一部分 。 以前#xff0c;我们已经定义了系统的功能要求#xff0c;创建了网关#xff0c;分离器#xff0c;过滤器和基于内容的路由器组件。 让… 这是本教程的第二部分我们将使用Apache Camel创建发票处理应用程序。 如果您错过了它一定要看一下第一部分 。 以前我们已经定义了系统的功能要求创建了网关分离器过滤器和基于内容的路由器组件。 让我们继续创建一个转换器。 5.将发票转换为付款 现在我们已经成功地从系统中过滤掉了“过于昂贵”的发票它们可能需要人工检查等。 重要的是我们现在可以收取发票并从中产生付款。 首先让我们将Payment类添加到banking包中 package com.vrtoonjava.banking;import com.google.common.base.Objects;import java.math.BigDecimal;public class Payment {private final String senderAccount;private final String receiverAccount;private final BigDecimal dollars;public Payment(String senderAccount, String receiverAccount, BigDecimal dollars) {this.senderAccount senderAccount;this.receiverAccount receiverAccount;this.dollars dollars;}public String getSenderAccount() {return senderAccount;}public String getReceiverAccount() {return receiverAccount;}public BigDecimal getDollars() {return dollars;}Overridepublic String toString() {return Objects.toStringHelper(this).add(senderAccount, senderAccount).add(receiverAccount, receiverAccount).add(dollars, dollars).toString();}} 因为我们将有两种方法从本地和国外发票创建付款所以我们定义一个用于创建付款的通用合同界面。 将界面PaymentCreator放入banking包 package com.vrtoonjava.banking;import com.vrtoonjava.invoices.Invoice;/*** Creates payment for bank from the invoice.* Real world implementation might do some I/O expensive stuff.*/ public interface PaymentCreator {Payment createPayment(Invoice invoice) throws PaymentException;} 从技术上讲这是一个简单的参数化工厂。 请注意它将引发PaymentException 。 稍后我们将进行异常处理但这是简单的PaymentException的代码 package com.vrtoonjava.banking;public class PaymentException extends Exception {public PaymentException(String message) {super(message);}} 现在我们可以将两个实现添加到invoices包中了。 首先让我们创建LocalPaymentCreator类 package com.vrtoonjava.invoices;import com.vrtoonjava.banking.Payment; import com.vrtoonjava.banking.PaymentCreator; import com.vrtoonjava.banking.PaymentException; import org.springframework.stereotype.Component;Component public class LocalPaymentCreator implements PaymentCreator {// hard coded account value for demo purposesprivate static final String CURRENT_LOCAL_ACC current-local-acc;Overridepublic Payment createPayment(Invoice invoice) throws PaymentException {if (null invoice.getAccount()) {throw new PaymentException(Account can not be empty when creating local payment!);}return new Payment(CURRENT_LOCAL_ACC, invoice.getAccount(), invoice.getDollars());}} 另一个创建者将是ForeignPaymentCreator 它具有相当简单的实现 package com.vrtoonjava.invoices;import com.vrtoonjava.banking.Payment; import com.vrtoonjava.banking.PaymentCreator; import com.vrtoonjava.banking.PaymentException; import org.springframework.stereotype.Component;Component public class ForeignPaymentCreator implements PaymentCreator {// hard coded account value for demo purposesprivate static final String CURRENT_IBAN_ACC current-iban-acc;Overridepublic Payment createPayment(Invoice invoice) throws PaymentException {if (null invoice.getIban()) {throw new PaymentException(IBAN mustnt be null when creating foreign payment!);}return new Payment(CURRENT_IBAN_ACC, invoice.getIban(), invoice.getDollars());}} 这两个创建者是简单的Spring beanApache Camel提供了一种将它们连接到路由的非常好的方法。 我们将在Camel的Java DSL上使用transform()方法创建两个转换器。 我们将正确的转换器插入seda:foreignInvoicesChannel seda:localInvoicesChannel和seda:foreignInvoicesChannel seda:localInvoicesChannel 并使它们将结果转发到seda:bankingChannel 。 将以下代码添加到您的configure方法中 from(seda:foreignInvoicesChannel).transform().method(foreignPaymentCreator, createPayment).to(seda:bankingChannel);from(seda:localInvoicesChannel).transform().method(localPaymentCreator, createPayment).to(seda:bankingChannel);6.将付款转到银行服务服务激活器 付款准备就绪包含付款的消息正在seda:bankingChannel中等待。 该流程的最后一步是使用Service Activator组件。 它的工作方式很简单–当频道中出现新消息时Apache Camel会调用Service Activator组件中指定的逻辑。 换句话说我们正在将外部服务连接到我们现有的消息传递基础结构。 为此我们首先需要查看银行服务合同。 因此将BankingService接口BankingService到banking程序包中在现实世界中它可能驻留在某些外部模块中 package com.vrtoonjava.banking;/*** Contract for communication with bank.*/ public interface BankingService {void pay(Payment payment) throws PaymentException;} 现在我们将需要BankingService的实际实现。 同样实现不太可能驻留在我们的项目中它可能是远程公开的服务但是至少出于教程目的让我们创建一些模拟实现。 将MockBankingService类添加到banking包 package com.vrtoonjava.banking;import org.springframework.stereotype.Service;import java.util.Random;/*** Mock service that simulates some banking behavior.* In real world, we might use some web service or a proxy of real service.*/ Service public class MockBankingService implements BankingService {private final Random rand new Random();Overridepublic void pay(Payment payment) throws PaymentException {if (rand.nextDouble() 0.9) {throw new PaymentException(Banking services are offline, try again later!);}System.out.println(Processing payment payment);}} 模拟实施会在某些随机情况下约10造成失败。 当然为了实现更好的解耦我们不会直接使用它而是将根据自定义组件在合同接口上创建依赖关系。 让我们现在将PaymentProcessor类添加到invoices包中 package com.vrtoonjava.invoices;import com.vrtoonjava.banking.BankingService; import com.vrtoonjava.banking.Payment; import com.vrtoonjava.banking.PaymentException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;/*** Endpoint that picks Payments from the system and dispatches them to the* service provided by bank.*/ Component public class PaymentProcessor {AutowiredBankingService bankingService;public void processPayment(Payment payment) throws PaymentException {bankingService.pay(payment);}} Apache Camel提供了一种简单的方法当消息到达特定端点时如何在任意bean上调用方法 EIP将其描述为服务激活器方法是在Camel的Java DSL上使用bean()方法 from(seda:bankingChannel).bean(PaymentProcessor.class, processPayment);错误处理 消息传递系统的最大挑战之一是正确识别和处理错误情况。 EAI描述了许多方法我们将使用Camel的Dead Letter Channel EIP的实现。 死信通道只是另一个通道当该通道中出现错误消息时我们可以采取适当的措施。 在实际的应用程序中我们可能会寻求一些重试逻辑或专业报告在我们的示例教程中我们只会打印出错误原因。 让我们修改先前定义的Service Activator并插入errorHandler()组件。 当PaymentProcessor引发异常时此errorHandler会将引起错误的原始消息转发到Dead Letter Channel from(seda:bankingChannel).errorHandler(deadLetterChannel(log:failedPayments)).bean(PaymentProcessor.class, processPayment); 最后这是最终的完整路线 package com.vrtoonjava.routes;import com.vrtoonjava.invoices.LowEnoughAmountPredicate; import com.vrtoonjava.invoices.PaymentProcessor; import org.apache.camel.LoggingLevel; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component;Component public class InvoicesRouteBuilder extends RouteBuilder {Overridepublic void configure() throws Exception {from(seda:newInvoicesChannel).log(LoggingLevel.INFO, Invoices processing STARTED).split(body()).to(seda:singleInvoicesChannel);from(seda:singleInvoicesChannel).filter(new LowEnoughAmountPredicate()).to(seda:filteredInvoicesChannel);from(seda:filteredInvoicesChannel).choice().when().simple(${body.isForeign}).to(seda:foreignInvoicesChannel).otherwise().to(seda:localInvoicesChannel);from(seda:foreignInvoicesChannel).transform().method(foreignPaymentCreator, createPayment).to(seda:bankingChannel);from(seda:localInvoicesChannel).transform().method(localPaymentCreator, createPayment).to(seda:bankingChannel);from(seda:bankingChannel).errorHandler(deadLetterChannel(log:failedPayments)).bean(PaymentProcessor.class, processPayment);}}运行整个事情 现在我们将创建一个作业它将以固定的速率将新发票发送到系统。 它只是利用Spring的Scheduled注释的标准Spring bean。 因此我们向项目添加一个新类– InvoicesJob package com.vrtoonjava.invoices;import org.apache.camel.Produce; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import java.util.ArrayList; import java.util.Collection; import java.util.List;Component public class InvoicesJob {private int limit 10; // default value, configurableAutowiredInvoiceCollectorGateway invoiceCollector;AutowiredInvoiceGenerator invoiceGenerator;Scheduled(fixedRate 4000)public void scheduleInvoicesHandling() {CollectionInvoice invoices generateInvoices(limit);System.out.println(\n Sending invoices.size() invoices to the system);invoiceCollector.collectInvoices(invoices);}// configurable from Injectorpublic void setLimit(int limit) {this.limit limit;}private CollectionInvoice generateInvoices(int limit) {ListInvoice invoices new ArrayList();for (int i 0; i limit; i) {invoices.add(invoiceGenerator.nextInvoice());}return invoices;} } Job会调用每4秒一次 InvoicesGenerator并将发票转发到Gateway我们了解的第一个组件。 为了使其工作我们还需要InvoicesGenerator类 package com.vrtoonjava.invoices;import org.springframework.stereotype.Component;import java.math.BigDecimal; import java.util.Random;/*** Utility class for generating invoices.*/ Component public class InvoiceGenerator {private Random rand new Random();public Invoice nextInvoice() {return new Invoice(rand.nextBoolean() ? iban() : null, address(), account(), dollars());}private BigDecimal dollars() {return new BigDecimal(1 rand.nextInt(20_000));}private String account() {return test-account rand.nextInt(1000) 1000;}private String address() {return Test Street rand.nextInt(100) 1;}private String iban() {return test-iban- rand.nextInt(1000) 1000;}} 这只是一个简单的模拟功能可让我们看到系统的运行情况。 在现实世界中我们不会使用任何生成器而可能会使用某些公开的服务。 现在在resources文件夹下创建一个新的spring配置文件– invoices-context.xml并声明组件扫描和任务计划支持 ?xml version1.0 encodingUTF-8? beans xmlns http://www.springframework.org/schema/beansxmlns:xsi http://www.w3.org/2001/XMLSchema-instance xmlns:task http://www.springframework.org/schema/taskxmlns:context http://www.springframework.org/schema/contextxsi:schemaLocation http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdimport resource camel-config.xml /context:component-scan base-package com.vrtoonjava /task:executor id executor pool-size10 /task:scheduler id scheduler pool-size10 /task:annotation-driven executorexecutor schedulerscheduler //beans 要查看整个运行过程我们还需要最后一块-标准Java主应用程序我们将在其中创建Spring的ApplicationContext。 package com.vrtoonjava.invoices;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Entry point of the application.* Creates Spring context, lets Spring to schedule job and use schema.*/ public class InvoicesApplication {public static void main(String[] args) {new ClassPathXmlApplicationContext(/invoices-context.xml);}} 只需从命令行运行mvn clean install并在InvoicesApplication类中启动main方法。 您应该能够看到类似的输出 Sending 10 invoices to the system 13:48:54.347 INFO [Camel (camel-1) thread #0 - seda://newInvoicesChannel][route1] Invoices processing STARTED Amount of $4201 can be automatically processed by system Amount of $15110 can not be automatically processed by system Amount of $17165 can not be automatically processed by system Amount of $1193 can be automatically processed by system Amount of $6077 can be automatically processed by system Amount of $17164 can not be automatically processed by system Amount of $11272 can not be automatically processed by system Processing payment Payment{senderAccountcurrent-local-acc, receiverAccounttest-account 1901000, dollars4201} Amount of $3598 can be automatically processed by system Amount of $14449 can not be automatically processed by system Processing payment Payment{senderAccountcurrent-local-acc, receiverAccounttest-account 8911000, dollars1193} Amount of $12486 can not be automatically processed by system 13:48:54.365 INFO [Camel (camel-1) thread #5 - seda://bankingChannel][failedPayments] Exchange[ExchangePattern: InOnly, BodyType: com.vrtoonjava.banking.Payment, Body: Payment{senderAccountcurrent-iban-acc, receiverAccounttest-iban-7451000, dollars6077}] Processing payment Payment{senderAccountcurrent-iban-acc, receiverAccounttest-iban-6201000, dollars3598} 参考 Apache Camel –在vrtoonjava博客上从JCG合作伙伴 Michal Vrtiak 从头开始开发应用程序第2部分/第2部分 。 翻译自: https://www.javacodegeeks.com/2013/11/apache-camel-developing-application-from-the-scratch-part-2-2.html
http://wiki.neutronadmin.com/news/106001/

相关文章:

  • 营销型网站建设营销型手机开发者选项怎么打开
  • 南山网站建设深圳信科怎么注册公司税最低
  • 东莞建站公司运转全网天下有 名wordpress 仿花瓣
  • 学校网站建设工作计划电子商务网站建设的核心多选
  • 营销型网站规划抖音宣传推广
  • 网站套餐到期什么意思海外网站cdn加速下载
  • 怎么做网站结构拓扑图wordpress 文章表
  • 惠州网站建设惠州建设劳务公司注册条件
  • 专业网站设计推荐深圳网络营销推广培训
  • asp网站安装解决方案
  • 网站设计的网站赣州是哪个省
  • app推广多少钱一个宿州网站建设优化
  • 如何建立一个小程序的网站wordpress首页改颜色
  • 网页网站建设软件网站建设高端培训学校
  • 深圳福田园岭网站建设如何建设公司的网站首页
  • 福州网站建设索q4791857002022重大时政热点事件简短
  • 淘宝客网站免费建站建筑工程网络设计
  • 网站建设用图制作音乐排行榜html5
  • 云南微网站建设的公司有哪些咨询公司怎么注册
  • 移动端网站建设的尺寸广西明电建设有限公司网站
  • 流行网站设计做婚庆网站有哪些内容
  • 网站建设贵苏州网络
  • 做网站公司费用wordpress的标题字体大小
  • 如何在网站后台备份数据库表网站建设氺金手指排名15
  • 权威的南昌网站建设是wordpress
  • dede5.7模板 新闻文章网站源码什么网站都有漏洞
  • 沈阳 网站制作报价网页制作自学教程
  • 广西建设厅招投标中心网站如何在百度上发布自己的广告
  • 安徽省住房与城乡建设网站网站制作一年多少钱
  • 广东广州免费建站营销型企业网站有哪些平台