Using searchService to search in a separate thread in Jira 8

Tanya Gordon
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 17, 2019

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 = searchService.search(user, 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.

Thanks!!!

Tanya

2 answers

1 vote
Tanya Gordon
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 4, 2020

Hi,

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)
jtlu.preCall()
try {
    //code code code} finally {
    jtlu.postCall(log)
}

(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! :-)

Tanya

Muhammad Ramzan(Atlassian Certified Master)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 25, 2020

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", {

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

 

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)
jtlu.preCall()
postCommentsToBMC(issue, comments, bmcTicketId, sendCommentsPath)
} catch (Exception e) {
logs("Error occured while sending comments : ${e.getCause()}")
}
finally {
jtlu.postCall(Logger.getLogger("com.onresolve.jira.groovy"))


}
})

 

0 votes
Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2019

What are the exceptions you're getting?

Tanya Gordon
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 4, 2019

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(ManagedIndexSearcherFactory.java:15)
com.atlassian.jira.issue.index.ThreadLocalSearcherCache$Cache.retrieveEntitySearcher(ThreadLocalSearcherCache.java:116)
com.atlassian.jira.issue.index.ThreadLocalSearcherCache.getSearcher(ThreadLocalSearcherCache.java:39)
com.atlassian.jira.issue.index.DefaultIndexManager.getEntitySearcher(DefaultIndexManager.java:888)
com.atlassian.jira.issue.index.DefaultIndexManager.getIssueSearcher(DefaultIndexManager.java:865)
sun.reflect.GeneratedMethodAccessor988.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
com.atlassian.jira.config.component.SwitchingInvocationHandler.invoke(SwitchingInvocationHandler.java:38)
com.sun.proxy.$Proxy8.getIssueSearcher(Unknown Source)
com.atlassian.jira.issue.search.SearchProviderFactoryImpl.getSearcher(SearchProviderFactoryImpl.java:17)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.getIssueSearcher(LuceneSearchProvider.java:130)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.searchDocuments(LuceneSearchProvider.java:443)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.searchDocuments(LuceneSearchProvider.java:450)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.runSearch(LuceneSearchProvider.java:439)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.getHits(LuceneSearchProvider.java:218)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search(LuceneSearchProvider.java:362)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search(LuceneSearchProvider.java:135)
com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search(LuceneSearchProvider.java:140)
com.atlassian.jira.bc.issue.search.DefaultSearchService.search(DefaultSearchService.java:118)
com.atlassian.jira.bc.issue.search.SearchService$search.call(Unknown Source)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:144)

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

Any suggestions? 

Nallusamy Narayanaswamy
Contributor
February 3, 2020

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

Ahmed Trabelsi
Contributor
August 31, 2021

Hi,

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

Regards,

Ahmed Trabelsi

Ibrahim Itani December 4, 2023

Hello @Tanya Gordon  @Ahmed Trabelsi 

Do you know how this can be done

I want after issue update, to launch several actions in a listener async (another issue update, jql,...) 

Do you know how can this be done

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events