定时任务关闭,意外中止

Cron4jPlugin cp = Cron4jPlugin()cp.addTask(AwsSqsReceiveTask(SystemUtils.().getOrderSqsName()awsCreds))me.add(cp)

定义一个定时任务,定时任务中包含对数据库的处理操作。

当这个定时任务正在执行的时候,如果停止tomcat导致定时任务未执行完毕,被意外中止。

Tomcat日志会报连接池被关闭的错误。如下:

image.png

问题:我应该怎么设置等待我的定时任务执行完毕后,再关闭Tomcat。

在beforeJfinalstop方法中我怎么获取当前正在执行的定时任务插件,并判断他的状态,如果正在运行,则等待执行完毕后再关闭Jfinal。 



评论区

JFinal

2020-06-01 12:26

Cron4jPlugin 传入 task 的时候,注意有一个配置:daemon, 这个设置成 false , tomcat 会等待其执行完成后再关闭

这个本质是 java 线程配置的事, Cron4jPlugin 提供了配置,你没有注意到

hb963724769

2020-06-01 14:45

@JFinal 谢谢回复。确实没认真看到配置,但是我设置不设置,如果我故意在定时任务线程执行的时候,去shutdown Tomcat,会抛出一个内存错误,能指点下我应该怎么解决吗? 如果我等待线程结束后关闭Tomcat就不会报错。

Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):

"localhost-startStop-2" #35 daemon prio=5 os_prio=0 tid=0x00007fb5740d0800 nid=0x7eca in Object.wait() [0x00007fb55fdfe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Thread.join(Thread.java:1249)
- locked <0x00000000d8baf220> (a java.lang.Thread)
at java.lang.Thread.join(Thread.java:1323)
at it.sauronsoftware.cron4j.TaskExecutor.join(Unknown Source)
at it.sauronsoftware.cron4j.Scheduler.tillExecutorDies(Unknown Source)
at it.sauronsoftware.cron4j.Scheduler.stop(Unknown Source)
- locked <0x00000000d88395f0> (a java.lang.Object)
at com.jfinal.plugin.cron4j.Cron4jPlugin$TaskInfo.stop(Cron4jPlugin.java:279)
at com.jfinal.plugin.cron4j.Cron4jPlugin.stop(Cron4jPlugin.java:226)
at com.jfinal.core.JFinal.stopPlugins(JFinal.java:115)
at com.jfinal.core.JFinalFilter.destroy(JFinalFilter.java:91)
at org.apache.catalina.core.ApplicationFilterConfig.release(ApplicationFilterConfig.java:312)
at org.apache.catalina.core.StandardContext.filterStop(StandardContext.java:4733)
- locked <0x00000000d5cabdb8> (a java.util.HashMap)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5528)
- locked <0x00000000d59e19d8> (a org.apache.catalina.core.StandardContext)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
- locked <0x00000000d59e19d8> (a org.apache.catalina.core.StandardContext)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1424)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1413)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

"Catalina-startStop-2" #34 daemon prio=5 os_prio=0 tid=0x00007fb59021b000 nid=0x7ec9 waiting on condition [0x00007fb580bfa000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000c0707990> (a java.util.concurrent.FutureTask)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:429)
at java.util.concurrent.FutureTask.get(FutureTask.java:191)
at org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:999)
- locked <0x00000000d59e1e88> (a org.apache.catalina.core.StandardHost)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
- locked <0x00000000d59e1e88> (a org.apache.catalina.core.StandardHost)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1424)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1413)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

"cron4j::scheduler[2bff2bb57426fc7c0fbc094a000001726e9c4f040b710954]::executor[2bff2bb57426fc7c52ae49a8000001726e9ca8966f71a121]" #32 prio=5 os_prio=0 tid=0x00007fb560077000 nid=0x7eb1 runnable [0x00007fb580dfb000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
- locked <0x00000000d9423d48> (a java.lang.Object)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
- locked <0x00000000d9423ec8> (a sun.security.ssl.AppInputStream)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at com.amazonaws.http.protocol.SdkHttpRequestExecutor.doReceiveResponse(SdkHttpRequestExecutor.java:82)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1330)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530)
at com.amazonaws.services.sqs.AmazonSQSClient.doInvoke(AmazonSQSClient.java:2207)
at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2174)
at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2163)
at com.amazonaws.services.sqs.AmazonSQSClient.executeReceiveMessage(AmazonSQSClient.java:1607)
at com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1578)
at com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1619)
at com.cms.task.AwsSqsReceiveTask.run(AwsSqsReceiveTask.java:41)
at it.sauronsoftware.cron4j.RunnableTask.execute(Unknown Source)
at it.sauronsoftware.cron4j.TaskExecutor$Runner.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

"ajp-nio-8009-Acceptor-0" #29 daemon prio=5 os_prio=0 tid=0x00007fb59048c800 nid=0x7eae waiting on condition [0x00007fb56c7ad000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:674)
at java.lang.Thread.run(Thread.java:745)

"ajp-nio-8009-ClientPoller-0" #28 daemon prio=5 os_prio=0 tid=0x00007fb590222800 nid=0x7ead waiting on condition [0x00007fb56c8ae000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:1023)
at java.lang.Thread.run(Thread.java:745)

"http-nio-5566-Acceptor-0" #27 daemon prio=5 os_prio=0 tid=0x00007fb590220800 nid=0x7eac waiting on condition [0x00007fb5809f8000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:674)
at java.lang.Thread.run(Thread.java:745)

"http-nio-5566-ClientPoller-0" #26 daemon prio=5 os_prio=0 tid=0x00007fb59021f000 nid=0x7eab waiting on condition [0x00007fb580af9000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:1023)
at java.lang.Thread.run(Thread.java:745)

"config.data" #22 prio=5 os_prio=0 tid=0x00007fb57467d000 nid=0x7ea7 waiting on condition [0x00007fb56ceb0000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d8824d58> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

热门反馈

扫码入社