SCA概念与应用实践(5. 一个例子演示)

5.1.        Application定义

下图是我们要实现的application

5_1

 

图5_1

这个只有一层composite, 命名为 restaurant,

它包含5component

    RestaurantServiceComponent

        显示餐馆菜单

        结帐账单.

    MenuServiceComponent

        提供菜单名字和价格.

    BillServiceComponent

        计算账单,包括消费税和小费.

    VATServiceComponent

        计算包含消费税的账单价格.

    TipServiceComponent

        计算小费的价格.

 

5.2.        过程

5.2.1.       创建一个java project

Ø        选择菜单 New > Java Project.

Ø        Project 名字为"Restaurant2". 选择 “Create separate folders for sources and class files”,点击next

Ø        Libraries里面,添加User Library TUSCANY

5_2

              图5_2

Ø        点击”next””finish”.

 

5.3.        创建SCA Composite diagram

Ø        project上右键,选择 New > Other....

Ø        SCA目录下,选择 SCA Composite Diagram,点击next

          5_3   

图5_3

Ø        文件名字设为Restaurant2.composite_diagram,点击 Finish.

 

完成后SCA Composite Designer自动打开新创建的文件. diagram上右键,选择Show Properties 来打开Properties view

 

5_4

图5_4

5.4.        添加component

使用palette创建component

5_5

图5_5

5_6

图5_6

 

 

 

填入RestaurantServiceComponent作为component的名字。同样创建其他的compoentMenuServiceComponent, BillServiceComponent, VatServiceComponent, and TipServiceComponent.

5_7

 

图5_7 

5.5.        添加Service

       有两种方式给component添加service

       一个是使用palette,在palette上选择service,然后在要添加的component上点一下,一个service就添加到component

       第二个方法是把鼠标移到component上,一个工具条自动就出现,选择绿色的那个。

 5_8

图5_8

    * RestaurantServiceComponent上添加 RestaurantService,

    *MenuServiceComponent上添加MenuService,

    *BillServiceComponent上添加BillService,

    *VatServiceComponent上添加VatService,

    *TipServiceComponent上添加TipService.

 5_9

图5_9 

5.6.        添加References

类似方法给component添加reference

    * RestaurantServiceComponent上添加menuService,

    * RestaurantServiceComponent上添加billService,

    * BillServiceComponent上添加vatService,

* BillServiceComponent上添加tipService

5_10

图5_10 

5.7.        wire referenceservice

palette上的wirereferenceservice连起来

RestaurantServiceComponent上的referencemenuService)连到MenuServiceComponentserviceMenuService),以此类推,把所有reference都连到相应service

 5_11

图5_11

5.8.        Promote service

       RestaurantServiceComponent上的serivceRestaurantServicepromotecomposite层,这样外面就可以调用这个application提供的功能。

       RestaurantService绿色图标上右键,选择promot

5_12

图5_12

 

5.9.        SCA Composite Model Editor打开Restaurant2.comosite,在Restaurant2.comosite 右键选open with

 5_13

 图5_13

Text Editor打开

 5_14

图5_14 

可以在这两个editor里编辑composite,会自动同步

 

5.10.     service定义java interface

把鼠标移到RestaurantServiceComponent上的service上,会自动有一个工具条出现,选择add JavaInterface

 5_15

图5_15

选中新添加的JavaInterface图标, properties中设置Interface的类名,包括packagerestaurant2.api.RestaurantService

 5_16

图5_16

以此类推给所有service添加JavaInterface

名字分别为restaurant2.api.MenuService

restaurant2.api.BillService

restaurant2.api.VatService

restaurant2.api.TipService

 5_17

图5_17

5.11.     component定义java implementation class的名字

鼠标移到RestaurantServiceComponent上,会自动有一个工具条出现,选择add JavaImplementation

 5_18

 

图5_18

选中新添加的JavaImplementation图标, properties中设置class,包括packagerestaurant2.lib.RestaurantServiceImpl

 5_19

 

图5_19

以此类推,给所有component添加JavaImplementation 类名分别为

restaurant2.lib.MenuServiceImpl

restaurant2.lib.BillServiceImpl

restaurant2.lib.VatServiceImpl

restaurant2.lib.TipServiceImpl

 5_20

图5_20 

5.12.     生成代码框架

首先在project上右键选择 Add SCA Nature

 5_21

图5_21

然后在Restaurant2.composite上右键 选择SCA>Generate Java Skeleton…

 5_22

图5_22

出现确认窗口,点ok

 5_23

图5_23

生成后项目结构是

 5_24

图5_24

生成的代码只有一个空壳,要在里面添加方法定义和实现。生成的代码的样子

 

 

 

 

5.13.     接口类的定义

  • Restaurant Service
 
     
     
  1. package restaurant2.api;  
  2.    
  3. public interface RestaurantService {   
  4.   Menu[] getMenus();  
  5.   double getBill(Menu menu);  
  6. }  
 
  • Menu Service
 
     
     
  1. package restaurant2.api;  
  2.    
  3. public interface MenuService {  
  4.   Menu[] getMenu();  
  5.   double getPrice(Menu menu);  
  6. }  
  • Bill Service
 
     
     
  1. package restaurant2.api;  
  2.    
  3. public interface BillService {  
  4.   double getBill(double menuPrice);  
  5. }  
  • Vat Service
 
     
     
  1. package restaurant2.api;  
  2.    
  3. public interface VatService {  
  4.   double getPriceWithVat(double price);  
  5. }  
  • Tip Service
 
     
     
  1. package restaurant2.api;  
  2.    
  3. public interface TipService {  
  4.   double getPriceWithTip(double price);  
  5. }  

添加DTO Menu:

 

  1. package restaurant2.api;  
  2.   
  3.    
  4.   
  5. import java.io.Serializable;  
  6.   
  7.    
  8.   
  9. public class Menu implements Serializable{  
  10.   
  11.       private int id;  
  12.   
  13.       private String details;  
  14.       public Menu(){}  
  15.   
  16.       public Menu(int idC, String detailsC) {  
  17.   
  18.         this.id = idC;  
  19.   
  20.         this.details = detailsC;  
  21.   
  22.       }  
  23.       public String printMenu() {  
  24.         return this.details;  
  25.       }  
  26.       public int getId() {  
  27.   
  28.         return this.id;  
  29.   
  30.       }  
  31.   
  32.     public String getDetails() {  
  33.   
  34.        return details;  
  35.     }  
  36.     public void setDetails(String details) {  
  37.   
  38.        this.details = details;  
  39.     }  
  40.   
  41.     public void setId(int id) {  
  42.        this.id = id;  
  43.     }    
  44.     
  45.   
  46. }  

 

5.14.        实现类
  • Restaurant Service
 
     
     
  1. package restaurant2.lib;  
  2.    
  3. import org.osoa.sca.annotations.Reference;  
  4. import org.osoa.sca.annotations.Service;  
  5.    
  6. import restaurant2.api.BillService;  
  7. import restaurant2.api.Menu;  
  8. import restaurant2.api.MenuService;  
  9. import restaurant2.api.RestaurantService;  
  10.    
  11. @Service(RestaurantService.class)  
  12. public class RestaurantServiceImpl implements RestaurantService {  
  13.    
  14.   private MenuService menuService;  
  15.   private BillService billService;  
  16.    
  17.   @Reference  
  18.   public void setMenuService(MenuService menuService) {  
  19.     this.menuService = menuService;  
  20.   }  
  21.    
  22.   @Reference  
  23.   public void setBillService(BillService billService) {  
  24.     this.billService = billService;  
  25.   }  
  26.    
  27.   public double getBill(Menu menu) {  
  28.     double menuPrice = this.menuService.getPrice(menu);  
  29.     return this.billService.getBill(menuPrice);  
  30.   }  
  31.    
  32.   public Menu[] getMenus() {  
  33.     return this.menuService.getMenu();  
  34.   }    
  35. }  
 
  • Menu Service

 

 

 
  • Bill service
 
     
     
  1. package restaurant2.lib;  
  2.    
  3. import org.osoa.sca.annotations.Reference;  
  4. import org.osoa.sca.annotations.Service;  
  5.    
  6. import restaurant2.api.BillService;  
  7. import restaurant2.api.TipService;  
  8. import restaurant2.api.VatService;  
  9.    
  10. @Service(BillService.class)  
  11. public class BillServiceImpl implements BillService {  
  12.    
  13.   private VatService vatService;  
  14.   private TipService tipService;  
  15.    
  16.   @Reference  
  17.   public void setVatService(VatService vatService) {  
  18.     this.vatService = vatService;  
  19.   }  
  20.    
  21.   @Reference  
  22.   public void setTipService(TipService tipService) {  
  23.     this.tipService = tipService;  
  24.   }  
  25.    
  26.   public double getBill(double menuPrice) {  
  27.     double pricewithTaxRate =   
  28.                    this.vatService.getPriceWithVat(menuPrice);  
  29.     double priceWithTipRate =   
  30.                    this.tipService.getPriceWithTip(pricewithTaxRate);  
  31.     return priceWithTipRate;  
  32.   }  
  33. }  
  • Vat Service

 

  • Tip service

 

5.15.      测试 Restaurant

创建一个新的测试类 Client。这个client会把Restaurant2发布到tuscany里面,然后调用promote出来的那个serviceinterface

 

 

5.16.        运行client:

class Client上右键选择【Run as > Java Application

运行结果为

5_25

图5_25

 到此一个简单的application就开发完了。后面会使用这个例子,结合SCA里面的概念仔细说明

  1. package test;  
  2.    
  3. import org.apache.tuscany.sca.host.embedded.SCADomain;  
  4.    
  5. import restaurant2.api.Menu;  
  6. import restaurant2.api.RestaurantService;  
  7.    
  8. public class Client {  
  9.    
  10.   public static void main(String[] args) throws Exception {  
  11.     SCADomain scaDomain = SCADomain.newInstance("Restaurant2.composite");  
  12.     RestaurantService restaurantService = scaDomain.getService(  
  13.     RestaurantService.class"RestaurantServiceComponent");  
  14.     Menu[] menus = restaurantService.getMenus();  
  15.     System.out.println("--- Menu ---");  
  16.     for (Menu m : menus) {  
  17.       System.out.println("- " + m.printMenu());  
  18.     }  
  19.     System.out.println();     
  20.     Menu menu = menus[3];  
  21.     System.out.println("My choice: " + menu.printMenu());  
  22.     System.out.println();  
  23.     double price = restaurantService.getBill(menu);  
  24.     System.out.println("Price (" + menu.printMenu() + "): " + price);  
  25.     scaDomain.close();  
  26.   }  
  27. }  

  1. package restaurant2.lib;  
  2.    
  3. import org.osoa.sca.annotations.Property;  
  4. import org.osoa.sca.annotations.Service;  
  5.    
  6. import restaurant2.api.TipService;  
  7.    
  8. @Service(TipService.class)  
  9. public class TipServiceImpl implements TipService {  
  10.    
  11.   @Property  
  12.   public double tipRate;  
  13.    
  14.   public TipServiceImpl(){  
  15.     this.tipRate=10;  
  16.   }  
  17.    
  18.   public double getPriceWithTip(double price) {  
  19.     return price * this.tipRate/100 + price;  
  20.   }   
  21. }  

  1. package restaurant2.lib;  
  2.    
  3. import org.osoa.sca.annotations.Service;  
  4.    
  5. import restaurant2.api.VatService;  
  6.    
  7. @Service(VatService.class)  
  8. public class VatServiceImpl implements VatService {  
  9.    
  10.   public double vatRate;  
  11.    
  12.   public VatServiceImpl(){  
  13.     this.vatRate=19.6;  
  14.   }  
  15.    
  16.   public double getPriceWithVat(double price) {  
  17.     return price * this.vatRate/100 + price;  
  18.   }    
  19. }  

  1. package restaurant2.lib;  
  2.   
  3.    
  4.   
  5. import org.osoa.sca.annotations.Init;  
  6.   
  7. import org.osoa.sca.annotations.Service;  
  8.   
  9.    
  10.   
  11. import restaurant2.api.Menu;  
  12.   
  13. import restaurant2.api.MenuService;  
  14.   
  15.    
  16.   
  17. @Service(MenuService.class)  
  18.   
  19. public class MenuServiceImpl implements MenuService {  
  20.   
  21.    
  22.   
  23.   private Menu[] menus;  
  24.   
  25.   private double[] prices;  
  26.   
  27.    
  28.   
  29.   @Init  
  30.   
  31.   public void init() {  
  32.   
  33.     this.menus = new Menu[] {  
  34.   
  35.             new Menu(0"Grilled hamburger with French fries" ),  
  36.   
  37.             new Menu(1"Roasted chicken with vegetables"),  
  38.   
  39.             new Menu(2"Duck breast in an orange sauce"),  
  40.   
  41.             new Menu(3"Duck foie gras & mango chutney") };  
  42.   
  43.     this.prices = new double[] { 10153550 };  
  44.   
  45.   }  
  46.   
  47.    
  48.   
  49.   public Menu[] getMenu() {  
  50.   
  51.     return this.menus;  
  52.   
  53.   }  
  54.   
  55.    
  56.   
  57.   public double getPrice(Menu menu) {  
  58.   
  59.     return this.prices[((Menu) menu).getId()];  
  60.   
  61.   }  
  62.   
  63. }   
  64.   public double getPrice(Menu menu) {  
  65.     return this.prices[((MenuImpl) menu).getId()];  
  66.   }  
  67. }  

  1. /* 
  2.  
  3.  * Generated from Restaurant2.composite on 06/17/10 at 16:37:42. 
  4.  
  5.  */  
  6.   
  7.    
  8.   
  9. package restaurant2.api;  
  10.   
  11.    
  12.   
  13. import org.osoa.sca.annotations.Remotable;  
  14.   
  15.    
  16.   
  17. @Remotable  
  18.   
  19. public interface BillService {  
  20.   
  21.    
  22.   
  23.        // TODO: add the service operations.  
  24.   
  25. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值