why JiraAuthenticationContext.getUser returns null?

I made a quartz schedule. The mehtod getUser returns null. Why?And how to get user for searchService?

public class MyJob implements Job {
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {		
		JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
		final User user = authenticationContext.getUser().getDirectoryUser();


2 answers

1 accepted

You can set the user like this:

UserUtil userUtil = (UserUtil) ComponentAccessor.getComponentOfType(UserUtil.class);
ApplicationUser user = userUtil.getUserByName("some-user");

JiraAuthenticationContext jac = (JiraAuthenticationContext) ComponentAccessor.getComponentOfType (JiraAuthenticationContext.class);

While Shravan provides a workaround, an answer to the problem at hand is not given yet. As I bumped into this myself, I investigated and quickly came to the real reason: an authentication context is only available for code directly in the execution path of a web request, be it via the web portal or via REST interface.

If you want to run code asynchronously, then you are not tied to a particular user session so you don't get a user object from the authentication context. If user information is needed to handle your async code, either choose a fixed user, or pass the user info from the web-request to the async code.

Suggest an answer

Log in or Join to answer
Community showcase
Jason Wong
Published yesterday in Agility Beta

Welcome to agility

Every team in the world is unique, and so   Atlassian believes   that each and every team's best way of working  needs to  be molded to their unique circumstances  – ...

389 views 6 16
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot