Requirement: Write a background task that runs every 5 minutes

Source code available at https://bitbucket.org/jiradev-tutorials/project-tab-panel

To get started, you will need an existing Plugin skeleton. We will be adding a few classes to an existing plugin skeleton.

  1. Add the below dependency to the pom.xml file.
    <dependency>
        <groupId>org.springframework.osgi</groupId>
        <artifactId>spring-osgi-core</artifactId>
        <version>1.2.1</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>com.atlassian.sal</groupId>
        <artifactId>sal-api</artifactId>
        <version>2.13.4</version>
        <scope>provided</scope>
    </dependency>
  2. Create a new package com.jiradev.jira.plugins.scheduler
  3. Create a new interface called ScheduledTaskMonitor.
    package com.jiradev.jira.plugins.scheduler;
    import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
    
    @Scanned
    public interface ScheduledTaskMonitor {
        public void reschedule(long interval);
    }
    
  4. Create a new class called ScheduledTask. This task should implement PluginJob and it contains the core logic of what your task needs to perform. For example, run a report every 4 hours.
    package com.jiradev.jira.plugins.scheduler;
    
    import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
    import com.atlassian.sal.api.scheduling.PluginJob;
    import java.util.Date;
    import java.util.Map;
    
    @Scanned
    public class ScheduledTask implements PluginJob {
    
        public void execute(Map jobDataMap) {
            // Do what your scheduled task needs to do here
            // In this example, we simply print a line to the console
            System.out.println("================= This is a scheduled task =====================" + new Date());
        }
    }
    
    
  5. Create a new package called impl. Create a new class called ScheduledTaskMonitorImpl that implements ScheduledTaskMonitor and LifecycleAware. You can set the interval using the interval variable.

    The minimum interval is 1 minute. Hence, if you try to set the interval to 5 seconds, you will notice the task only runs once a minute.

    package com.jiradev.jira.plugins.scheduler.impl;
    
    import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
    import com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport;
    import com.atlassian.sal.api.lifecycle.LifecycleAware;
    import com.atlassian.sal.api.scheduling.PluginScheduler;
    import com.jiradev.jira.plugins.scheduler.ScheduledTask;
    import com.jiradev.jira.plugins.scheduler.ScheduledTaskMonitor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.HashMap;
    
    @ExportAsService
    @Component
    public class ScheduledTaskMonitorImpl implements ScheduledTaskMonitor, LifecycleAware {
    
      /* package */ static final String KEY = ScheduledTaskMonitorImpl.class.getName() + ":instance";
      private static final String JOB_NAME = ScheduledTaskMonitorImpl.class.getName() + ":job";
      private long interval = 300000L;
      private Date lastRun = null;
    
      @JiraImport
      private final PluginScheduler pluginScheduler;
    
      @Autowired
      public ScheduledTaskMonitorImpl(PluginScheduler pluginScheduler) {
          this.pluginScheduler = pluginScheduler;
      }
    
      @Override
      public void onStart() {
          reschedule(interval);
      }
    
      public void reschedule(long interval) {
          this.interval = interval;
    
          pluginScheduler.scheduleJob(
                  JOB_NAME,                   // unique name of the job
                  ScheduledTask.class,     // class of the job
                  new HashMap() {{
                      put(KEY, ScheduledTaskMonitorImpl.this);
                  }},                         // data that needs to be passed to the job
                  new Date(),                 // the time the job is to start
                  interval);                  // interval between repeats, in milliseconds
      }
    
      /* package */ void setLastRun(Date lastRun) {
          this.lastRun = lastRun;
      }
    
    }
    
  6. That's about it. You now have a scheduled task that runs once every 5 minutes. Below is a screenshot of a sample scheduled task running every 60 seconds.


Share


Show your support by tweeting about this tutorial. Is Jiradev something you would recommend? Let me know.

Contribute


Do you have a Jira plugin tutorial that can be used on this site? Please do share it with me and I can add it along with the ones available.

Contribute

Feedback


Your feedback can help improve the content on this site. If you have anything that you would like me to change/implement on the site.