Confluence get BaseURL not working when in a Scheduled Job
Asked by Zach Mitchell:

Hey all,

I am trying to create a scheduled job to perform an action that is using the following command

```
import com.atlassian.confluence.setup.settings.SettingsManager;
...
String baseUrl = settingsManager.getGlobalSettings().getBaseUrl();

...
```

When running this as a service it wont run, i believe due to Scheduled job not running as an authenticated user. What can i do to solve this?

Thanks!


Answers:
author: Zach Mitchell
body: I rebuilt the plugin from scratch and finally got it to work, here is my JobService.class

```
import com.atlassian.confluence.setup.settings.SettingsManager;
import com.atlassian.plugin.spring.scanner.annotation.imports.ConfluenceImport;
import com.atlassian.scheduler.JobRunner;
import com.atlassian.scheduler.JobRunnerRequest;
import com.atlassian.scheduler.JobRunnerResponse;
import org.apache.log4j.Logger;


import javax.inject.Inject;
import javax.inject.Named;


@Named ("jobService")
public class JobService implements JobRunner {
    private static Logger log = Logger.getLogger(JobService.class);

    // Import SettingsManger using Spring.scanner
    @ConfluenceImport
    private SettingsManager settingsManager;
    // To associate settingManger as an object
    @Inject
    public void setSettingsManager(SettingsManager settingsManager) {
        this.settingsManager = settingsManager;
    }

    //Scheduled job
    @Override
    public JobRunnerResponse runJob(JobRunnerRequest request) {
        log.info("Test-Service Job started...");
        try {
            String baseUrl = settingsManager.getGlobalSettings().getBaseUrl();
            log.debug("baseUrl is " + baseUrl);
        } catch (Exception e) {
            log.error("Could not execute Scheduled Job named Test-Service cause of:" + e.getCause() + " " + e.getMessage());
            return JobRunnerResponse.failed("Job failed." + e.getCause());
        }
        System.out.println("Test-Service successfully executed...");
        log.info("Test-Service successfully executed...");
        return JobRunnerResponse.success("Job finished successfully.");
    }

}
```

Here is my atlassian-plugin.xml

```
<atlassian-plugin key="${atlassian.plugin.key}" name="${project.name}" plugins-version="2">
    <plugin-info>
        <description>${project.description}</description>
        <version>${project.version}</version>
        <vendor name="${project.organization.name}" url="${project.organization.url}" />
        <param name="plugin-icon">images/pluginIcon.png</param>
        <param name="plugin-logo">images/pluginLogo.png</param>
    </plugin-info>

    <!-- Job Service -->
    <job-config name="Test Service" key="JobService">
        <job key="jobService" perClusterJob="true" clusteredOnly="false"/>
        <schedule cron-expression="0 0 0 * * ? " jitterSecs="10" />
        <managed editable="true" keepingHistory="true" canRunAdhoc="true" canDisable="true" />
    </job-config>

</atlassian-plugin>
```


author: Stephen Sifers
body: Hello Zach,

Thank you for providing details into what you're attempting to accomplish. Without an error message or log entry, it will be difficult to find the actual cause of this not running.

Could you please review your logs after you attempt a scheduled run of this job and let us know of any errors or "caused by" events within the logs?

For more information about accessing the logs, please see [Working with Confluence Logs](https://confluence.atlassian.com/doc/working-with-confluence-logs-108364721.html).

We look forward to your response so we may find the cause and a solution.

Regards,  
Stephen Sifers


Tags: atl-adm

confluence-app

developer