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
4,365,084
Community Members
 
Community Events
168
Community Groups

Using searchService to search in a separate thread in Jira 8

Edited

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

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

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

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(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? 

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

Hi,

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

Regards,

Ahmed Trabelsi

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events