Grails - 在后台运行TCP线程 [英] Grails - running a TCP thread on background
问题描述
我想有一个应用程序监听TCP套接字连接并对它们做出反应。为了我需要启动一个后台线程 - 我可以在BootStrap.groovy中做到这一点。
对于后台线程,我下载了执行程序插件。
代码如下所示:
class BootStrap {
def myService
def init = {servletContext - >
log.info(Bootstrapping)
development {
log.info(Doing myService async)
myService.doSomething()
}
}
class MyService {
def doSomething(){
runAsync {
printlnRunning!
}
}
}
}
代码是来自SO的另一个线程的复制粘贴。
我得到这个错误:
|错误2014-06-06 22:30:37,317 [localhost-startStop-1] ERROR context.GrailsContextLoader - 初始化应用程序时出错:无法在null对象上调用方法doSomething()
消息:无法调用方法doSomething()on空对象
Line |方法
- >> 14 | doCall in BootStrap $ _closure1_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
看起来myService对象没有被填充。编辑:尝试使用executorService,但这并没有帮助,或者我需要编辑一些配置或者什么?
。
|错误2014-06-07 00:06:36,099 [localhost-startStop-1]错误context.GrailsContextLoader - 初始化应用程序时出错:无法在null对象上调用方法doSomething()
消息:无法调用方法doSomething()on空对象
Line |方法
- >> 14 |在BootStrap中执行doCall $ _closure1_closure2
Bootstrap.groovy
class BootStrap {
def myService
def init = {servletContext - >
log.info(Bootstrapping)
development {
log.info(Doing myService async)
myService.doSomething()
}
}
}
MyService.groovy
class MyService {
def doSomething(){
runAsync {
printlnRunning!
}
}
}
I want to have an app that listens to TCP socket connections and can react to them. In order for that I need to start a background thread on start - I can do that in BootStrap.groovy.
For the background threading I downloaded the executor plugin.
The code looks like this:
class BootStrap {
def myService
def init = { servletContext ->
log.info("Bootstrapping")
development {
log.info("Doing myService async ")
myService.doSomething()
}
}
class MyService {
def doSomething() {
runAsync {
println "Running!"
}
}
}
}
This code is a copy-paste from an another thread here at SO.
I am getting this error:
| Error 2014-06-06 22:30:37,317 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Cannot invoke method doSomething() on null object
Message: Cannot invoke method doSomething() on null object
Line | Method
->> 14 | doCall in BootStrap$_closure1_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
It seems that the myService object is not populated. Do I need to edit some configs or something?
Edit: tried to use executorService, but that didn't help either.
| Error 2014-06-07 00:06:36,099 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Cannot invoke method doSomething() on null object
Message: Cannot invoke method doSomething() on null object
Line | Method
->> 14 | doCall in BootStrap$_closure1_closure2
You should move the service class to the proper folder in Grails project.
Bootstrap.groovy
class BootStrap {
def myService
def init = { servletContext ->
log.info("Bootstrapping")
development {
log.info("Doing myService async ")
myService.doSomething()
}
}
}
MyService.groovy
class MyService {
def doSomething() {
runAsync {
println "Running!"
}
}
}
这篇关于Grails - 在后台运行TCP线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!