How to Write a Simple G-Reg Lifecycle Executor to send mails when triggered using WSO2 G-Reg 5 Series.

When its comes to SOA governance lifecycle management aka LCM is a very useful feature. Recently I wrote a post about lifecycle checkpoints which is again another useful feature that comes with WSO2 G-Reg 5 series. Before start writing an LC executor you need to have basic knowledge of G-Reg LCM, lifecycle syntax and good knowledge in java.
In this sample, I will create a simple lifecycle that has 3 states which are Development, Tested, and Production. In each state, I will call the LC executor and send mails to the defined list in the lifecycle.

To get a basic idea about WSO2 G-Reg Lifecycles please go through Lifecycle Sample documentation. If you have a fair knowledge about LC tags and attributes you can straightaway add the first checkpoint to LC. You can find the official G-Reg documentation for this from here.

First let's start off with writing a simple lifecycle call EmailExecutorLifeCycle, In the LC there is only below things that you need to consider if you have basic knowledge in WSO2 LCs

<state id="Development">
   <datamodel>
     <data name="transitionExecution">
       <execution forEvent="Promote" class="org.wso2.lc.executor.sample.executors.EmailExecutor">                             <parameter name="email.list" value="thusharak@wso2.com"/>                  </execution>
     </data>
   </datamodel>   
                    <transition event="Promote" target="Tested"/>   
</state>

Let's explain above bold syntax.
<datamodel> : This is where we define additional data models that need to be executed in a state change.

<data name="transitionExecution"> : Within this tag we define general stuff that need to be executed subsequent to the state change. Like wise we have transitionValidation, transitionPermission, transitionScripts, transitionApproval, etc..

forEvent="Promote" : This defines for which event this execution need to happen. In this case, it is promote.

class="org.wso2.lc.executor.sample.executors.EmailExecutor" : Class path of the executor, we will be discussing this file later. For the record this jar file need to be added to the  dropins or libs directory which is located in repository/components/.

name="email.list" : This is a custom parameter that we define for this sample. Just add some valid emails for the sake of testing.

<transition event="Promote" target="Tested"/> : Transition actions available for the state, there can be 0 to many transitions available for the state. In this case, it is promote an event for the Tested state.


Please download the EmailExecutorLifeCycle.xml and apply it using G-Reg mgt console.

Since we are sending a mail using our executor we need to fill email settings of the mail admin. Please go to axis2.xml which is located in repository/conf/axis2/. Now in that XML file uncomment mail transport sender codes. Please find the sample settings for gmail transport sender.

<!-- To enable mail transport sender, ncomment the following and change the parameters
         accordingly-->
<transportSender name="mailto"
                     class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.from"><username@gmail.com></parameter>
        <parameter name="mail.smtp.user"><username@gmail.com></parameter>
        <parameter name="mail.smtp.password"><password></parameter>
        <parameter name="mail.smtp.host">smtp.gmail.com</parameter>

        <parameter name="mail.smtp.port">587</parameter>
        <parameter name="mail.smtp.starttls.enable">true</parameter>
        <parameter name="mail.smtp.auth">true</parameter>
</transportSender>

Please fill the bold fields with correct values.

If you have enabled 2 step verification in your Gmail account you have to disable that first. Please follow these steps:

 1. Login to Gmail. 
 2. Go to Gmail security page.
 3. Find 2 step verification from there. open it.
 4. Select "Turn off".

Lets download and save the org.wso2.lc.executor.sample-1.0.0.jar file in <GREG_HOME>/repository/components/libs/. Then do a restart.

Now it's time to see the executor in action,

1. Add the EmailExecutorLifeCycle.xml to any metadata type rxt such as soapservice, restservice, etc... using the mgt console.
2. Login to the publisher and from the lifecycle tab promote the LC state to next state.

3. Now check the inbox of any mail ID that you included in email.list.


You can download the source code from here.

Comments

Post a Comment