Forum

Accueil » Liferay Portal » English » 3. Development

Vue combinée Vue Plate Arborescence
Sujets [ Précédent | Suivant ]
toggle
Charalampos Chrysikopoulos
Liferay 6 - Programmatically scheduling a job
15 décembre 2011 06:14
Réponse

Charalampos Chrysikopoulos

Rang: New Member

Publications: 21

Date d'inscription: 8 décembre 2011

Publications Récentes

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 décembre 2011 05:31
Réponse

Charalampos Chrysikopoulos

Rang: New Member

Publications: 21

Date d'inscription: 8 décembre 2011

Publications Récentes

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 décembre 2011 11:36
Réponse

Jitendra Rajput

Rang: Liferay Master

Publications: 738

Date d'inscription: 7 janvier 2011

Publications Récentes

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 décembre 2011 23:47
Réponse

Charalampos Chrysikopoulos

Rang: New Member

Publications: 21

Date d'inscription: 8 décembre 2011

Publications Récentes

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 décembre 2011 06:35
Réponse

Jelmer Kuperus

Rang: Liferay Legend

Publications: 1192

Date d'inscription: 10 mars 2010

Publications Récentes

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 décembre 2011 00:17
Réponse

Charalampos Chrysikopoulos

Rang: New Member

Publications: 21

Date d'inscription: 8 décembre 2011

Publications Récentes

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

Andreea Ardelean

Rang: New Member

Publications: 1

Date d'inscription: 21 juillet 2011

Publications Récentes

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 février 2012 05:26
Réponse

Charalampos Chrysikopoulos

Rang: New Member

Publications: 21

Date d'inscription: 8 décembre 2011

Publications Récentes

Could you describe how did you configured your jobs?
Nagendra Kumar Busam
RE: Liferay 6 - Programmatically scheduling a job
5 avril 2013 20:11
Réponse

Nagendra Kumar Busam

Rang: Liferay Master

Publications: 637

Date d'inscription: 7 juillet 2009

Publications Récentes

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 décembre 2013 15:03
Réponse

Riccardo Noviello

Rang: New Member

Publications: 6

Date d'inscription: 29 novembre 2013

Publications Récentes

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 décembre 2013 05:28
Réponse

Ahmed Kamel

Rang: New Member

Publications: 13

Date d'inscription: 28 avril 2011

Publications Récentes

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

Duy Nguyen

Rang: New Member

Publications: 3

Date d'inscription: 14 juin 2013

Publications Récentes

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 mars 2014 02:53
Réponse

Duy Nguyen

Rang: New Member

Publications: 3

Date d'inscription: 14 juin 2013

Publications Récentes

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 mars 2014 01:04
Réponse

Charalampos Chrysikopoulos

Rang: New Member

Publications: 21

Date d'inscription: 8 décembre 2011

Publications Récentes

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