Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Andrzej Drozdowski
Does anybody managed to schedule a job programmatically?
April 22, 2013 7:56 AM
Answer

Andrzej Drozdowski

Rank: Junior Member

Posts: 25

Join Date: September 3, 2010

Recent Posts

Hi,
I am repeating my question because sill cannot find a solution.
I would like to programmatically schedule a job for Liferay 6.1 on jboss. I want it runs at time and on weekdays specified by user. For example at 5AM on Mondays and Fridays.

If I add schedule entry to liferay-portlet.xml the job will run properly
 1
 2          <scheduler-entry>
 3            <scheduler-description>description</scheduler-description>
 4            <scheduler-event-listener-class>scheduler.test.ScheduledJob</scheduler-event-listener-class>
 5            <trigger>
 6                <cron>
 7                    <cron-trigger-value>0 0 5 ? * MON,FRI</cron-trigger-value>
 8                </cron>
 9            </trigger>
10        </scheduler-entry>


If I try to set the same cron trigger programmatically , it will not run. I cannot see any exception.

My listener class (I want to load the cron trigger value from parameters later.)
 1
 2public class ScheduledJob implements MessageListener {
 3
 4    private static final Log LOG = LogFactoryUtil.getLog(ScheduledJob.class);
 5
 6    public static SchedulerEntryImpl SCHEDULER_ENTRY = new SchedulerEntryImpl() {
 7        {
 8            setDescription("Job description");
 9            setTriggerType(TriggerType.CRON);
10            setTriggerValue(0 0 5 ? * MON,FRI");
11            setEventListenerClass(ScheduledJob.class.getName());
12        }
13    };
14
15    @Override
16    public void receive(Message message) throws MessageListenerException {
17        LOG.info("Scheduled RUNS !!!");
18    }
19}


And the portlet that sets the schedule:
 1
 2public class TestPortlet extends MVCPortlet {
 3
 4    private static final Log LOG = LogFactoryUtil.getLog(TestPortlet.class);
 5
 6    public void setSchedule(ActionRequest request, ActionResponse response) {
 7        try {
 8            ThemeDisplay themeDisplay = (ThemeDisplay) request
 9                    .getAttribute(WebKeys.THEME_DISPLAY);
10            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
11            String portletId = portletDisplay.getId();
12            
13            SchedulerEngineUtil.schedule(ScheduledJob.SCHEDULER_ENTRY,
14                    StorageType.PERSISTED, portletId, 0);
15            
16            LOG.info("SCHEDULED SET !!!!");
17            
18        } catch (SchedulerException e) {
19            LOG.error("Scheduling the job", e);
20        }
21    }
22}


What am I doing wrong? Thank You for help.

Regards,
A.D.
guru prasad
RE: Does anybody managed to schedule a job programmatically?
April 26, 2013 3:02 AM
Answer

guru prasad

Rank: New Member

Posts: 12

Join Date: January 29, 2012

Recent Posts

Hi . the problem in your code is the portlet Id is not getting ..
Print the portlet Id and check.
Heard code the portlet Id (from the portlet table) and run.
It will work fine

please see the following code :
portlet Id i heardcoded :=newIncidentSubmissionScheduler_WAR_IncidentManagementportlet


SchedulerEntry entry = new SchedulerEntryImpl();
entry.setDescription("Scheduler portlet for incident submission to ITSM" );
entry.setEventListenerClass(NewIncidentSubmission.class.getName());
entry.setTriggerType(TriggerType.SIMPLE);
entry.setTriggerValue(time);
entry.setTimeUnit(TimeUnit.MINUTE);

try {
SchedulerEngineUtil.schedule(entry, StorageType.MEMORY,
"newIncidentSubmissionScheduler_WAR_IncidentManagementportlet", 0);
} catch (SchedulerException e1) {
e1.printStackTrace();
System.out.println("exception");
}

}


Regards
Guruprasad Hebbar
Liferay Associate Consultant
Sai Sriharsha Kasturi
RE: Does anybody managed to schedule a job programmatically?
May 8, 2015 6:35 AM
Answer

Sai Sriharsha Kasturi

Rank: Junior Member

Posts: 29

Join Date: October 21, 2012

Recent Posts

I achieved it through the following way,

1. Extend your scheduler class to MVCPortlet and implement to MessageListener interface.
2. Now, override init() method by placing the following code,


 1String cron = "0 0/1 * 1/1 * ? *";// You can read it from portal-ext.properties using PropsUtil
 2Trigger trigger = null; 
 3try { 
 4trigger = TriggerFactoryUtil.buildTrigger(TriggerType.CRON, EngageMailScheduler.class.getName(), EngageMailScheduler.class.getName(), new Date(), null, cron); 
 5  } catch (SchedulerException e) { 
 6   e.printStackTrace(); 
 7  } 
 8
 9  Message message = new Message(); 
10  //message.put(SchedulerEngine.CONTEXT_PATH, portlet.getContextPath()); 
11  message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, EngageMailScheduler.class.getName()); 
12  message.put(SchedulerEngine.PORTLET_ID, portlet.getPortletId()); 
13
14  try { 
15   SchedulerEngineHelperUtil.schedule(trigger, StorageType.PERSISTED, "", "liferay/scheduler_dispatch", message, 5); 
16  } catch (SchedulerException e) { 
17   e.printStackTrace(); 
18  }



3. Now, you can place your logic in the receive() method,

1public void receive(Message message) throws MessageListenerException {
2     LOGGER.info("IN: Dynamic scheduler");
3}


4. Finally, give the complete path of your implemented class in liferay-portlet.xml, as below

1 <portlet-class>com.test.MyScheduler</portlet-class>


Hope this helps,
Harsha
Vishal Kumar
RE: Does anybody managed to schedule a job programmatically?
May 11, 2015 10:50 PM
Answer

Vishal Kumar

Rank: Regular Member

Posts: 198

Join Date: December 11, 2012

Recent Posts

Hi Andrzej,

Did you get your answer?