2

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.

Community
  • 1
  • 1
pcesarperez
  • 67
  • 1
  • 8

1 Answers1

0

Do you want to schedule or to reschedule? For rescheduling you will first need to delete the old schedule:

SchedulerEngineHelperUtil.delete(SessionCollector.class.getName(),
    StorageType.MEMORY_CLUSTERED);

(StorageType.MEMORY_CLUSTERED is the storage type that is used by deploy)

But if you want to create a new schedule on initialization anyway, you can just remove the trigger from the liferay-portlet.xml as well.

Next thing is your scheduling. This is what is working for me for Liferay 6.2, but this may change between different Liferay versions:

Message message = new Message();
message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, className);
message.put(SchedulerEngine.DESTINATION_NAME, DestinationNames.SCHEDULER_DISPATCH);
message.put(SchedulerEngine.RECEIVER_KEY, new ReceiverKey(className, className));
message.put(SchedulerEngine.PORTLET_ID, context.getPortletName());

SchedulerEngineHelperUtil.schedule(trigger, 
    StorageType.MEMORY_CLUSTERED, "description",
    DestinationNames.SCHEDULER_DISPATCH, message, 0);

I don't think that you need StorageType.PERSISTED for portlet specific schedulers.

Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • I forgot to mention that I am working with Liferay 6.1.1 CE. Anyway, I'll give it a try, changing the storage type and using your code snippet. Thanks! – pcesarperez Nov 20 '15 at 15:29