Sending a Simple Message to a Datasource
com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links' is unknown.

Sending a Simple Message to a Datasource

Let’s try a simple scenario where a patient makes an inquiry specifying the doctor's specialization (category) to retrieve a list of doctors that match the specialization. The required information is stored in an H2 database that is shipped with this product. We will create a data service in the ESB profile of WSO2 Enterprise Integrator (WSO2 EI), which will expose the information in the database, thereby decoupling the client and the database layer in the back end. The client will then communicate with the data service hosted in WSO2 EI to get the required information instead of communicating directly with the back end.

 

In this tutorial, we will define a data service in the ESB profile of WSO2 EI to expose the back-end database. A client can then invoke the data service to send messages to the database. If you want to use a back-end service instead of a database, see the tutorial on sending a simple message to a service.

Let's get started!

This tutorial includes the following sections:

Downloading and set up WSO2 EI

  1. Install Oracle Java SE Development Kit (JDK) version 1.8.* and set the JAVA_HOME environment variable.

  2. Download the product installer from here, and run the installer.

Let's set up a back-end database for the healthcare service. We will create a database named DATA_SERV_QSG in the <EI_HOME>/samples/data-services/database directory for this purpose.

  1. Download the dataServiceSample.zip file and extract it to a location on your computer. Let's call this location <Dataservice_Home>. This contains a DB script for updating the back-end database with the channeling information of the healthcare service.

  2. Open a terminal, navigate to the <Dataservice_Home> directory and execute the following command:

    ant -Ddshome=<PATH_TO_EI_HOME>

The database is now updated with information on all available doctors in the healthcare service.

Starting WSO2 EI

Follow the steps given below to start the ESB runtime of WSO2 EI and create the data service"

  1. Start the ESB profile:

  1. In your Web browser, navigate to the ESB profile's management console using the following URL:  https://localhost:9443/carbon/.

  2. Log into the management console using the following credentials:

    • Username: admin

    • Password: admin

Exposing a datasource through a data service

Now, let's start creating the data service using the management console.

  1. On the Main tab, click Create under Data Service

  2. In the Create Data Service screen, enter DOCTORS_DataService as the data service name, and click Next.

  3. Click Add New Datasource and enter the following details:

  4. Click Save and then click Next to start defining a query.

  5. Now, let's start writing a query to get data from the datasource. 
    First, you need to define a query to get the details of all the available doctors from the database.

    1. Click Add New Query to open the Add New Query screen.

    2. Enter the following values:

    3. Click Generate Response to automatically create mappings for the fields.

    4. Under the Result (Output Mapping) section, change the values of the following fields.

      The output mapping will be as shown below.

  6. Click Save.

  7. Now, let's create another query that can get the doctor information based on specialization.

    1. Click Add New Query to open the Add New Query screen.

    2. Enter the following values:

    3. Click Generate Input Mappings and a new input mapping record will be created. 

    4. Edit the record and change the Mapping Name to SPECIALITY, and click Save. You will now have the following input mapping:

    5. Click Main Configuration, to go back to the main configuration after adding the input mapping.

    6. Click Generate Response to automatically create mappings for the fields. 

    7. Under the Result (Output Mapping) section, change the values of the following fields

      The output mapping will be as shown below.

  8. Click Save and then click Next to open the Operations screen.
    Since we are exposing the data as a REST resource, we do not need an operation. An operation is needed only if you are exposing the data as a SOAP operation.

  9. Click Next to open the Resources screen.

    1. Click Add New Resources to open the Add Resources screen. 
      Let's first create a resource to invoke the select_all_DOCTORS_query:

    2. Click Save to save the resource.

  10. Now, let's create another resource to invoke the select_DOCTORS_from_SPECIALITY_query.

    1. Click Add New Resources to open the Add Resources screen and enter the following details:

    2. Click Save to save the resource.

  11. Click Finish after you have defined the resources to complete the data service creation process. You are now taken to the Deployed Services screen, which shows all the data services deployed on the server including the one you created. 

Sending requests to the ESB

Let's send a request to the data service, which is now deployed in the ESB profile of WSO2 EI. You will need a REST client like curl for this.

  1. Open a command line terminal and enter the following request to get the information of all the doctors specializing under surgery.

    curl -v http://localhost:8280/services/DOCTORS_DataService/getDoctors?SPECIALITY=surgery
  2. You see the following response message from data service with a list of all available doctors and relevant details.

    <DOCTORSLIST xmlns="http://ws.wso2.org/dataservice"> <DOCTOR> <NAME>thomas collins</NAME> <HOSPITAL>grand oak community hospital</HOSPITAL> <SPECIALITY>surgery</SPECIALITY> <AVAILABILITY>9.00 a.m - 11.00 a.m</AVAILABILITY> <CHARGE>7000</CHARGE> </DOCTOR> <DOCTOR> <NAME>anne clement</NAME> <HOSPITAL>clemency medical center</HOSPITAL> <SPECIALITY>surgery</SPECIALITY> <AVAILABILITY>8.00 a.m - 10.00 a.m</AVAILABILITY> <CHARGE>12000</CHARGE> </DOCTOR> <DOCTOR> <NAME>seth mears</NAME> <HOSPITAL>pine valley community hospital</HOSPITAL> <SPECIALITY>surgery</SPECIALITY> <AVAILABILITY>3.00 p.m - 5.00 p.m</AVAILABILITY> <CHARGE>8000</CHARGE> </DOCTOR> </DOCTORSLIST>
com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.