掲示板

Does anybody managed to schedule a job programmatically?

thumbnail
11年前 に Andrzej Drozdowski によって更新されました。

Does anybody managed to schedule a job programmatically?

Junior Member 投稿: 27 参加年月日: 10/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

  		<scheduler-entry>
			<scheduler-description>description</scheduler-description>
			<scheduler-event-listener-class>scheduler.test.ScheduledJob</scheduler-event-listener-class>
			<trigger>
				<cron>
					<cron-trigger-value>0 0 5 ? * MON,FRI</cron-trigger-value>
				</cron>
			</trigger>
		</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.)

public class ScheduledJob implements MessageListener {

	private static final Log LOG = LogFactoryUtil.getLog(ScheduledJob.class);

	public static SchedulerEntryImpl SCHEDULER_ENTRY = new SchedulerEntryImpl() {
		{
			setDescription("Job description");
			setTriggerType(TriggerType.CRON);
			setTriggerValue(0 0 5 ? * MON,FRI");
			setEventListenerClass(ScheduledJob.class.getName());
		}
	};

	@Override
	public void receive(Message message) throws MessageListenerException {
		LOG.info("Scheduled RUNS !!!");
	}
}


And the portlet that sets the schedule:

public class TestPortlet extends MVCPortlet {

	private static final Log LOG = LogFactoryUtil.getLog(TestPortlet.class);

	public void setSchedule(ActionRequest request, ActionResponse response) {
		try {
			ThemeDisplay themeDisplay = (ThemeDisplay) request
					.getAttribute(WebKeys.THEME_DISPLAY);
			PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
			String portletId = portletDisplay.getId();
			
			SchedulerEngineUtil.schedule(ScheduledJob.SCHEDULER_ENTRY,
					StorageType.PERSISTED, portletId, 0);
			
			LOG.info("SCHEDULED SET !!!!");
			
		} catch (SchedulerException e) {
			LOG.error("Scheduling the job", e);
		}
	}
}


What am I doing wrong? Thank You for help.

Regards,
A.D.
thumbnail
11年前 に guru prasad によって更新されました。

RE: Does anybody managed to schedule a job programmatically?

New Member 投稿: 15 参加年月日: 12/01/30 最新の投稿
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
thumbnail
8年前 に Sai Sriharsha Kasturi によって更新されました。

RE: Does anybody managed to schedule a job programmatically?

Junior Member 投稿: 34 参加年月日: 12/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,


String cron = "0 0/1 * 1/1 * ? *";// You can read it from portal-ext.properties using PropsUtil
Trigger trigger = null;  
try {  
trigger = TriggerFactoryUtil.buildTrigger(TriggerType.CRON, EngageMailScheduler.class.getName(), EngageMailScheduler.class.getName(), new Date(), null, cron);  
  } catch (SchedulerException e) {  
   e.printStackTrace();  
  }  

  Message message = new Message();  
  //message.put(SchedulerEngine.CONTEXT_PATH, portlet.getContextPath());  
  message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, EngageMailScheduler.class.getName());  
  message.put(SchedulerEngine.PORTLET_ID, portlet.getPortletId());  

  try {  
   SchedulerEngineHelperUtil.schedule(trigger, StorageType.PERSISTED, "", "liferay/scheduler_dispatch", message, 5);  
  } catch (SchedulerException e) {  
   e.printStackTrace();  
  }



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

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


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

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


Hope this helps,
Harsha
thumbnail
8年前 に Vishal Kumar によって更新されました。

RE: Does anybody managed to schedule a job programmatically?

Regular Member 投稿: 198 参加年月日: 12/12/12 最新の投稿
Hi Andrzej,

Did you get your answer?