Skip to content

Commit

Permalink
#49 Fixed unexpectable thread executor termination
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogich-sc committed Apr 22, 2021
1 parent 4b74c78 commit 73ac15e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion GrailsflowGrailsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.jcatalog.grailsflow.status.ProcessStatusEnum
import com.jcatalog.grailsflow.scheduling.triggers.ConfigurableSimpleTrigger

class GrailsflowGrailsPlugin {
def version = "1.9.3-SNAPSHOT"
def version = "1.9.3-issue-49-SNAPSHOT"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.3 > *"
def dependsOn = [quartz: "1.0.1 > *"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import com.jcatalog.grailsflow.extension.SendEventParameters
import com.jcatalog.grailsflow.engine.concurrent.ProcessLock

import grails.util.Environment

import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Callable
import org.springframework.transaction.support.TransactionSynchronizationManager
Expand All @@ -57,6 +59,9 @@ import com.jcatalog.grailsflow.process.PostKillProcessHandler
import org.springframework.transaction.TransactionStatus
import org.quartz.ObjectAlreadyExistsException
import org.quartz.SimpleTrigger

import java.util.concurrent.Future

import static org.quartz.TriggerKey.*;
import java.util.concurrent.ThreadFactory
import com.jcatalog.grailsflow.status.ProcessStatusEnum
Expand Down Expand Up @@ -1295,11 +1300,14 @@ class ProcessManagerService implements InitializingBean {
log.debug("Executing node '${node.nodeID}' of process #${node.process?.id}(${node.process?.type})")
Closure actionsCode = processClass.nodeActions[node.nodeID]
// execute actions in separate session (creation a separate thread)
return Executors.newSingleThreadExecutor( new ThreadFactory() {
// This is important to put new instance to the separate variable to resolve the existed JDK issue
// https://bugs.openjdk.java.net/browse/JDK-8145304

ThreadFactory threadFactory = new ThreadFactory() {
public Thread newThread(Runnable r) {
SecurityManager s = System.getSecurityManager();
def group = (s != null)? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
Thread.currentThread().getThreadGroup();
def namePrefix = "#${node.process?.id}(${node.process?.type})-${node.nodeID}"
Thread t = new Thread(group, r, namePrefix, 0);
if (t.isDaemon())
Expand All @@ -1313,7 +1321,12 @@ class ProcessManagerService implements InitializingBean {
notifier.invocationThreadLock.writeLock().unlock()
return t;
}
}).submit( {
}

final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1, threadFactory )
final ExecutorService threadExecutor = Executors.unconfigurableExecutorService(fixedThreadPool)

final Future futureTask = threadExecutor.submit( {
def result
try {
Session session = SessionFactoryUtils.getNewSession(sessionFactory)
Expand Down Expand Up @@ -1381,7 +1394,12 @@ class ProcessManagerService implements InitializingBean {
}
}
return result
} as Callable ).get()
} as Callable )

def threadExecutionResult = futureTask.get()
threadExecutor.shutdown()

return threadExecutionResult

}

Expand Down

0 comments on commit 73ac15e

Please sign in to comment.