J tricks - Little JIRA Tricks
  • Home
  • Plugins ↓
    • JQL Tricks Plugin
      • JQL Tricks Plugin - Cloud
        • JQLT Cloud Installation
        • JQLT Cloud Configuration
        • JQLT Cloud Usage
        • JQLT Cloud License
        • JQLT Cloud FAQ
      • JQL Tricks Plugin - DC
        • JQLT DC Installation
        • JQLT DC Configuration
        • JQLT DC Usage
          • JQLT Issue Functions
          • JQLT Subtask Functions
          • JQLT Links Functions
          • JQLT Development Functions
          • JQLT Worklog Functions
          • JQLT Project Functions
          • JQLT Component Functions
          • JQLT Version Functions
          • JQLT Group Functions
          • JQLT User Functions
          • JQLT Date Functions
        • JQLT DC License
        • JQLT DC FAQ
        • JQLT DC Known Issues
        • JQLT DC Performance
      • JQL Tricks Cloud Migration
    • Simplified Planner
      • J-Planner Installation
      • J-Planner Configuration
      • J-Planner Usage
        • Creating a plan
        • Editing a plan
        • Deleting a plan
        • Viewing a plan
        • Modifying a plan
      • J-Planner FAQ
    • Atla-Search Plugin
      • Atla-Search Installation
      • Atla-Search Configuration
      • Atla-Search Usage
      • Atla-Search License
      • Atla-Search FAQ
    • Copy to subtask Plugin
    • All Plugins
  • Tutorials
  • The Book
  • Contact Us
  • Home
  • Plugins ↓
    • JQL Tricks Plugin
      • JQL Tricks Plugin - Cloud
        • JQLT Cloud Installation
        • JQLT Cloud Configuration
        • JQLT Cloud Usage
        • JQLT Cloud License
        • JQLT Cloud FAQ
      • JQL Tricks Plugin - DC
        • JQLT DC Installation
        • JQLT DC Configuration
        • JQLT DC Usage
          • JQLT Issue Functions
          • JQLT Subtask Functions
          • JQLT Links Functions
          • JQLT Development Functions
          • JQLT Worklog Functions
          • JQLT Project Functions
          • JQLT Component Functions
          • JQLT Version Functions
          • JQLT Group Functions
          • JQLT User Functions
          • JQLT Date Functions
        • JQLT DC License
        • JQLT DC FAQ
        • JQLT DC Known Issues
        • JQLT DC Performance
      • JQL Tricks Cloud Migration
    • Simplified Planner
      • J-Planner Installation
      • J-Planner Configuration
      • J-Planner Usage
        • Creating a plan
        • Editing a plan
        • Deleting a plan
        • Viewing a plan
        • Modifying a plan
      • J-Planner FAQ
    • Atla-Search Plugin
      • Atla-Search Installation
      • Atla-Search Configuration
      • Atla-Search Usage
      • Atla-Search License
      • Atla-Search FAQ
    • Copy to subtask Plugin
    • All Plugins
  • Tutorials
  • The Book
  • Contact Us

Scheduled Tasks in JIRA

5/2/2011

34 Comments

 
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.
Picture
And when the job runs, you will see the following printed in the console!
Picture
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!
scheduled-task-plugin.zip
File Size: 3 kb
File Type: zip
Download File

34 Comments
Matt Doar link
5/5/2011 05:34:30 am

Handy trick!

Reply
J-Tricks
5/5/2011 06:48:57 am

Thanks Matt :)

Reply
sudhir link
9/22/2011 03:29:36 am

hello j-tricks,

i would like to include this functionality into my jira plugin.

i tried with 'implementing plugin Job' also tried with 'QuartzInitializerServlet' but i am not able to get it. As my plugin has no WEB-INF directory so can you please tell me how should i use this.

Reply
J-Tricks
9/22/2011 12:31:18 pm

Sudhir, what is the error you get when you try implementing Job?

The WEB-INF directory is for JIRA and not for the plugin. You need to put the jar file in the WEB-INF/lib directory of the JIRA installation.

Reply
sudhir link
9/22/2011 09:56:15 pm

thanks a lot for such quick response.

i have put the the jar at required place also make change scheduler-config.xml it works for me..:)
(my mistake-i was using my own scheduler-config.xml)

but it doesn't full-fill my need i would like to write a service in which i can schedule the multiple job through the UI (using quartz scheduler api or something).

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,

i also would like to include this functionality into my jira plugin v2.
What must i do that the classpath of the execute methode will be found.
My opinion was to extend the atlassian-plugin.xml - but i dont know what to add.
Do you have any suggestion?

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:
I found there what i'm looking for. By the Way - Congratiolations for your "JIRA Development Cookbook". Is verry helpful for Jira programming beginners like me.

J-Tricks
6/28/2012 04:01:11 am

Glad it is useful. Thanks Ulrich :)

Bastien
11/26/2012 11:34:28 pm

Hi,

I would like to include a cron property in my Jira service.
Is it possible ?

Regards.

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,
I tried to perform my own job class (similar to exsample you described here) but when jira starts scheduler returns me ClassNotFoundException and points to my class inspite of my class is included to scheduler-config.xml, my .jar placed in WEBINF/libs, introduced in atlassian-plugin.xml as plugin v1. I don't mind what jira wants from me )... Any ideas maybe? Thanks.

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:

Thanks for the article. I don't have a atlassian-plugin.xml in my scheduled job project, but I am getting errors when I start JIRA:


2014-04-16 13:47:13,425 main WARN [atlassian.jira.startup.JiraStartupLogger]

*********************************************************************************************************************************************************************
***********************************
The following plugins failed to load:
MyScheduledJob: OSGi plugins cannot be deployed via the classpath, which is usually WEB-INF/lib.
URL is: jar:file:/C:/tomcat6//webapps/jira/WEB-INF/lib/MyScheduledJob-1.0.
jar!/atlassian-plugin.xml
*********************************************************************************************************************************************************************
***********************************

2014-04-16 13:47:13,436 main INFO [atlassian.jira.startup.JiraStartupLogger]

********************************************************************************
JIRA 4.4.5 build: 665 started. You can now access JIRA through your web browser.
********************************************************************************

I guess there is something wrong with my pom.xml? It looks something like this:
---------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.att.jira.plugins</groupId>
<artifactId>MyScheduledJob</artifactId>
<version>1.0</version>


<organization>
<name>my company</name>
<url>http://www.mycompany.com</url>
</organization>

<name>MyScheduledJob</name>
<description>MyScheduledJob description</description>
<packaging>atlassian-plugin</packaging>

<properties>
<jira.version>4.4.5</jira.version>
<jira.data.version>4.4</jira.data.version>
<amps.version>3.7.3</amps.version>
</properties>
.
(truncated)
---------------------------------------------------------------------------------------

Looks like I need to change values in <groupId> and <packaging>, but I am not sure what values to use...

Hope you/someone can help. Thanks for your time.

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?

Royce link
4/16/2014 10:44:58 am

I don't have atlassian-plugin.xml in my scheduled job project, but yet after maven build, the MyScheduleJob.jar file contains 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,
Is it possible to include writing contents in JIRA to an external file and modifying values of the JIRA DB, in the execute method of the JTricksScheduledJob class?

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.
I was planning to do this with Jelly scripts and services, but the users would like this run only once a week on a specified day/time. If I use a service, when it runs will change depending on when/if JIRA is restarted.

Is it possible to invoke Jelly scripts using the method you describe above?
If possible, how is this done or do you have an example?

Thanks

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.

You make use of the same code that service does to invoke the jelly runner. I haven't tried it but this might work.

EmbededJellyContext jelly = new EmbededJellyContext();
if (JiraJelly.allowedToRun())
{
try
{
jelly.runScript(inputFilename, outputFilename);
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
}
else
{
log.error(JiraJelly.JELLY_NOT_ON_MESSAGE);
throw new UnsupportedOperationException(JiraJelly.JELLY_NOT_ON_MESSAGE);
}

Reply
Esh
1/7/2014 02:52:10 pm

Hi,
Is it possible to put the jar file in a different location other than WEB-INF/lib and reference it from the scheduler-config.xml?
That is would this be possible,
<job name="ATask" class="<full path to installed directory>com.a.b.APluginPackage.ScheduledTask" />

Thanks

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 ?
Is there a security concern as files are added in to WEB-INF/lib or classes?Also is there an alternative to have the same functionality as a scheduled task in any other plug-in type?

Thanks

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?
Thanks a lot for the prompt responses, it was really very helpful :)

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:

<job name="NewJob" class="JobTrigger">
<job-data-map>
<entry>
<key>key0</key>
<value>value0</value>
</entry>
</job-data-map>
</job>

Reply
J-Tricks
3/31/2014 05:18:08 am

Hi,

Never tried this. You might want to ask in Atlassian Answers.

Reply
bani
3/26/2018 05:22:35 am

I have created some schedulers using following Link.
https://developer.atlassian.com/server/framework/atlassian-sdk/scheduling-events-via-sal-tutorial/

Now I need to run it one of it every Midnight, one every Monday and one to every Month end . how we can set the scheduler interval to 1 week. or one month I want my scheduler to run every Monday or every month end .

please give me some suggestion on how we can set the scheduler interval to 1 week. or one month or month .

Reply
Ajay pratap
3/12/2019 07:49:52 am

There is no such file as scheduler-config.xml under WEB-INF/classes
Do I have to Enable scheduler?? do i have to add an addon to jira to use this feature.
If not should I simply create the file and append the content?

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Enter your email address:

    Author

    Jobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. 

    Author of JIRA Development Cookbook and JIRA 5.x Development Cookbook.


    RSS Feed

    Categories

    All
    Acive Objects
    Ajs
    Book
    Components
    Condition
    Custom Fields
    Customization
    Events
    Gadgets
    Javascript
    Jql
    Listener
    Mail
    Permissions
    Plugin Framework
    Post Function
    Properties
    Remote Invocation
    Reporting
    Rest
    Scheduled Tasks
    Search
    Services
    Soap
    Summit
    User Interface
    Validator
    Webwork Actions
    Workflow

    Archives

    October 2016
    August 2016
    March 2016
    January 2016
    December 2015
    May 2014
    December 2013
    November 2013
    July 2013
    June 2013
    April 2013
    October 2012
    September 2012
    August 2012
    July 2012
    May 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011
    June 2011
    May 2011
    April 2011
    March 2011
    February 2011
    January 2011
    November 2010
    October 2010
    September 2010
    August 2010

SUPPORT
APPS
TUTORIALS
THE BOOK
© J-Tricks