Sometimes
it requires to execute some backend code on regular interval or at some
specific time.
For example : wants to send mail to user on
regular interval or at specific time.
Liferay has provided feature of scheduler for achieving this kind of
requirement.
For scheduling we are using two types of trigger:
1. Simple Trigger (when ever scheduling requirement
is based on regular interval basis, will use simple trigger)
2. Cron Trigger
(Whenever scheduling requirement is based on some specific time, will
use cron trigger)
Simple Trigger Scheduler Steps
Step 1: Create one liferay project of portlet
plugin type, in our example project name is Scheduler, (suffix portlet will automatically added
after project name) so after creation project complete name will be Scheduler-portlet
Inside that Scheduler-portlet project create one
portlet , in our example portlet class name is Schedule.
Step 2: Create one class name SimpleTrigger inside docroot/web-inf-src/com/test/ ,
after class creation complete path will be
docroot/web-inf-src/com/test/SimpleTrigger
This simpleTrigger class will implements MessageListener
Below is
the complete code of SimpleTrigger
class.
package
com.test;
import
com.liferay.portal.kernel.messaging.Message;
import
com.liferay.portal.kernel.messaging.MessageListener;
import
com.liferay.portal.kernel.messaging.MessageListenerException;
public class SimpleTrigger implements MessageListener {
public void receive(Message arg0) throws
MessageListenerException {
//any logic whatever you want to
execute, you can write here.
System.out.println("simple
trigger scheduler example...");
}
}
Step 3: Paste below code in portlet view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p style="border:1px solid green;">Liferay Scheduler</p>
<p>Simple
Trigger Scheduler, Trigger every 1 min interval</p>
Step 4: In
liferay-portlet.xml define scheduler entry like below
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC
"-//Liferay//DTD Portlet Application
6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>schedule</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
<scheduler-event-listener-class>com.test.SimpleTrigger</scheduler-event-listener-class>
<trigger>
<simple>
<simple-trigger-value>1</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>schedule-portlet</css-class-wrapper>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power
User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>
*In above scheduler
entry we have used
<scheduler-event-listener-class>com.test.SimpleTrigger</scheduler-event-listener-class>
for defining
scheduler event listener class path.
<trigger></trigger>
using trigger tag we
have defined which trigger we are going to use simple/cron, in this example we
are using simple trigger.
in <time-unit> tag we can give
hour,day,week value also.
*In this simple trigger example , for every 1 min
of regular interval code written inside receive method will execute.
Output :
Cron Trigger Scheduler Steps
sometime, we may need that scheduler job run at specific time [say 11 :20PM, each tuesday ] job should run. Trigger specified in above code(simple trigger) will not work in that case. For this type of requirement we can specify cron trigger.
Step 1: Create one liferay project of portlet
plugin type, in our example project name is Scheduler, (suffix portlet will automatically added
after project name) so after creation project complete name will be Scheduler-portlet
Inside that Scheduler-portlet project create one
portlet , in our example portlet class name is Schedule.
Step 2:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p style="border:1px solid green;">Liferay Scheduler</p>
<p>Cron Trigger Scheduler, Trigger every year,every month,every day,every hour, every minute on 10's second</p>
Step 2:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p style="border:1px solid green;">Liferay Scheduler</p>
<p>Cron Trigger Scheduler, Trigger every year,every month,every day,every hour, every minute on 10's second</p>
Step 3: Create one new class name CronTrigger inside docroot/web-inf-src/com/test/ ,
after class creation complete path will be
docroot/web-inf-src/com/test/CronTrigger
This CronTrigger class will implements MessageListener
Below is
the complete code of CronTrigger
class.
package com.test;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
public class CronTrigger implements MessageListener {
public void receive(Message arg0) throws MessageListenerException {
System.out.println("Cron trigger scheduler example...");
}
}
package com.test;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
public class CronTrigger implements MessageListener {
public void receive(Message arg0) throws MessageListenerException {
System.out.println("Cron trigger scheduler example...");
}
}
Step 4: Define scheduler entry for cron trigger in liferay-portlet.xml
<trigger>
<cron>
<cron-trigger-value>10 * * ? * * *</cron-trigger-value>
</cron>
</trigger>
[above cron text specify : every
year,every month,every day,every hour, every minute on 10's second]
MIN HOUR DAYOFMONTH MONTH DAYOFWEEK YEAR
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
"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
complete liferay-portlet.xml code:
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>schedule</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
<scheduler-event-listener-class>com.test.CronTrigger</scheduler-event-listener-class>
<trigger>
<cron>
<cron-trigger-value>50 * * ? * * *</cron-trigger-value>
</cron>
</trigger>
</scheduler-entry>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>schedule-portlet</css-class-wrapper>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>
Output :
0 comments:
Post a Comment