Ever thought of running scheduled tasks within JIRA? Why do we need scheduled tasks when we have the JIRA Services? We have seen how to write a service in one of the previous posts. But in spite of all its advantages discussed there, services have a disadvantage. It always starts when JIRA is restarted and runs at regular intervals after that. So if you have a service that do some heavy memory intensive operation and if you restart JIRA in the middle of the day, you will suddenly find your instance's performance compromised! If it is scheduled to run every 24 hours, you will find the same service running in the middle of the day from then on until the next restart.
Scheduled tasks in JIRA are a good way to make sure all such operations happen at quite times, midnight for example. In this post we will write a real simple scheduled tasks and see how easy that can be!
The first task to create a scheduled task is to write a Java class that implements the Quartz Job interface. JIRA internally uses Quartz for scheduling its tasks and so Quartz comes bundled within JIRA. Our scheduled job just prints a line to the console and hence the Java class that we write is as simple as follows:
package com.jtricks; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class JTricksScheduledJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Running the job at "+(new Date()).toString()); } } As you can see, the only thing we need to do here is to implement the execute method. Whatever we do in the method can be as simple as the above one liner or as complex as initiating a nuclear explosion! The next task is to let JIRA know of our new task that we have written and also define the schedule for it. For that, we first need to create a jar file with the above class and drop it under the WEB-INF/lib folder of your JIRA installation. Note that it is not a v2 plugin and hence dropping it under JIRA_HOME/plugins/installed-plugins won't work. JIRA stores the schedule tasks information in the scheduler-config.xml file under WEB-INF/classes folder. We need to define the schedule for our new task in the same file. First we need to define a job under the jobs tag as shown:
<job name="JTricksJob" class="com.jtricks.JTricksScheduledJob" />
Next, we need to add a trigger that runs the JTricksJob. This is where we define the cron expression to run the job at defined timings.
<trigger name="JTricksJobTrigger" job="JTricksJob" type="cron"> <expression>0 0/2 * * * ?</expression><!-- run every 2 minutes --> </trigger> The above trigger schedules the job to run every 2 minutes. Lot more details on writing a cron expression can be found here. And that's it! The scheduled job is ready to run. All we need now is to restart JIRA. Once JIRA is restarted, the new job can be seen at the scheduler details page under Administration > Scheduler Details. We can also verify the next fire time for the task in the same page as shown in the below picture.
And when the job runs, you will see the following printed in the console!
That was simpler than we thought. Wasn't it? Attached is the sample code we have seen and the modified scheduler-config.xml. Enjoy the new trick!
34 Comments
J-Tricks
5/5/2011 06:48:57 am
Thanks Matt :)
Reply
hello j-tricks,
Reply
J-Tricks
9/22/2011 12:31:18 pm
Sudhir, what is the error you get when you try implementing Job?
Reply
thanks a lot for such quick response.
Reply
J-Tricks
9/23/2011 12:22:00 am
Sudhir, Glad it works for you now. You cannot schedule the jobs through UI yet. See JIRA services if that helps!
Reply
Ulrich Nack
6/28/2012 12:28:31 am
hello j-tricks,
Reply
J-Tricks
6/28/2012 12:34:12 am
Try this tutorial: https://developer.atlassian.com/display/DOCS/Plugin+Tutorial+-+Scheduling+Events+via+SAL
Reply
Ulrich Nack
6/28/2012 01:34:28 am
thanks for the fast response:
J-Tricks
6/28/2012 04:01:11 am
Glad it is useful. Thanks Ulrich :)
Bastien
11/26/2012 11:34:28 pm
Hi,
Reply
J-Tricks
11/27/2012 01:43:54 pm
Nope. That is why you need a scheduled task. See this as well : https://developer.atlassian.com/display/DOCS/Plugin+Tutorial+-+Scheduling+Events+via+SAL
Reply
Anton
8/22/2013 12:44:59 am
Hi,
Reply
J-Tricks
8/27/2013 08:52:21 am
You don't need any atlassian-plugin.xml in the jar. It is just a jar file and not a plugin.
Reply
Royce
4/16/2014 10:00:52 am
Hi J-Tricks:
Royce
4/16/2014 10:31:49 am
I don't have atlassian-plugin.xml in my scheduled job project, but yet after maven build, the MyScheduleJob.jar file has a atlassian-plugin.xml in it. Could it be something extra in my pom.xml?
J-Tricks
4/25/2014 09:09:52 am
Can you try the packaging as jar in pom.xml? I think if you use the Plugin SDK, it will use the packaging as atlassian plugin.
Royce
4/25/2014 09:21:49 am
That works.
J-Tricks
4/25/2014 10:27:25 am
Awesome!
Esh
8/28/2013 01:26:46 am
Hi,
Reply
J-Tricks
8/28/2013 02:21:49 am
You can do anything you want in the scheduled job. Having said that, modifying contents in JIRA DB should be done extremely carefully and only if you know what you are doing. You might end up in DB corruption or with stale values in cache if you don't use proper APIs.
Reply
Steve
10/27/2013 03:14:23 pm
I'd like to schedule something to once a week check for issues in JIRA that match certain criteria, and then send emails to the reporter/assignee/grpup members/etc.
Reply
J-Tricks
10/31/2013 07:38:47 am
You can certainly do that using this method. In the "execute" method, you can invoke the Jelly Runner and run the same script which the service runs.
Reply
Esh
1/7/2014 02:52:10 pm
Hi,
Reply
J-Tricks
1/7/2014 03:43:04 pm
The only other option is to put it under WEB-INF/classes folder in the appropriate folder structure (matching the package).
Reply
Esh
1/7/2014 04:05:01 pm
Thanks a lot for the response, For further clarification, is this considered as a method not recommended if using a version like JIRA 6.0.6 ?
J-Tricks
1/7/2014 04:07:29 pm
There is no security concern. Having said that, you can do it in a v2 plugin to make future upgrades easier. See https://developer.atlassian.com/display/DOCS/Plugin+Tutorial+-+Scheduling+Events+via+SAL for more details. It is already mentioned in one of the comments earlier :)
Esh
1/7/2014 05:02:26 pm
Can SAL be used with cron expressions as well or is it only for recurring intervals?
Reply
J-Tricks
1/8/2014 12:36:25 am
You will have to code it inside I guess. I haven't used it for cron expressions and so not sure :)
Reply
Mvilella
3/27/2014 12:23:51 am
Hi, it is possible to put parameters to the job as job-data-map? I use this configuration but job-data-map returns null:
Reply
J-Tricks
3/31/2014 05:18:08 am
Hi,
Reply
bani
3/26/2018 05:22:35 am
I have created some schedulers using following Link.
Reply
Ajay pratap
3/12/2019 07:49:52 am
There is no such file as scheduler-config.xml under WEB-INF/classes
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|