Multithreading Grails、线程、自动写入服务

Multithreading Grails、线程、自动写入服务,multithreading,grails,redis,executor,Multithreading,Grails,Redis,Executor,我已经在我的类中自动连接了redisService@Transactional。我有另一个workerThread类,它实现了runnable,它应该使用我以前的类中的Redis连接在DB中存储一些数据。如何使用worker thread类中第一个类的相同redisService对象?您可以将worker线程作为resources.groovy中的bean连接起来,并向redisService传递一个引用 workerThread(WorkerThread) { redisService =

我已经在我的类中自动连接了redisService@Transactional。我有另一个workerThread类,它实现了runnable,它应该使用我以前的类中的Redis连接在DB中存储一些数据。如何使用worker thread类中第一个类的相同redisService对象?

您可以将worker线程作为resources.groovy中的bean连接起来,并向redisService传递一个引用

workerThread(WorkerThread) {
  redisService = ref('redisService')
}
也就是说,如果您通过依赖项注入获得workerThread的句柄。如果您自己实例化它,您可以通过static Holders类获得Spring应用程序上下文的句柄,以手动提取bean:

Holders.grailsApplication.mainContext.getBean("redisService")
后者是一个坏主意,但使该方案可行

祝你好运