...
This sample demonstrates the stateful behavior of a rule session through a simple banking application which uses business rules. This sample should be deployed in a stateful axis2 session - i.e. session scope or transport scope.
Info |
---|
Before executing this sample, it is recommended that you refer Exposing Rules as Services which explains in detail the process of writing and deploying a business rule. |
...
Sample Configuration
Table of Contents | ||||
---|---|---|---|---|
|
Sample Rule Definition
Rules
Rule 1: Withdrawing is only allowed if the account balance is higher than the requested amount.
Rule 2: 5% credits given if the deposit is higher than 1000.
Facts
There are two facts - ": A customer made a deposit request
" and "A customer made a withdraw request
".
Code Block | ||
---|---|---|
| ||
package samples.banking; /** * Account */ public class Account { private String accountNumber; private int balance; public String getAccountNumber() { return accountNumber; } public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; } public int getBalance() { return balance; } public void setBalance(int balance) { this.balance = balance; } public void increment(int value) { balance += value; } public void decrement(int value) { balance -= value; } } package samples.banking; /** * Deposit fact */ public class Deposit { private String accountNumber; private int amount; public String getAccountNumber() { return accountNumber; } public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } package samples.banking; /** * DepositAccept facts */ public class DepositAccept { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } package samples.banking; /** * Withdraw fact */ public class Withdraw { private String accountNumber; private int amount; public String getAccountNumber() { return accountNumber; } public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } package samples.banking; /** * WithdrawAccept fact */ public class WithdrawAccept { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } package samples.banking; /** * WithdrawReject fact */ public class WithdrawReject { private String reason; public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } } |
Rule Service Configuration (service.rsl)
An in-line rule set is used within the service.rsl file as follows.
...
Deploying and Testing the Service
1.- Deploy the rule service through the BRS management console. You can follow either of the two methods:
- Bundle all artifacts in an .aar file and upload it (Rule Service -> Upload menu).
- Create using the Rule Service wizard UI (Rule Service -> Create menu).
The above steps are discussed in detail in section Exposing Rules as Services.
- After deployment, click on List under Services in the main tab of the management console. The service will appear in
- the Deployed Services
3. In the Client Operations
widget of the dashboard, invoke the Try-it Tool to test the service.
- page.
- Click Banking Service to access the dashboard of the service.
- Click Try this service in the Client Operations widget of the dashboard to invoke the Try-it tool.
In the service's Try-it window, issue a request similar to the following. As the first step, we do a deposit followed by funds withdrawal.
Make a Deposit:
Code Block | ||
---|---|---|
| ||
<amount>2432</amount> <accountNumber>330021vc</accountNumber> |
Make a withdrawal:
Code Block | ||
---|---|---|
| ||
<amount>200</amount> <accountNumber>330021vc</accountNumber> |
Client Operations
widget of the dashboard to invoke the service. A client using generated stub codes is shown below where the codes were generated with option "Unpacks the databinding classes".
Code Block | ||
---|---|---|
| ||
package org.wso2.carbon.samples; import org.apache.axis2.AxisFault; import org.wso2.carbon.samples.bankingService.deposit.Deposit; import org.wso2.carbon.samples.bankingService.deposit.DepositAccept; import org.wso2.carbon.samples.bankingService.deposit.DepositE; import org.wso2.carbon.samples.bankingService.stub.BankingServiceStub; import org.wso2.carbon.samples.bankingService.withdraw.*; import java.rmi.RemoteException; public class BankingServiceTestCase { public static void main(String[] args) { try { BankingServiceStub bankingServiceStub = new BankingServiceStub("http://localhost:9763/services/BankingService"); bankingServiceStub._getServiceClient().getOptions().setManageSession(true); DepositE depositRequest = new DepositE(); Deposit deposit = new Deposit(); deposit.setAccountNumber("070229x"); deposit.setAmount(1000); depositRequest.addDeposit(deposit); Deposit[] deposits = new Deposit[1]; deposits[0] = deposit; depositRequest.setDeposit(deposits); DepositAccept[] results = bankingServiceStub.deposit(deposits); String result = results[0].getMessage(); System.out.println(result); WithDrawE withDrawRequest = new WithDrawE(); Withdraw withdraw = new Withdraw(); withdraw.setAccountNumber("070229x"); withdraw.setAmount(500); Withdraw[] withdraws = new Withdraw[1]; withdraws[0] = withdraw; withDrawRequest.setWithDraw(withdraws); WithDrawRespone withDrawRespone = bankingServiceStub.withDraw(withdraws); WithdrawAccept[] withdrawAccepts = withDrawRespone.getWithdrawAccept(); WithdrawReject[] withdrawRejects = withDrawRespone.getWithdrawReject(); String resultWithDraw = withdrawAccepts[0].getMessage(); System.out.println(resultWithDraw); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } } |
...