Skip to main content

JBoss BRMS - 自訂Domain的任務

那天在Present的時候,因為在當場Sample 流程裡面的任務都是用Drools,
因此有人問我jBPM 是否可以結合呼叫外部的Application 或是 Service.
當然可以啦。因為jBPM 5 開始,jBPM 它的Flow 跟 Drools 的 rules 結合得很緊密,
所以應該大部份的時候都不太需要這樣做啦。。。

=========================================================
請先學會怎麼自己建立Web Service. (連結)。
因為這篇的重點是在jBPM裡面去呼叫既有的Web Service.

因為BRMS 5.3 是使用 jBPM5, 也就是用BPMN 2.0, 聽說 BPMN 2.0 也有直接對應WebService 的定義,可是身為一個Java 的開發者,我比較喜歡用Java Code 去呼叫。
這樣又會更直覺一點。而且使用這個方式也可以舉一反三,不只是 Web Service, 一些其他的特定程式模組呼叫也可以使用這種方式。

在流程中的任務,加上自訂的WorkItem
=========================================================

1. 在Workspace 中按下滑鼠左鍵,建立一個jBPM 的專案。

2. 取名DemoProject,因為是要全新建立一個專案,請選擇建立空白(Empty)專案,



3. 左邊會出現剛剛建立的DemoProject, 到程式路徑下,的 ../resource 建立 META-INF 的資料夾,然後在 META-INF 資料夾裡面建立一個檔案 drools.rulebase.conf



4. 打開剛剛建立的drools.rulebase.conf檔案,在裡面定義我自定的 "任務定義檔" 位置。
drools.workDefinitions=MyDef.wid

5. 在與同一層的目錄下建立MyDef.wid檔案,裡面定義任務的內容,如 input, output, icon等....

6. 因為剛剛在icon那裡定義一個圖檔,所以要把圖檔放上去。放在../resource/icons 之下。

7. 這時我們的Task 已經定義好了,可以開始定義流程了,請先按下滑鼠右鍵,建立BPMN2 的流程。 這次取名叫demoprocess.


8.可以看到流程(Process) 的畫面的任務選項中,多了剛剛建立的自定任務。

9.這次流程比較簡單,開始node之後,直接就拉到結束。
在DemoWebService 上按兩下,在input的地方先寫入input內容。這次的內容很簡單,就是將個人的需求發送到Webservice上,Webservice 那邊再回應回來而已。
   

另外,想要在這個流程的一開始,寫一些log, 因此到Properties 的畫面,分別選擇 onEntryActions 與 onExitActions 上面寫System.out 的log. 


  

10. 開始建立我們的WorkItem, 先寫一個名叫 WebServiceWorkItem 的 Java Class,必須implement org.drools.runtime.process.WorkItemHandler.

11. 建立WebService Client 之後(請見先前的Webservice 建立),在剛剛的 WebServiceWorkItem 裡面呼叫WebService 即可。
package com.redhat.workitems;

import org.drools.runtime.process.WorkItem;
import org.drools.runtime.process.WorkItemHandler;
import org.drools.runtime.process.WorkItemManager;

import com.redhat.demows.client.DemoService;
import com.redhat.demows.client.DemoServiceService;

public class WebServiceWorkItem implements WorkItemHandler{

 @Override
 public void abortWorkItem(WorkItem arg0, WorkItemManager arg1) {
  System.out.println("ABORTING WORK ITEMS..........");
  
 }

 @Override
 public void executeWorkItem(WorkItem workitem, WorkItemManager workitemManager) {
  String name = (String)workitem.getParameter("name");
  String todoAction = (String)workitem.getParameter("todoAction");
  System.out.println(name+" 將要發送需求 :["+todoAction+"]");
 
  DemoServiceService service1 = new DemoServiceService();
     DemoService port1 = service1.getDemoServicePort();
     System.out.println("Server said: " + port1.doService(name,todoAction));
  workitemManager.completeWorkItem(workitem.getId(), null);
 }

}

這次的Webservice 也很簡單,加個字串,把資料還回去。
從wsdl自動建立的Client Code



12. 建立呼叫的程式,重點在於把我們建立的WebServiceWorkItem 註冊進去,
ksession.getWorkItemManager().registerWorkItemHandler("DemoWebServiceTask", new WebServiceWorkItem());
package com.redhat;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import com.redhat.workitems.WebServiceWorkItem;

/**
 * This is a sample file to launch a process.
 */
public class ProcessMain {

 public static final void main(String[] args) throws Exception {
  // load up the knowledge base
  KnowledgeBase kbase = readKnowledgeBase();
  StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
  
  ksession.getWorkItemManager().registerWorkItemHandler("DemoWebServiceTask", new WebServiceWorkItem());
  
  // start a new process instance
  ksession.startProcess("com.demo.tasks.webservice");
 }

 private static KnowledgeBase readKnowledgeBase() throws Exception {
  KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
  kbuilder.add(ResourceFactory.newClassPathResource("demoprocess.bpmn"), ResourceType.BPMN2);
  return kbuilder.newKnowledgeBase();
 }
 
}

13. 開始執行啦!這是執行剛剛寫的Processmain.java, 可以看到,Server 回應回來的結果。


Server 端也有收到我發送的request!

以上。

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 …

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 …

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 …