2014/05/07
7 May, 2014

Real-time loan processing with WSO2 CEP 3.1 - Part 2

  • Mohanadarshan Vivekanandalingam
  • Technical Lead - WSO2
Archived Content
This article is provided for historical perspective only, and may not reflect current conditions. Please refer to relevant product page for more up-to-date product information and resources.

Table of contents

  • Scenario 2 - Loan payment processing
  • How to test the scenario
  • Conclusion and expected outcome

Scenario 2 - Loan payment processing

In this scenario we are going to use loanPaymentStream as the input stream for processing loan payments. Events are sent to this stream via http requests. Then we’ll process those events in Siddhi and send SMS notifications to the user. Each step on how to configure this scenario will be explained below.

  1. Create an input event adaptor
  2. Since the CEP is going to receive http requests a http event adaptor needs to be created.

    For more information refer to https://docs.wso2.org/display/CEP310/Input+HTTP+Event+Adaptor

  3. Create a necessary output adaptor
  4. We need to create an email adaptor called ‘emailAdaptor’.

    Refer to https://docs.wso2.org/display/CEP310/Output+Email+Event+Adaptor for more info.

  5. Create a flow
  6. Now, we have created all the necessary input and output adaptors for this scenario so we can create a complete flow to process the loan payment events.

    An execution plan UI can be created and the complete flow can be generated there.

    • First, provide a name for the execution plan and fill out other relevant details which are required (You can refer to https://docs.wso2.org/display/CEP310/Configuring+Execution+Plans for more info)
    • Then click on the “Create Stream Definition” option in the import Stream drop down list. You will then see an interface like the one shown below. In this form, the user needs to enter input event stream related details like event stream name, version and stream attribute information.
    • Event Stream Name : loanPaymentStream

      Event Stream Version : 1.0.0

      Payload Attributes : accountNo (string) & paidAmount (double)

      After entering the above information, click on “Add Event Stream”. Then you will see an info dialog box confirming the stream creation.

      Then click on the “OK” button in the dialog box. Then you will see another confirmation dialog box to create event builders.

      Here, you have two options

      • Default WSO2Event Builder - The CEP will create an event builder with DefaultWSO2EventAdaptor, WSO2Event mapping and custom mapping disabled.
      • Custom Event Builder - You can define necessary mapping for the builder and use any event adaptor type.
    • Select the “Custom Event Builder” option and click on “OK”. Then you will able to see an interface like the one shown below.
    • Here, you need to enter necessary details that are required for event builder creation.

      Event Builder Name : loanPaymentEventBuilder

      Input Event Adaptor Name : httpEventAdaptor (which created in 1st step)

      Topic : loanPayment

      Now we need to add the mapping related information. Here, we are going to send the event as xml format via http. Then you add the prefix and namespace and click on “Add” button.

      Prefix : loan

      Namespace : https://samples.wso2.org/

      Sample XML event :

        
      
              
                    AC67536
                    5000
              
      
      

      Then add the necessary Xpath values for the event attributes. The XPath will be defined based on the event format above.

      accountNo XPath - //loan:loanPayment/loan:accountNo

      paidAmount XPath - //loan:loanPayment/loan:paidAmount

      After adding necessary values click on “Add Event Builder”. This will create the event builder.

      When you create an event builder with http adaptor the CEP will create an http endpoint with the below syntax (without port offset);

      https://localhost:9443/endpoints/(adaptor_name)/(topic_name) or

      https://localhost:9763/endpoints/(adaptor_name)/(topic_name)

      In this case it should be https://localhost:9443/endpoints/httpEventAdaptor/loanPayment and https://localhost:9763/endpoints/httpEventAdaptor/loanPayment

    • After creating the stream and event builder we need to write the necessary Siddhi query. Before writing it we need to import necessary input event streams.
    • Here we need to to import loanPaymentStream:1.0.0 as an input stream. Since we don’t have stream versioning in Siddhi level, we need to provide an alias and import it.

      Then we can write the below Siddhi query,

        
      define table clientLoanTable (clientAccountNo string, toalLoanAmount double, totalAmountDue double) from ('datasource.name'='mysql_datasource', 'database.name'='clientDB', 'table.name'='clientLoanTable');
      
      define table clientInfoTable (clientName string, clientAccountNo string, clientEmailAddress string) from ('datasource.name'='mysql_datasource', 'database.name'='clientDB', 'table.name'='clientInfoTable');
      
      from clientLoanTable as clientLoanInfoStream
      join loanPaymentStream on clientLoanInfoStream.clientAccountNo == loanPaymentStream.accountNo
      select clientLoanInfoStream.clientAccountNo as clientAccountNo ,clientLoanInfoStream.toalLoanAmount as toalLoanAmount , (clientLoanInfoStream.totalAmountDue - loanPaymentStream.paidAmount) as totalAmountDue
      insert into filteredPaymentStream;
      
      from filteredPaymentStream
      update clientLoanTable 
      on clientLoanTable.clientAccountNo == filteredPaymentStream.clientAccountNo;  
      
      from clientInfoTable as clientInfoStream 
      join filteredPaymentStream on  clientInfoStream.clientAccountNo == filteredPaymentStream.clientAccountNo
      select clientInfoStream.clientName,  clientInfoStream.clientEmailAddress , filteredPaymentStream.clientAccountNo , filteredPaymentStream.totalAmountDue
      insert into loanPaymentNotificationStream;
      
      

      After writing necessary Siddhi queries, we need to export necessary streams to output. Here we have exported “loanPaymentNotificationStream” as the output. So we can send email based on the events which are in the streams.

      When you select the “Create Stream Definition” option from the streamId dropdown you will see an interface as shown below;

      This window generates a UI with necessary attributes based on the Siddhi query. Here you need to simply press the “Add Event Stream” button and create the stream. After adding the stream you will be notified by a info dialog box saying “stream definition is added”. Then click on “OK”.

      Now you will see a confirmation dialog box as shown below;

      Select the “Custom Event Formatter” option and click on “OK” since you are going to write an event formatter with an email adaptor that sends email to the customer by confirming the payment.

      Then you will see a popup window like the one below;

      You need to select the created email adaptor from the dropdown and necessary values for other properties.

      Event Formatter Name : loanPaymentNotificationFormatter

      Output Event Adaptor : emailAdaptor

      Email Address : {{clientEmailAddress}}

      Subject : Loan Payment Received - {{clientAccountNo}}

Text mapping

Hi {{clientName}},

Dear valued customer, You loan payment has been received.

Your current loan due is {{totalAmountDue}}

If you have any concerns or questions please contact Direct Loan Customer Services at telephone number (27) 456789 from 8 am to 5 pm, Monday to Friday. We look forward to the completion of your loan.

Thanks,

Bank of Trust

If you want to you can use the SMS output of the CEP and send events as SMSs to the client. To configure SMS output follow the doc link

https://docs.wso2.org/display/CEP310/Output+SMS+Event+Adaptor

After completing the configuration, you will see a flow like the one shown below (You can go to “Monitor” view of the CEP and select “Event Flow”);

How to test the scenario

Use case 1

To test the first use case, you can send an email from a mail client to an email address that you define. In this example, the receiver mail address is [email protected]. The sender email address needs to be there in the “clientInfoTable” table and the “accountInfoTable” table.

Sample Email format :

Hi,

I am hereby requesting for a personal loan. Please kindly process my request.

I am enclosing photocopies of my salary slips, pan card number and other relevant documents with this mail for your perusal. If you have any concerns please feel free to contact me on my mobile number or email address.

I will be awaiting a positive reply from you.

Thanks & Regards,

Mohan

Use case 2

You can send a message as a http request using any rest client. We have sent the event as a cUrl request as mentioned below

curl -v -k -d @payload.xml --user admin:admin --header "Content-Type:application/xml" https://localhost:9443/endpoints/httpEventAdaptor/loanPayment

Payload.xml contains the below xml event,

  

        
              AC67536
              5000
        

Conclusion and outcome

You now know how to create a complete process flow in WSO2 CEP. We have introduced many new features to it like the http event adaptor, event tables, event flow and much more. The functionality of the latest WSO2 CEP and the way it works with WSO2 Enterprise Service Bus can be very useful for any enterprise solution that requires analysis over time such as fraud detection, sales, shipping and other such business events.

 

About Author

  • Mohanadarshan Vivekanandalingam
  • Technical Lead
  • WSO2