I have created a portlet (extending MVCPortlet
), with a class that extends MessageListener
to perform certain automated tasks. Let's call this class SessionCollector
.
The file liferay-portlet.xml
contains the following configuration snippet:
<scheduler-entry>
<scheduler-event-listener-class>
com.myportal.scheduler.SessionCollector
</scheduler-event-listener-class>
<trigger>
<simple>
<simple-trigger-value>10</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>
The task is set to start every 10 minutes.
The thing is, in my project, every configuration variable must be in a property file available in the server classpath. Let's say I have a .properties
file with the following line:
session.collector.cron.text=0 0/5 * * * ?
So, I want to reschedule the task to use that Cron expression, which means the task will be run every 5 minutes.
I've tried to write the rescheduling in the init ( )
method of the portlet, but to no avail.
This is the code, with some simplifications:
@Override
public void init ( ) throws PortletException {
// Reads the contents of the properties file.
String cronText = getCronText ( );
String jobName = SessionCollector.class.getName ( );
String jobGroup = SessionColllector.class.getName ( );
// The trigger is set to fire up using the Cron expression.
Trigger trigger = TriggerFactoryUtil.buildTrigger (
TriggerType.CRON,
jobName,
jobGroup,
new Date ( ),
null,
cronText
);
// This is the message to fire the rescheduling.
Message message = new Message ( );
message.put (SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, jobName);
message.put (SchedulerEngine.PORTLET_ID, this.getPortletName ( ));
// And finally, the actual reschedule.
SchedulerEngineUtil.schedule (
trigger,
StorageType.PERSISTED,
""
"liferay/scheduler_dispatch",
message,
5
);
}
I suspect the problem lies in the way I get the portlet id (at least, it could be one of the problems). Anyway, the rescheduling does not throw any exception, but the rescheduling does not take place, nevertheless. The task keeps on running every 10 minutes, not 5.
So, to be clear, is there any way to reschedule the task dynamically in the portlet itself? Notice that the portlet is just deployed, but not added to any page, thus, it lacks any rendering code.
Thanks!
Update (23/11/2015)
This question has been proposed as being a duplicate of another question, but I think it's fairly unrelated.
My question is related to the mechanisms Liferay uses to schedule/reschedule automated tasks, using Quartz as the underlying scheduler engine, and the other question is related to an H2 Database/Hibernate layer problem, as far as I can tell.