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.
<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>
package com.jiradev.jira.plugins.scheduler;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
@Scanned
public interface ScheduledTaskMonitor {
public void reschedule(long interval);
}
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());
}
}
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;
}
}

Show your support by tweeting about this tutorial. Is Jiradev something you would recommend? Let me know.
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.
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.