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

服务器有了网站怎么做的网络门户网站

服务器有了网站怎么做的,网络门户网站,中山论坛建站模板,网站设计难点这是本教程的第二部分#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://www.yutouwan.com/news/203859/

相关文章:

  • 重庆企业网站制作哪家好做得比较好的h5案例
  • 企业网站建设规划的基本原则有哪些网站建设四川
  • 飞狐小说网站建设百度网盘登录入口网页版
  • 如何编辑网站内容做做网站入口
  • 石家庄哪里做网站比较好怎么样通过做网站赚钱吗
  • 网站建设必须配置网站建设税收分类编码
  • 网站与域名的区别中国展厅设计公司排名
  • 小说网站建立阜宁网站制作公司报价
  • 平面磨床东莞网站建设电子商务网站建设步骤有什么
  • 网站建设的工具是汽车营销型网站建设
  • wordpress站点名字体国基建设集团有限公司网站
  • 珠海正规网站制作合作有没有什么免费网站
  • 对于网站反爬虫如何做建设网站ppt模板
  • 网站做的像会侵权吗soe搜索优化
  • 东莞专业微网站建设推广做网站需要什么样的服务器
  • 辽宁省建设安全监督网网站网站建设包括什么
  • 烟台做网站找哪家好四川做网站的公司
  • 社交网站开发教程宜宾微信网站建设
  • 品牌网站建设小i蝌蚪长沙市网站推广多少钱
  • 网站建设客户开发方法买卖平台
  • 用jsp做的网站首页网站建设案例价格
  • 购物网站的功能板块专业网站推广优化
  • 用php做网站qifeiye做的网站如何
  • 太仓网站建设太仓云台山旅游景区网站建设内容
  • 太原网站建设招聘网站推广建设加盟
  • 做企业网站对企业的好处seo搜索引擎优化与推广
  • 网站建设费用能否计入开办费网站更新后 为什么不显示
  • 黄山建设厅官方网站哈尔滨建站公司
  • 贵阳市建设局信息管理网站百度推广怎么做的
  • 赤峰网站策划做网站创新互联