从四个java源码构文件建一个机票预定业务的规则引擎(下篇)

一、前言

本文仅代表作者的个人观点;

本文的内容仅限于技术探讨,不能作为指导生产环境的素材;

本文素材是红帽公司产品技术和手册;

本文分为上篇和下篇两部分,本文是下篇;

二、一个飞机购票程序源码解析

我们继续上一篇的案例,看一个kjar:kie-server-lab-kjar。

Kjar和普通jar不同的地方是,它把规则打包起来。

我们看一下kie-server-lab-kjar的目录结构:

通过IDE工具打开:查看订票流程定义文件:

我们查看这个kjar的三个源码文件,这其实是三个数据对象:

第一个:Applicant.java,这个代码主要是获取买票申请人的个人信息:

先看外部结构:

package org.specialtripsagency;

/**

* This class was automatically generated by the data modeler tool.

*/

public class Applicant implements java.io.Serializable

{

static final long serialVersionUID = 1L;

private java.lang.String name;

private java.lang.String emailAddress;

private java.lang.Integer numberOfTravelers;

private java.lang.String otherDetails;

public Applicant()

{

}

public java.lang.String getName()

{

return this.name;

}

public void setName(java.lang.String name)

{

this.name = name;

}

public java.lang.String getEmailAddress()

{

return this.emailAddress;

}

public void setEmailAddress(java.lang.String emailAddress)

{

this.emailAddress = emailAddress;

}

public java.lang.Integer getNumberOfTravelers()

{

return this.numberOfTravelers;

}

public void setNumberOfTravelers(java.lang.Integer numberOfTravelers)

{

this.numberOfTravelers = numberOfTravelers;

}

public java.lang.String getOtherDetails()

{

return this.otherDetails;

}

public void setOtherDetails(java.lang.String otherDetails)

{

this.otherDetails = otherDetails;

}

public Applicant(java.lang.String name, java.lang.String emailAddress,

java.lang.Integer numberOfTravelers, java.lang.String otherDetails)

{

this.name = name;

this.emailAddress = emailAddress;

this.numberOfTravelers = numberOfTravelers;

this.otherDetails = otherDetails;

}

}

这个源码的外部结构:

第二个:BookingObject.java,这个主要是预定的对象:

package org.specialtripsagency;

/**

* This class was automatically generated by the data modeler tool.

*/

public class BookingObject extends java.lang.Object implements java.io.Serializable

{

static final long serialVersionUID = 1L;

private org.specialtripsagency.Applicant applicant;

private String bookingId;

private org.specialtripsagency.Flight flight;

public BookingObject()

{

}

public org.specialtripsagency.Applicant getApplicant()

{

return this.applicant;

}

public void setApplicant(org.specialtripsagency.Applicant applicant)

{

this.applicant = applicant;

}

public java.lang.String getBookingId()

{

return this.bookingId;

}

public void setBookingId(java.lang.String bookingId)

{

this.bookingId = bookingId;

}

public org.specialtripsagency.Flight getFlight()

{

return this.flight;

}

public void setFlight(org.specialtripsagency.Flight flight)

{

this.flight = flight;

}

public BookingObject(org.specialtripsagency.Applicant applicant,

java.lang.String bookingId, org.specialtripsagency.Flight flight)

{

this.applicant = applicant;

this.bookingId = bookingId;

this.flight = flight;

}

}

再看第三个:Flight.java,这个主要是航班信息相关内容:

package org.specialtripsagency;

/**

* This class was automatically generated by the data modeler tool.

*

*/

public class Flight extends java.lang.Object implements java.io.Serializable

{

static final long serialVersionUID = 1L;

private java.lang.String carrier;

private java.lang.String flightNr;

private java.lang.Integer priceDiscount;

private java.lang.Integer ratePerPerson;

private java.lang.String returnDate;

private java.lang.String startCity;

private java.lang.String targetCity;

private java.lang.String travelDate;

private java.lang.String travelClass;

private java.lang.String bookingStatus;

private java.lang.String bookingId;

public Flight()

{

}

public java.lang.String getCarrier()

{

return this.carrier;

}

public void setCarrier(java.lang.String carrier)

{

this.carrier = carrier;

}

public java.lang.String getFlightNr()

{

return this.flightNr;

}

public void setFlightNr(java.lang.String flightNr)

{

this.flightNr = flightNr;

}

public java.lang.Integer getPriceDiscount()

{

return this.priceDiscount;

}

public void setPriceDiscount(java.lang.Integer priceDiscount)

{

this.priceDiscount = priceDiscount;

}

public java.lang.Integer getRatePerPerson()

{

return this.ratePerPerson;

}

public void setRatePerPerson(java.lang.Integer ratePerPerson)

{

this.ratePerPerson = ratePerPerson;

}

public java.lang.String getReturnDate()

{

return this.returnDate;

}

public void setReturnDate(java.lang.String returnDate)

{

this.returnDate = returnDate;

}

public java.lang.String getStartCity()

{

return this.startCity;

}

public void setStartCity(java.lang.String startCity)

{

this.startCity = startCity;

}

public java.lang.String getTargetCity()

{

return this.targetCity;

}

public void setTargetCity(java.lang.String targetCity)

{

this.targetCity = targetCity;

}

public java.lang.String getTravelDate()

{

return this.travelDate;

}

public void setTravelDate(java.lang.String travelDate)

{

this.travelDate = travelDate;

}

public java.lang.String getTravelClass()

{

return this.travelClass;

}

public void setTravelClass(java.lang.String travelClass)

{

this.travelClass = travelClass;

}

public java.lang.String getBookingStatus()

{

return this.bookingStatus;

}

public void setBookingStatus(java.lang.String bookingStatus)

{

this.bookingStatus = bookingStatus;

}

public java.lang.String getBookingId()

{

return this.bookingId;

}

public void setBookingId(java.lang.String bookingId)

{

this.bookingId = bookingId;

}

public Flight(java.lang.String carrier, java.lang.String flightNr,

java.lang.Integer priceDiscount, java.lang.Integer ratePerPerson,

java.lang.String returnDate, java.lang.String startCity,

java.lang.String targetCity, java.lang.String travelDate,

java.lang.String travelClass, java.lang.String bookingStatus,

java.lang.String bookingId)

{

this.carrier = carrier;

this.flightNr = flightNr;

this.priceDiscount = priceDiscount;

this.ratePerPerson = ratePerPerson;

this.returnDate = returnDate;

this.startCity = startCity;

this.targetCity = targetCity;

this.travelDate = travelDate;

this.travelClass = travelClass;

this.bookingStatus = bookingStatus;

this.bookingId = bookingId;

}

}

在BPM中打开Kjar

查看数据对象:

再看business流程:

选择org.specialtripsagency package.

接下来看一下流程建模,本流程是审核机票预订流程的定义。

我们分步骤查看,整体上,就是订票,输入信息,然后付款,付款成功订票成功;否则取消:

第一步Prepare WS Data的脚本:

/* Populating Applicant object */

org.specialtripsagency.Applicant _applicant = new org.specialtripsagency.Applicant();

_applicant.setName(applicantName);

/* Populating Flight object */

org.specialtripsagency.Flight _flight = new org.specialtripsagency.Flight();

_flight.setCarrier(flight_carrier);

_flight.setFlightNr(flight_flightNr);

_flight.setTravelClass(flight_travelClass);

/* Populating BookingObject object */

org.specialtripsagency.BookingObject _booking = new org.specialtripsagency.BookingObject();

_booking.setApplicant(_applicant);

_booking.setFlight(_flight);

kcontext.setVariable("booking", _booking);

/* FlightService : Booking */

org.acme.specialtripsagency.flightservice.Booking _flightBooking = new org.acme.specialtripsagency.flightservice.Booking();

_flightBooking.setCarrier(_flight.getCarrier());

_flightBooking.setFlightNr(_flight.getFlightNr());

_flightBooking.setTravelClass(_flight.getTravelClass());

_flightBooking.setType("PREBOOKING");

kcontext.setVariable("flightBooking", _flightBooking);

第二步:flight pre-booking:

这步主要做的数据重定向:

第三步:处理订票数据:

/* Populate BookingObject with data from flight booking */

org.specialtripsagency.BookingObject _booking = (org.specialtripsagency.BookingObject)kcontext.getVariable("booking");

org.acme.specialtripsagency.flightservice.BookingResponse _flightBookingResponse = (org.acme.specialtripsagency.flightservice.BookingResponse)kcontext.getVariable("flightBookingResponse");

_booking.getFlight().setBookingId(_flightBookingResponse.getBooking());

_booking.getFlight().setBookingStatus(_flightBookingResponse.getStatus());

_booking.setBookingId(_flightBookingResponse.getBooking());

第四步:Payment Instructions STUB

System.out.println("Flight Booking Process: Payment Instructions Sent to Customer");

第五步:Process payment

选择1:Prepare Data for confirmation

org.specialtripsagency.BookingObject _booking = (org.specialtripsagency.BookingObject) kcontext.getVariable("booking");

org.specialtripsagency.Flight _flight = _booking.getFlight();

/* FlightService : Booking */

org.acme.specialtripsagency.flightservice.Booking _flightBooking = new org.acme.specialtripsagency.flightservice.Booking();

_flightBooking.setCarrier(_flight.getCarrier());

_flightBooking.setFlightNr(_flight.getFlightNr());

_flightBooking.setTravelClass(_flight.getTravelClass());

_flightBooking.setType("BOOKING");

_flightBooking.setBooking(_flight.getBookingId());

kcontext.setVariable("flightBooking", _flightBooking);

订票成功:

选择2:订票取消:

org.specialtripsagency.BookingObject _booking = (org.specialtripsagency.BookingObject) kcontext.getVariable("booking");

org.acme.specialtripsagency.flightservice.BookingCancellation _flightCancellation = new org.acme.specialtripsagency.flightservice.BookingCancellation();

_flightCancellation.setBooking(_booking.getFlight().getBookingId());

kcontext.setVariable("flightCancellation", _flightCancellation);

机票取消:

三、源码的构建与部署

选择构建和部署:

增加web container:

查看部署在KIE服务器上的容器:

四、流程模拟

点击下图start:

提示输入信息:购票人、航空公司、航班号、仓位:

处理任务:

提交以后:

这个时候,流程已经走到了:

处理支付:

魏新宇

  • "大魏分享"运营者、红帽资深解决方案架构师
  • 专注开源云计算、容器及自动化运维在金融行业的推广
  • 拥有MBA、ITIL V3、Cobit5、C-STAR、TOGAF9.1(鉴定级)等管理认证。
  • 拥有红帽RHCE/RHCA、VMware VCP-DCV、VCP-DT、VCP-Network、VCP-Cloud、AIX、HPUX等技术认证。

原文发布于微信公众号 - 大魏分享(david-share)

原文发表时间:2018-06-02

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ccylovehs

Java爬取网络博客文章

近期本人在某云上购买了个人域名,本想着以后购买与服务器搭建自己的个人网站,由于需要筹备的太多,暂时先搁置了,想着先借用GitHub Pages搭建一个静态的站,...

811
来自专栏JackieZheng

Java豆瓣电影爬虫——模拟登录的前世今生与验证码的爱恨情仇

前言 并不是所有的网站都能够敞开心扉让你看个透彻,它们总要给你出些难题让你觉得有些东西是来之不易的,往往,这也更加激发你的激情和斗志! 从《为了媳妇的一张号,...

3276
来自专栏Petrichor的专栏

python: TODO 助记符

TODO 在 python 中作为一种 助记符 (Mnemonics),用来解释 将要做什么 。

662
来自专栏编程之旅

iOS 全局禁止横屏,但视频播放界面选择性横屏的解决办法

有时我们的APP并没有适配横屏的需求,但是在个别视频播放界面,我们需要在播放视频的时候横屏,退出全屏的时候不能横屏,但是有时候并没有原生API并没有给出解决方案...

1732
来自专栏乐沙弥的世界

Oracle字符集与字符类型存储空间占用

562
来自专栏程序员互动联盟

android apk 防反编译技术第一篇-加壳技术

做android framework方面的工作将近三年的时间了,现在公司让做一下android apk安全方面的研究,于是最近就在网上找大量的资料来学习。现在...

3818
来自专栏杨建荣的学习笔记

linux乱码和数据库乱码的问题简单排查(r2笔记83天)

在本地的虚拟机环境一直凑合着用英文,今天想看看中文的东西都显示乱码,下定决心要把问题解决了。 如果直接打印文本内容,通过putty也会显示乱码。 [ora11g...

3314
来自专栏冷冷

【Jfinal源码】第一章 com.jfinal.core.JFinalFilter(1)

前言: 首先在gitosc获取到jfinal的源码,本学习笔记使用的是jfinal2.2版本。 ---- 从web.xml开始,我们去学习jfinal是怎么从...

1996
来自专栏Java成神之路

Java企业微信开发_03_自定义菜单

这里需要格外注意的是,企业微信中请求包的数据是Json字符串格式的,而不是xml格式。关于json序列化的问题请参考上一节   Java企业微信开发_03_通讯...

812
来自专栏GIS讲堂

shape文件上传与展示

812

扫码关注云+社区