Skip to main content

JBoss FSW 6 - Switch Yard 小程式

這篇本來更早就寫好想要放上來,可惜在Alpha版本不是很穩定的狀況下,我還是等到Beta 才把這篇放出來給大家看。

JBoss SOA 正式改名為 Fuse Service Work, 所以以後 FSW 就是SOA拉~ 在這裡正名一下。
前些日子本來很想寫這篇,現在終於可以補上來了。

建議你在開始這個小程式之前,先去看一下SwitchYard 的基礎介紹。按這裡
安裝工具,與JBoss FSW 因為Beta版的installer 還有在改東西,所以我等他們改好以後再放好了。(有空再寫 :p 忙忙忙忙忙... )

首先,按下滑鼠右鍵,建立一個SwitchYard專案。


輸入這個Project的名字。

選擇FSW 的版本,與裡面會用到元件。
(PS, 我抓圖的時候還在Alpha, 但是Final ㄧ)



在一個SwitchYard 的專案中的META-INF 會有一個重要的檔案,switchyard.xml,這個檔案就是整個AP的定義檔,例如說,從哪裡會有Data 近來,送到哪裡處理,以及處理完的資料要送到哪裡等等... 在 JBoss Developer Studio 裡面,可以透過GUI的工具看到這個定義的內容,然後也可以拖拉的把資料加入。

現在開始寫拉,把Bean 也就是主要的Java程式邏輯拖拉到Composite(組合)上。


當把Bean拖拉上去時,會自動跳出一個請你輸入Bean資訊的視窗,請點選 interface (這是一個合約).


輸入interface名稱後,按確認。它會自動幫你產生對應的Bean 還有 Service (合約)名稱。

按下確認後就可以在對應的Folder下看到產出的java檔案。


打開Payments.java, 這是一個合約的interface, 現在,請在裡面定義這個payments 合約可以有whopay這個動作。

Payments.java
package com.example.switchyard.ChristinaDemo;

public interface Payments {

 void whatInterestRate(Customer customer);
}

因為payments只是一個合約,實作的內容還是要寫在Bean裡面。

PaymentsBean.java
package com.example.switchyard.ChristinaDemo;

import org.switchyard.component.bean.Service;

@Service(Payments.class)
public class PaymentsBean implements Payments {

 @Override
 public void whatInterestRate(Customer customer) {
  if(customer.creditRating > 300){
      System.out.println("10%");
   
  }else{
   System.out.println("30%");
 
  }
 }

}


裡面有個Customer 的物件,如下:

Customer.java
package com.example.switchyard.ChristinaDemo;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

 int creditRating;
 boolean isVIP;
 
 public int getCreditRating() {
  return creditRating;
 }
 public void setCreditRating(int creditRating) {
  this.creditRating = creditRating;
 }
 public boolean isVIP() {
  return isVIP;
 }
 public void setVIP(boolean isVIP) {
  this.isVIP = isVIP;
 } 
 
}


以上三個Java 完成後,我們就完成一個元件服務,現在,就開始定義到底資料要如何送入這個服務中。請在你的compenent service上的綠色小箭頭上按下滑鼠右鍵。點選,promote service.


會自動跳出要promote成對外什麼服務的選擇視窗,我們要對外開放成Web Service, 所以請選WSDL.然後按下interface。


按下之後,它會自動跳出這次幫你建立Web Service WSDL 的儲存位置,請按Next 往下一步。


我喜歡把Web Service 名稱叫做XXXService, 所以就把service name 改成 PaymentService,按Finish 完成這個步驟。


這時它就會跳回去一開始我們想要建立Web Service 的 Promote Component Service 的畫面。請按下一步,



因為Web Service 收的是XML, 而我們收的是Java 物件,所以一定要做一個轉換資料的動作,一般來說,XML 轉物件選 JAXB 就可以了。但是這次我選Java Transformer, 這樣,以後有特殊需求,我可以自己改動轉換內容。按下一步繼續


之後他會跳出幫你自動建立轉換的Java程式 GUI 畫面,在Name 裡面填上PaymentTransformer即可。


之後,再打開的 PaymentTransformer 裡面,寫以下轉換的程式碼。
package com.example.switchyard.ChristinaDemo;

import org.switchyard.annotations.Transformer;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public final class PaymentTransformer {

	@Transformer(from = "{urn:com.example.switchyard:ChristinaDemo:1.0}whatInterestRate")
	public Customer transformWhatInterestRateToCustomer(Element from) {
		
		int creditRating = Integer.parseInt(from.getElementsByTagName("creditRating").item(0).getChildNodes().item(0).getNodeValue());
		boolean isVIP = Boolean.parseBoolean(from.getElementsByTagName("creditRating").item(0).getChildNodes().item(0).getNodeValue());
		System.out.println("creditRating:["+ creditRating+"]");
		System.out.println("isVIP:["+ isVIP+"]");
		
		Customer customer = new Customer();
		customer.setCreditRating(creditRating);
		customer.setVIP(isVIP);
		
		return customer;
	}
}
現在,基本上完成了,可是,我們剩最後一步,還沒有定義Web Service 是使用哪種通訊協定出去的,一般Web Service 都是走SOAP, 所以我們就把最左手邊的Binding SOAP 拖拉到剛剛建立的PayementService(Composite Service) 上。
這一步可以都照預設值拉~看你.. 
這樣就完成拉!!!!! 把剛剛做好的ChristinaDemo Project 丟到 FSW 的Server 上。
看到剛剛設定的 PayamentService 正確的啓動,就可以開始測試囉~
透過工具,發一個XML,裡面的Credit Rating 是 400 
Log 上寫 10% , 沒錯,跟我寫的邏輯是一樣的。
再發一個XML,裡面的Credit Rating 是 100
Log 上寫 30%,哇哈哈~正確無誤,收工!
以上。

Comments

Popular posts from this blog

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

RHTE - Supercharge your integration services

Red Hat Tech Exchange has taken place in Vietnam, Ho Chi Minh city two weeks ago, it is a great event held by Red Hat in Asia Pacific Region. It is open to all Red Hat partners who are interested in learning what Red Hat is doing recently, see what the trend of the open source world, basically it is a great event to share your knowledge and experience, to meet other enthusiastic people.

I am very fortunate to talk in this great event, to talk about the things I have been working on and even discuss it with many. Also got lots of great ideas too. So here are the slide.

My first talk was with Thomas Qvarnström about how to handle large size data in JBoss Fuse and how JBoss Data Grid can help in the situation.

Here is the agenda of the talk, we will be talk about this in the up coming webinar on 24th Sept.

Integration often involves storing, retrieving, and transforming data. Using a traditional database in your integration is likely to becomes a bottleneck that is expensive and hard to …

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …