我编写了一个应用程序,使用Gearman向一个或多个工作人员提交工作(客户),尤其是java-gearman-service 0.6.2。
现在我正在运行无限循环来检查每10秒钟是否需要向Jobserver提交新作业。这背后有一些逻辑。请注意,我只想提交它们,而不是获得结果或回调。
基本上我所做的是:
Gearman gearman = Gearman.createGearman();
GearmanClient client = gearman.createGearmanClient();
client.addServer(gearman.createGearmanServer("someip",somePort);
while(true) {
// get the jobs to submit from the application logic
client.submitBackgroundJob("functionname","dataBytes");
// sleep for a few seconds.
}
这会产生越来越多的线程,直到消耗了大量内存,才会抛出OutOfMemory
异常。
我尝试在循环中实现Gearman
和GearmanClient
- 并将其关闭:
client.shutdown();
gearman.shutdown();
提交所有作业后,但结果是:
28 [gearman-1] INFO齿轮手 - [127.0.1.1:4730]:已连接
634 [main] FATAL main - Exception!
显示java.lang.NullPointerException
at org.gearman.impl.util.GearmanUtils.toString(Unknown Source)
at org.gearman.impl.serverpool.AbstractConnectionController.onDisconnect(Unknown Source)
at org.gearman.impl.server.remote.GearmanServerRemote $ InnerGearmanConnectionHandler.onDisconnect(Unknown Source)
at org.gearman.impl.core.GearmanConnectionManager $ SocketHandlerImpl.onDisconnect(Unknown Source)
at org.gearman.impl.reactor.SocketImpl.closeConnection(Unknown Source)
at org.gearman.impl.reactor.SocketImpl.close(未知来源)
at org.gearman.impl.core.GearmanConnectionManager $ SocketHandlerImpl $ Connection.close(Unknown Source)
at org.gearman.impl.server.remote.GearmanServerRemote.shutdown(Unknown Source)
at org.gearman.impl.GearmanImpl.shutdown(未知来源)
at MyApplication.start(ClientManager.java:clientShutdownLine)
31 [gearman-1] INFO齿轮手 - [127.0.1.1:4730]:断开连接
java.lang.IllegalStateException
at org.gearman.impl.client.GearmanJobReturnImpl.eof(Unknown Source)
at org.gearman.impl.client.ClientImpl.failTo(未知来源)
at org.gearman.impl.client.ClientImpl.onClose(Unknown Source)
at org.gearman.impl.client.ClientImpl.access $ 700(未知来源)
at org.gearman.impl.client.ClientImpl $ InnerConnectionController.onClose(Unknown Source)
at org.gearman.impl.serverpool.AbstractConnectionController.closeServer(Unknown Source)
at org.gearman.impl.serverpool.AbstractConnectionController.onDisconnect(Unknown Source)
at org.gearman.impl.server.remote.GearmanServerRemote $ InnerGearmanConnectionHandler.onDisconnect(Unknown Source)
at org.gearman.impl.core.GearmanConnectionManager $ SocketHandlerImpl.onAccept(Unknown Source)
at org.gearman.impl.reactor.NioReactor $ 1.completed(Unknown Source)
at org.gearman.impl.reactor.NioReactor $ 1.completed(Unknown Source)
at sun.nio.ch.Invoker.invokeUnchecked(未知来源)
at sun.nio.ch.Invoker $ 2.run(未知来源)
at sun.nio.ch.AsynchronousChannelGroupImpl $ 1.run(未知来源)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
我怎样才能避免Gearman-Java-Service在一段时间后产生如此多的线程?
答案 0 :(得分:0)
尝试更新到最新版本。我想你可能会看到这些问题:
另请注意,submitBackgroundJob是异步调用,并且某些排队正在后台进行。如果您提交的作业比客户端提供的更快,则会发生内存不足异常。鉴于你看起来好像等了几秒钟,情况可能并非如此,但试试这个:
Gearman gearman = Gearman.createGearman();
GearmanClient client = gearman.createGearmanClient();
client.addServer(gearman.createGearmanServer("someip",somePort);
while(true) {
// get the jobs to submit from the application logic
GearmanJobReturn jobReturn = client.submitBackgroundJob("functionname","dataBytes");
while(!jobReturn.isEOF()) {jobReturn.poll();}
}
GearmanJobReturn将在到达文件结尾之前轮询后台作业上的作业句柄(或错误消息)。这也可以在您提交下一份工作之前完全提交作业。
请勿在提交之间关闭服务。这会减慢一切。已建立的任何连接都已关闭,您需要重新建立连接以提交下一个作业。