フォーラム

ホーム » Liferay Portal » English » 3. Development

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Andrzej Drozdowski
Does anybody managed to schedule a job programmatically?
2013/04/22 7:56
答え

Andrzej Drozdowski

ランク: Junior Member

投稿: 25

参加年月日: 2010/09/03

最近の投稿

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?
2013/04/26 3:02
答え

guru prasad

ランク: New Member

投稿: 12

参加年月日: 2012/01/29

最近の投稿

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?
2015/05/08 6:35
答え

Sai Sriharsha Kasturi

ランク: Junior Member

投稿: 30

参加年月日: 2012/10/21

最近の投稿

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?
2015/05/11 22:50
答え

Vishal Kumar

ランク: Regular Member

投稿: 198

参加年月日: 2012/12/11

最近の投稿

Hi Andrzej,

Did you get your answer?