Monday 17 June 2013

How to write Simple/Cron Scheduler in Liferay ?

Many time, it requires to execute some code on regular interval or at some specific time.

For example : mail for reminders, weekly subscriptions, deleting unused data, generating reports.

Liferay comes with very easy implementation strategy for such requirements with scheduler.

In Liferay portlet, one can write scheduler easily.


1.  Create one java class in your portlet which implements    com.liferay.portal.kernel.messaging.MessageListener interface.

 public class Scheduler implements MessageListener {  
      public void receive(Message arg0) throws MessageListenerException {  
       // TODO Auto-generated method stub : your job code  
       //Write code : what you want to execute on timely basis.  
   }  
   }  


The code written in receive method would be executes on time basis.This class will receive
a message at a regular interval specified by the trigger element[liferay-portlet.xml].

In liferay-portlet.xml provide this class entry as shown below.


2.  Job Schedule timing can be configured in liferay-portlet.xml by trigger element as below.
  <scheduler-entry>  
     <scheduler-event-listener-class>com.example.schedule.Scheduler</scheduler-event-listener- class>  
        <trigger>  
         <simple>  
         <simple-trigger-value>1</simple-trigger-value>  
         <!-- this would be some number,-->  
         <!--Based in above configurations scheduler will run in every one minute.-->  
         <time-unit>minute</time-unit>  
          <!-- time unit can be day","hour", "minute", "second", or "week"-->  
         </simple>  
       </trigger>  
   </scheduler-entry>  


You can either provide <simple-trigger-value> or <property-key>.

If you provided property-key like <property-key>interval</property-key>, the property-key value specifies a property key that will be queried from portal.properties or portlet.properties to create a trigger. For Example: if you provide <property-key>interval</property-key> and provide interval=3 in portlet.properties then job will run after each 3 minutes.


But sometime, you may need that scheduler job should not just run after particular interval, but you need that at specific time [say 11 :20PM, each tuesday ] job should run. Trigger specified in above code will not work in that case. You can specify cron trigger for such scenario.

 <trigger>  
         <cron>  
           <cron-trigger-value>50 * * ? * * *</cron-trigger-value>  
         </cron>  
 </trigger>   


[above cron text specify : every year,every month,every day,every hour, every minute on 50's second]

You can also use property-key instead cron-trigger-value.

cron-trigger-value /property value should be in cron-text format :

 MIN HOUR DAYOFMONTH MONTH DAYOFWEEK YEAR
Some Example Cron-text:
"0 0 12 * * ? *"                    Fire at 12pm (noon) every day
"0 15 10 ? * * *"                  Fire at 10:15am every day
"0 10,44 14 ? 3 WED *"     Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
"0 15 10 ? * MON-FRI *"  Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday

NOTE: Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on one of these fields).

Thus, you can easily write your custom scheduler in Liferay.
Example code available at exampleScheduler-portlet

Enjoy Learning!









10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Is it possible to set value from properties file?

    ReplyDelete
  3. Yes, portlet.properties or portal-ext.properties will work as mentioned in blog

    ReplyDelete
    Replies
    1. can we perform export data from mysql DB & import data to MS SQL.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This plugin is very cool,but recently I have meet the issue.when I use this tag(cron) to run the job at the specific time,but it did't add to the job management,so what can I do for this issure,thank you

      Delete
  5. hi, i new to liferay scheduler, can we mention particular date as per the user.

    ReplyDelete