Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Charalampos Chrysikopoulos
Liferay 6 - Programmatically scheduling a job
15. Dezember 2011 06:14
Antwort

Charalampos Chrysikopoulos

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 8. Dezember 2011

Neue Beiträge

Hello everyone,

in my portlet I have the need of a UI, where the user can schedule some jobs.

That's why the scheduling through the configuration of liferay-portlet.xml is not a solution.

I'd like to be able to create some triggers of my own and schedule them for a given Job.
I created a class that implements MessageListener:
1
2public class MyJob implements MessageListener {
3
4    @Override
5    public void receive(Message message) {
6              // do my stuff here
7        }
8}


Then I tried to use SchedulerEngineUtil to schedule this job:
1
2SchedulerEntry entry = new SchedulerEntryImpl();
3entry.setEventListenerClass(someClassName);
4entry.setTriggerValue(someCronString);
5entry.setTriggerType(TriggerType.CRON);
6entry.setDescription(someClassName);
7       
8SchedulerEngineUtil.schedule(entry, this.getClass().getClassLoader());


The above code doesn't create any errors. After running the code in the portlet and looking at the QUARTZ tables in the database, everything seems correct. The job is there and the trigger too.

But its never fired. No errors. No feedback from the portal scheduler. Simply my job is never fired.

With exactly the same job class, when I try to run my job through liferay-portlet.xml like:
 1
 2        <scheduler-entry>
 3            <scheduler-description>My description</scheduler-description>
 4            <scheduler-event-listener-class>somePackage.MyJob</scheduler-event-listener-class>
 5            <trigger>
 6                <cron>
 7                    <cron-trigger-value>someTriggerString</cron-trigger-value>
 8                </cron>
 9            </trigger>
10        </scheduler-entry>

I see exactly the same entries in the QUARTZ tables in the database, and the job fires as the trigger commands.

Does someone has any suggestions, what to do?
Charalampos Chrysikopoulos
RE: Liferay 6 - Programmatically scheduling a job
16. Dezember 2011 05:31
Antwort

Charalampos Chrysikopoulos

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 8. Dezember 2011

Neue Beiträge

As a workaround I used my own quartz scheduler in the portlet with different tables in the database and this works fine, but its a workaround and not a clean work. If someone knows how to schedule a job programmatically, please help me. Thanks a lot.
Jitendra Rajput
RE: Liferay 6 - Programmatically scheduling a job
23. Dezember 2011 11:36
Antwort

Jitendra Rajput

Rang: Liferay Master

Nachrichten: 831

Eintrittsdatum: 7. Januar 2011

Neue Beiträge

If u want to run scheduler based on user inputs at run time i would suggest implement your own quartz scheduler .
And get the cron expression from preferences . and when ever user update scheduler time update preferences so scheduler timings will be automatically changed
Charalampos Chrysikopoulos
RE: Liferay 6 - Programmatically scheduling a job
27. Dezember 2011 23:47
Antwort

Charalampos Chrysikopoulos

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 8. Dezember 2011

Neue Beiträge

Thank you for your answer Jitendra. What you describe is what I finally did in my second post because I was in a dead-end and under time pressure. But I would like to know the "right" way to do using the liferay API, if something like this is even possible in the current version.
Jelmer Kuperus
RE: Liferay 6 - Programmatically scheduling a job
27. Dezember 2011 06:35
Antwort

Jelmer Kuperus

Rang: Liferay Legend

Nachrichten: 1192

Eintrittsdatum: 10. März 2010

Neue Beiträge

I am not sure why its not working for you but i've done it from a startup action in liferay 6.0.6 and that worked fine

1Thread thread = Thread.currentThread();
2SchedulerEngineUtil.schedule(DocumentConversionJob.SCHEDULER_ENTRY, thread.getContextClassLoader());


 1public class DocumentConversionJob implements MessageListener {
 2
 3    public static final SchedulerEntry SCHEDULER_ENTRY = new SchedulerEntryImpl() {
 4        {
 5            setDescription("Job that processes pending document conversion requests asynchronously");
 6            setTriggerType(TriggerType.SIMPLE);
 7            setTimeUnit(TimeUnit.SECOND);
 8            setTriggerValue("30");
 9            setEventListenerClass(DocumentConversionJob.class.getName());
10        }
11    };
12
13....
Charalampos Chrysikopoulos
RE: Liferay 6 - Programmatically scheduling a job
28. Dezember 2011 00:17
Antwort

Charalampos Chrysikopoulos

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 8. Dezember 2011

Neue Beiträge

I tried your approach and it worked fine. Thanks for that.
Andreea Ardelean
RE: Liferay 6 - Programmatically scheduling a job
16. Januar 2012 10:51
Antwort

Andreea Ardelean

Rang: New Member

Nachrichten: 1

Eintrittsdatum: 21. Juli 2011

Neue Beiträge

Hi there,

I have a portlet that I use to schedule programmatically a job that has a cron trigger. Job is in quartz tables and fired when it is set.

If I restart application server, job is not in quarz tables anymore.
Is there a way (some property to be set) that can help me block this deletion at re-start of the server?

Thanks in advance
Charalampos Chrysikopoulos
RE: Liferay 6 - Programmatically scheduling a job
10. Februar 2012 05:26
Antwort

Charalampos Chrysikopoulos

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 8. Dezember 2011

Neue Beiträge

Could you describe how did you configured your jobs?
Nagendra Kumar Busam
RE: Liferay 6 - Programmatically scheduling a job
5. April 2013 20:11
Antwort

Nagendra Kumar Busam

Rang: Liferay Master

Nachrichten: 637

Eintrittsdatum: 7. Juli 2009

Neue Beiträge

Hi,
I was doing something similar to what you have mentioned. In my local jobs are executing fine. Where as on our Dev & QA servers when processing is taking more time, there are multiple threads running for same job when I run once. How can overcome multiple threads from running.

Thanks in advance
Riccardo Noviello
RE: Liferay 6 - Programmatically scheduling a job
14. Dezember 2013 15:03
Antwort

Riccardo Noviello

Rang: New Member

Nachrichten: 6

Eintrittsdatum: 29. November 2013

Neue Beiträge

In Liferay 6.1 there is no method schedule(SchedulerEntry arg0, ContextClassLoader arg1) anymore.

Also there seems to be an issue with the registration of the MessageListener. I have managed to make work this code at run time:


//Create the Message
Message message = new Message();
message.put("message", "Hello this is a reminder");
message.put("email", "6@gmail.com");

//My custom Job Listener class (Implements MessageListener)
ScheduledJobListener job = new ScheduledJob();

//Create the trigger for the job
Trigger trigger = new CronTrigger("The Job Name", "The Group Name", new Date(),null, " 0/10 1/1 * 1/1 * ? *"); //triggers every 10 seconds

cronJob.setTrigger(trigger);

System.out.println("Scheduling "+trigger.getJobName());

//Schedule new Job
MessageBusUtil.registerMessageListener(DestinationNames.SCHEDULER_DISPATCH, job);
SchedulerEngineUtil.schedule(trigger, StorageType.PERSISTED,trigger.getJobName(), DestinationNames.SCHEDULER_DISPATCH, message, 10);


Which works, however when you restart your server, the scheduler does not start working the jobs persisted in your DB, until you schedule a new instance of the same listener (ScheduledJobListener)...So I am starting thinking there might be an issue with the registration/un-registration of the Listeners.
Perhaps I am following the wrong approach, so please suggest.

P.S. If I don't register the MessageBusUtil.registerMessageListener the job never triggers...
Ahmed Kamel
RE: Liferay 6 - Programmatically scheduling a job
25. Dezember 2013 05:28
Antwort

Ahmed Kamel

Rang: New Member

Nachrichten: 13

Eintrittsdatum: 28. April 2011

Neue Beiträge

I'm facing the same problem, Did you find the solution?
Duy Nguyen
RE: Liferay 6 - Programmatically scheduling a job
3. Januar 2014 02:09
Antwort

Duy Nguyen

Rang: New Member

Nachrichten: 3

Eintrittsdatum: 14. Juni 2013

Neue Beiträge

Riccardo Noviello:
In Liferay 6.1 there is no method schedule(SchedulerEntry arg0, ContextClassLoader arg1) anymore.

Also there seems to be an issue with the registration of the MessageListener. I have managed to make work this code at run time:


//Create the Message
Message message = new Message();
message.put("message", "Hello this is a reminder");
message.put("email", "6@gmail.com");

//My custom Job Listener class (Implements MessageListener)
ScheduledJobListener job = new ScheduledJob();

//Create the trigger for the job
Trigger trigger = new CronTrigger("The Job Name", "The Group Name", new Date(),null, " 0/10 1/1 * 1/1 * ? *"); //triggers every 10 seconds

cronJob.setTrigger(trigger);

System.out.println("Scheduling "+trigger.getJobName());

//Schedule new Job
MessageBusUtil.registerMessageListener(DestinationNames.SCHEDULER_DISPATCH, job);
SchedulerEngineUtil.schedule(trigger, StorageType.PERSISTED,trigger.getJobName(), DestinationNames.SCHEDULER_DISPATCH, message, 10);


Which works, however when you restart your server, the scheduler does not start working the jobs persisted in your DB, until you schedule a new instance of the same listener (ScheduledJobListener)...So I am starting thinking there might be an issue with the registration/un-registration of the Listeners.
Perhaps I am following the wrong approach, so please suggest.

P.S. If I don't register the MessageBusUtil.registerMessageListener the job never triggers...


Hi Ricardo,

Actually I can run the job without need to register MessageBusUtil.registerMessageListener, we also do not need to:
cronJob.setTrigger(trigger);

But also get the same problem as yours, somehow when restarting server job not resumed correctly, I need to delete jobs in db, then reset job information via GUI, then it runs.

Its a big problem with us because we need to set a lot of jobs scheduled at different time.

Appreciate a lot any help in resuming jobs when server restart.

Thanks
Duy Nguyen
RE: Liferay 6 - Programmatically scheduling a job
13. März 2014 02:53
Antwort

Duy Nguyen

Rang: New Member

Nachrichten: 3

Eintrittsdatum: 14. Juni 2013

Neue Beiträge

Hi all,

Finally I found out the solution to schedule a job programmatically and restart it successfully, I wanna share my experience here for you guys.
1. Create custom Destination name instead of using DestinationNames.SCHEDULER_DISPATCH:
We use init() method to add custom Destination to MessageBusUtil (required):
1   _log.info("Restart existing job scheduler...");
2            if(MessageBusUtil.getMessageBus().getDestination("Sample Dest") == null) {
3                Destination dest = new ParallelDestination("Sample Dest");
4                MessageBusUtil.addDestination(dest);
5            }


2. Get all information from GUI, info such as start time, end time, cron expression, then you schedule NEW job via ScheduleEngineUtil:
1SchedulerEngineUtil.schedule(trigger, StorageType.PERSISTED,
2                        description, destination, message, 0);


3. In case you want to update existing job, use update instead:
1SchedulerEngineUtil.update(trigger, StorageType.PERSISTED);


4. To handle when server restart, first we need to recalculate next start time, then call:
1SchedulerEngineUtil.schedule(trigger, StorageType.PERSISTED,
2                        description, destination, message, 0);

5. How to build trigger:
1 CronTrigger trigger = new CronTrigger(jobName, groupName,
2                startTimeTrigger, cronExpression);

We did test and it worked really well, I found out liferay scheduler is tough task and this is the way how we solved the problem, hope it help.

Regards,
Duy
Charalampos Chrysikopoulos
RE: Liferay 6 - Programmatically scheduling a job
11. März 2014 01:04
Antwort

Charalampos Chrysikopoulos

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 8. Dezember 2011

Neue Beiträge

Very interesting. I will try this out when I have time and post my results.