Invoking Multiple Operations via Request Box
In this tutorial, you define a data service that can invoke request box operations. The request box feature allows you to invoke multiple operations (consecutively) to a datasource using a single operation.
Setting up a datasource
Follow the steps given below to set up a MySQL database for this tutorial.
- Install the MySQL server.
- Download the JDBC driver for MySQL and copy it to your
<EI_HOME>/lib
directory. Create a database named Company.
CREATE DATABASE Company;
Create the Employees table:
USE Company; CREATE TABLE `Employees` (`EmployeeNumber` int(11) NOT NULL, `FirstName` varchar(255) NOT NULL, `LastName` varchar(255) DEFAULT NULL, `Email` varchar(255) DEFAULT NULL, `JobTitle` varchar(255) DEFAULT NULL, `OfficeCode` int(11) NOT NULL, PRIMARY KEY (`EmployeeNumber`,`OfficeCode`));
Define a data service to invoke request box operations
Let's create a data service using the Create Data Service wizard:
Start the WSO2 ESB profile.
- Open the ESB profile's Management Console using
https://localhost:9443/carbon
, and log in usingadmin
as the username and the password. - Click Create under Data Service.
Add a name for the data service.
Select the Enable Boxcarring check box.
As a result of selecting this check box, the Disable Legacy Boxcarring Mode check box will appear on your screen as shown below. Select this new check box (Disable Legacy Boxcarring Mode) as well.What is Boxcarring and Request Box?
Boxcarring is a method of grouping a set of service calls so that they can be executed as a group (i.e., the individual service calls will be executed consecutively in the specified order). Note that we have now deprecated the boxcarring method for data services. Instead, we have replaced boxcarring with a new request type called request_box.
Request box is simply a wrapper element (request_box), which wraps the service calls that need to be invoked. When the request_box is invoked, the individual service calls will be executed in the specified order, and the result of the last service call in the list will be returned. In this tutorial, we are using the request box to invoke the following two service calls:
- Add a new employee to the database
- Get details of the office of the added employee
When you click the Enable Boxcarring check box for the data service, both of the above functions (Boxcarring and Request box) are enabled. However, since boxcarring is deprecated in the product, it is recommended to disable boxcarring by clicking the Disable Legacy Boxcarring Mode check box.
- Click Next and then click Add New Datasource.
Connect to the Company database that you defined above.
Datasource ID Datasource Datasource Type RDBMS Datasource Type (Default/External) Leave Default selected. Database Engine MySQL Driver Class com.mysql.jdbc.Driver
URL jdbc:mysql://localhost:3306/Company
User Name Enter your MySQL server's username. Password Enter your MySQL server's password.
If you have not assigned a password, keep this field empty.- Click Save and then click Next to go to the Queries screen.
Click Add New Query to specify the query details:
Enter addEmployeeQuery as the query ID.
Enter the following SQL dialect:
insert into Employees (EmployeeNumber, FirstName, LastName, Email,OfficeCode) values(:EmployeeNumber,:FirstName,:LastName,:Email,:OfficeCode)
Click Generate Input Mapping and input mappings will be generated automatically.
Click Save.
Click Add New Query to Create another query for the Company datasource:
Enter selectEmployeebyIDQuery as the query ID.
Enter the following SQL dialect:
select EmployeeNumber, FirstName, LastName, Email, OfficeCode from Employees where EmployeeNumber=:EmployeeNumber
Generate input and output mappings:
Click Generate Input Mapping and input mappings will be generated automatically for the employee number.
Click Generate Response to automatically generate the output mappings for the fields in the Employees table.
Click Save.
Click Next and then click Add New Operation.
Create two operations to add and select employees.
Enter the following details to create the addEmployeeOp operation that adds new employees to the database.
Operation Name addEmployeeOp
Query ID addEmployeeQuery
- Save the operation.
Click Add New Operation and enter the details as shown below to create the selectEmployeeOp operation that gets the details of an employee from the database.
Operation Name selectEmployeeOp
Query ID selectEmployeebyIDQuery
Click Save.
Click Finish.
Invoking the data service
The Deployed Services window allows you to manage data services. You can try the data service you created by using the TryIt tool in this screen, which is in your product by default.
- In the Deployed Services screen, click Try this Service to open the data service you just created from the TryIt tool.
You will find that there is an additional request_box operation. - Select the request_box operation and enter the following values:
Enter values for the addEmployeeQuery query.
Enter the same employee number you used for the above query in the selectEmployeebyID query:
Copy the following, paste it in the TryIt tool, and click Send to try it out:
<body> <p:request_box xmlns:p="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <addEmployeeOp xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1003</xs:EmployeeNumber> <!--Exactly 1 occurrence--> <xs:FirstName xmlns:xs="http://ws.wso2.org/dataservice">Chris</xs:FirstName> <!--Exactly 1 occurrence--> <xs:LastName xmlns:xs="http://ws.wso2.org/dataservice">Sam</xs:LastName> <!--Exactly 1 occurrence--> <xs:Email xmlns:xs="http://ws.wso2.org/dataservice">chris@sam.com</xs:Email> <!--Exactly 1 occurrence--> <xs:OfficeCode xmlns:xs="http://ws.wso2.org/dataservice">1</xs:OfficeCode> </addEmployeeOp> <!--Exactly 1 occurrence--> <selectEmployeeOp xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1003</xs:EmployeeNumber> </selectEmployeeOp> </p:request_box> </body>
The following events take place once you click Send:
- First, the addEmployeeOp operation will be executed. The data will be successfully added to the database.
Secondly, the selectEmployeeOp operation will be executed and the details of the office attached to the employee will be returned in the result as shown below.
You get the following response:
<axis2ns26:DATA_SERVICE_REQUEST_BOX_RESPONSE xmlns:axis2ns26="http://ws.wso2.org/dataservice"> <Entries xmlns="http://ws.wso2.org/dataservice"> <Entry> <EmployeeNumber>1003</EmployeeNumber> <FirstName>Chris</FirstName> <LastName>Sam</LastName> <Email>chris@sam.com</Email> <OfficeCode>1</OfficeCode> </Entry> </Entries> </axis2ns26:DATA_SERVICE_REQUEST_BOX_RESPONSE>