Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Using searchService to search in a separate thread in Jira 8


Hi, community!

We are trying to upgrade to Jira 8 and are having an issue with the searchService functionality. 

We have several scripted listeners that are running in separate threads, but after upgrading to Jira 8, we encountered new exceptions. 

This is the code we use for search:

def searchService = ComponentAccessor.getComponent(SearchService.class)
def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
def query = queryParser.parseQuery(jqlSearch)
def search =, query, PagerFilter.getUnlimitedFilter())
List<Issue> issues = search.results

But the whole block runs in a separate thread (each listener starts with Thread.start function).

Please help me fix this. We can't remove the threading because otherwise, all the listeners run on the UI thread, and the user feels the slowness.



2 answers

1 vote


I'm gonna answer to myself here, just to help others if and when they have the same problem. This answer actually comes from Adaptavist support team:

It seems like you will need to set up a fresh thread-local context at the start of your threaded code, then tear it down at the end:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.thread.JiraThreadLocalUtil

def jtlu = ComponentAccessor.getComponent(JiraThreadLocalUtil)
try {
    //code code code} finally {

(Obviously the above needs to be wrapped in whatever threading boilerplate you are using. The postCall method requires you to pass in a logger but I believe that the log provided by ScriptRunner in the bindings should suffice.)


Hope this helps! :-)


Thanks for sharing it

I have listener in Jira which have complex code and taking time , so what did, i am creating a new thread so the user can proceed with the issue.


I am facing the same error in logs as mentioned above, here is my code


try {
Thread.start("My-Thread", {

} catch (Exception e) {
logs("Error occurred: ${e.getCause()}")
finally {


In my code i am doing multiple actions like


- Updating custom fields

- Creating issues

- Adding comments


Can you please guide what is 


I did like this way and error gone, is it the right one , are you using on Prodcution?


Thread.start("Thread-Send-Comments-BMC", {
def jtlu
try {

jtlu = ComponentAccessor.getComponent(JiraThreadLocalUtil)
postCommentsToBMC(issue, comments, bmcTicketId, sendCommentsPath)
} catch (Exception e) {
logs("Error occured while sending comments : ${e.getCause()}")
finally {



0 votes

What are the exceptions you're getting?

Hi Matt!

I'm getting the following exception in my code:

2019-11-04 11:02:11,167 Thread-32757 ERROR gordon [com.indigo.InvolvedTeamsListener] Exception for issue SWSHANI-805: java.lang.IllegalStateException: Incorrect usage of JIRA/lucene search API. You can only create/use: ManagedIndexSearcher inside a context (request or Jira-Thread-Local). Check: JiraThreadLocalUtils for details.
2019-11-04 11:02:11,180 Thread-32757 ERROR gordon [com.indigo.InvolvedTeamsListener] com.atlassian.jira.index.ManagedIndexSearcherFactory.createFrom(
sun.reflect.GeneratedMethodAccessor988.invoke(Unknown Source)
com.sun.proxy.$Proxy8.getIssueSearcher(Unknown Source)$ Source)

I basically sais I can't use search functionality inside a separate thread.

Any suggestions? 

Hi @Matt Doar__ LinkedIn , We are also facing the same issue. Any help will be much appreciated. 


Any update on this issue? I have the same requirement.


Ahmed Trabelsi

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events