Atlassian scheduler in 7.4.2 jira version not showing up right time in Scheduler details

Hi,

Have implemented atlassian scheduler using com.atlassian.scheduler. It works perfectly fine except, the Last run time and next run time are little messed up.

Using atlassian SDK 6.2.15 and jira version in pom is 7.4.2

POM.xml

<?xml version="1.0" encoding="UTF-8"?>


4.0.0
com.xxxx.xxxx.jiraPlugins
jiraExamples
1.0.0-SNAPSHOT

<organization>
    <name>Example Company</name>
    <url>http://www.example.com/</url>
</organization>
<name>jiraExamples</name>
<description>This is the com.xxxx.xxxx.jiraPlugins:jiraExamples plugin for Atlassian JIRA.</description>
<packaging>atlassian-plugin</packaging>
<dependencies>
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-api</artifactId>
        <version>${jira.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- Add dependency on jira-core if you want access to JIRA implementation classes as well as the sanctioned API. -->
    <!-- This is not normally recommended, but may be required eg when migrating a plugin originally developed against JIRA 4.x -->
    
    <!-- <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-core</artifactId>
        <version>${jira.version}</version>
        <scope>provided</scope>
    </dependency> -->
   
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.plugin</groupId>
        <artifactId>atlassian-spring-scanner-annotation</artifactId>
        <version>${atlassian.spring.scanner.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.plugin</groupId>
        <artifactId>atlassian-spring-scanner-runtime</artifactId>
        <version>${atlassian.spring.scanner.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- WIRED TEST RUNNER DEPENDENCIES -->
    <dependency>
        <groupId>com.atlassian.plugins</groupId>
        <artifactId>atlassian-plugins-osgi-testrunner</artifactId>
        <version>${plugin.testrunner.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2-atlassian-1</version>
        <!-- <scope>provided</scope> -->
    </dependency>
    <!-- Uncomment to use TestKit in your project. Details at https://bitbucket.org/atlassian/jira-testkit -->
    <!-- You can read more about TestKit at https://developer.atlassian.com/display/JIRADEV/Plugin+Tutorial+-+Smarter+integration+testing+with+TestKit -->
    <!--
    <dependency>
        <groupId>com.atlassian.jira.tests</groupId>
        <artifactId>jira-testkit-client</artifactId>
        <version>${testkit.version}</version>
        <scope>test</scope>
    </dependency>
    -->
   	
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.10.RELEASE</version>
      <scope>provided</scope>
  </dependency>	
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>com.atlassian.maven.plugins</groupId>
            <artifactId>maven-jira-plugin</artifactId>
            <version>${amps.version}</version>
            <extensions>true</extensions>
            <configuration>
                <productVersion>${jira.version}</productVersion>
                <productDataVersion>${jira.version}</productDataVersion>
                <!-- Uncomment to install TestKit backdoor in JIRA. -->
                <!--
                <pluginArtifacts>
                    <pluginArtifact>
                        <groupId>com.atlassian.jira.tests</groupId>
                        <artifactId>jira-testkit-plugin</artifactId>
                        <version>${testkit.version}</version>
                    </pluginArtifact>
                </pluginArtifacts>
                -->
                <enableQuickReload>true</enableQuickReload>
                <enableFastdev>false</enableFastdev>
                <!-- See here for an explanation of default instructions: -->
                <!-- https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins -->
                <instructions>
                    <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
                    <!-- Add package to export here -->
                    <Export-Package>
                        com.xxxx.xxxx.jiraPlugins.jiraExamples.api,
                    </Export-Package>
                    <!-- Add package import here -->
                    <Import-Package>
                        org.springframework.osgi.*;resolution:="optional",
                        org.eclipse.gemini.blueprint.*;resolution:="optional",
                        *
                    </Import-Package>
                    <!-- Ensure plugin is spring powered -->
                    <Spring-Context>*</Spring-Context>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
            <version>${atlassian.spring.scanner.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>atlassian-spring-scanner</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
            </executions>
            <configuration>
                <scannedDependencies>
                    <dependency>
                        <groupId>com.atlassian.plugin</groupId>
                        <artifactId>atlassian-spring-scanner-external-jar</artifactId>
                    </dependency>
                </scannedDependencies>
                <verbose>false</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>
<properties>
    <jira.version>7.4.2</jira.version>
    <amps.version>6.2.11</amps.version>
    <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
    <atlassian.spring.scanner.version>1.2.13</atlassian.spring.scanner.version>
    <!-- This key is used to keep the consistency between the key in atlassian-plugin.xml and the key to generate bundle. -->
    <atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
    <!-- TestKit version 6.x for JIRA 6.x -->
    <testkit.version>6.3.11</testkit.version>
</properties>

Implementing class

import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import org.jfree.util.Log;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.scheduler.JobRunner;
import com.atlassian.scheduler.JobRunnerRequest;
import com.atlassian.scheduler.JobRunnerResponse;
import com.atlassian.scheduler.SchedulerService;
import com.atlassian.scheduler.SchedulerServiceException;
import com.atlassian.scheduler.config.JobConfig;
import com.atlassian.scheduler.config.JobId;
import com.atlassian.scheduler.config.JobRunnerKey;
import com.atlassian.scheduler.config.RunMode;
import com.atlassian.scheduler.config.Schedule;

@ExportAsService({ scheduleJob.class })
@Named("scheduleJob")
public class scheduleJob implements JobRunner, InitializingBean, DisposableBean {

	private static final long EVERY_MINUTE = TimeUnit.MINUTES.toMillis(1);
	private static final JobRunnerKey JOB_RUNNER_KEY = JobRunnerKey.of(scheduleJob.class.getName());
	private static final JobId JOB_ID = JobId.of(scheduleJob.class.getName());
	private final SchedulerService scheduler;

	@Inject
	public scheduleJob(@ComponentImport SchedulerService scheduler) {
		this.scheduler = scheduler;
	}

	@Override
	public JobRunnerResponse runJob(JobRunnerRequest request) {
		System.out.println("Running scheduleJob at " + request.getStartTime());
		return JobRunnerResponse.success();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Starting...");
	
	scheduler.registerJobRunner(JOB_RUNNER_KEY, this);

		final JobConfig jobConfig = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY)
											 .withRunMode(RunMode.RUN_LOCALLY)
											 .withSchedule(Schedule.forInterval(EVERY_MINUTE, null));

		try {
			scheduler.scheduleJob(JOB_ID, jobConfig);
		} catch (SchedulerServiceException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("Stopping...");
		scheduler.unscheduleJob(JOB_ID);
	}

}

Any one who had this issue? Any suggestions?

Thanks!

1 Like

I am using Jira 8.3, trying to import the component SchedulerService but my plugin does not start once I try to inject it.
Is there an working example for Jira 8 and above?

I am experiencing the same issue. There is an open Bug regarding this problem: JRASERVER-65816