Skip to content

Instantly share code, notes, and snippets.

@belano
Created January 26, 2015 11:04
Show Gist options
  • Save belano/1eb21b64c817d91f750c to your computer and use it in GitHub Desktop.
Save belano/1eb21b64c817d91f750c to your computer and use it in GitHub Desktop.
jBPM 3.2 Groovy scripting
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- hibernate dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.url">your_connection_url</property>
<property name="connection.username">your_username</property>
<property name="connection.password">your_password</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- DataSource properties (begin) === <property
name="hibernate.connection.datasource">java:comp/env/jdbc/JbpmDataSource</property>
==== DataSource properties (end) -->
<!-- JTA transaction properties (begin) === -->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!--
<property
name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
==== JTA transaction properties (end)
-->
<!--
CMT transaction properties (begin) === <property
name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property> <property
name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
==== CMT transaction properties (end)
-->
<!-- ################################### -->
<!-- # common settings # -->
<!-- ################################### -->
<!--
Automatic schema creation (begin) === <property name="hibernate.hbm2ddl.auto">create</property> ==== Automatic schema
creation (end)
-->
<!-- Simple memory-only cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- logging properties -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<!-- ############################################ -->
<!-- # mapping files with external dependencies # -->
<!-- ############################################ -->
<!-- Additional mappings defined per module go here -->
<mapping resource="hibernate.extra.hbm.xml" />
<mapping resource="hibernate.identity.hbm.xml" />
<!-- ###################### -->
<!-- # jbpm mapping files # -->
<!-- ###################### -->
<!-- hql queries -->
<mapping resource="org/jbpm/db/hibernate.queries.hbm.xml" />
<!--
hql queries used in simulation for querying historical data. uncomment if you want to use the
GetSimulationInputCommand or you want to use the queries directly. beware: the queries need the stddev function to be
enabled in your dialect. for more information, visit:
http://www.camunda.com/business_process_simulation_news/mysql_and_stddev.html
-->
<!--
<mapping resource="org/jbpm/sim/bam/hibernate.queries.hbm.xml" />
-->
<!-- graph.action mapping files -->
<mapping resource="org/jbpm/graph/action/MailAction.hbm.xml" />
<!-- graph.def mapping files -->
<mapping resource="org/jbpm/graph/def/ProcessDefinition.hbm.xml" />
<mapping resource="org/jbpm/graph/def/Node.hbm.xml" />
<mapping resource="org/jbpm/graph/def/Transition.hbm.xml" />
<mapping resource="org/jbpm/graph/def/Event.hbm.xml" />
<mapping resource="org/jbpm/graph/def/Action.hbm.xml" />
<mapping resource="org/jbpm/graph/def/SuperState.hbm.xml" />
<mapping resource="org/jbpm/graph/def/ExceptionHandler.hbm.xml" />
<mapping resource="org/jbpm/instantiation/Delegation.hbm.xml" />
<!-- ########################################### -->
<!-- # mapping file with external dependencies # -->
<!-- ########################################### -->
<!--
the following mapping file has a dependency on 'bsh-{version}.jar'. uncomment this if bsh is not in your classpath.
the script element will not be available in process definition files. note: this mapping must appear below
org/jbpm/graph/def/Action.hbm.xml
-->
<mapping resource="org/jbpm/graph/action/Script.hbm.xml" />
<!-- graph.node mapping files -->
<mapping resource="org/jbpm/graph/node/StartState.hbm.xml" />
<mapping resource="org/jbpm/graph/node/EndState.hbm.xml" />
<mapping resource="org/jbpm/graph/node/ProcessState.hbm.xml" />
<mapping resource="org/jbpm/graph/node/Decision.hbm.xml" />
<mapping resource="org/jbpm/graph/node/Fork.hbm.xml" />
<mapping resource="org/jbpm/graph/node/Join.hbm.xml" />
<mapping resource="org/jbpm/graph/node/MailNode.hbm.xml" />
<mapping resource="org/jbpm/graph/node/State.hbm.xml" />
<mapping resource="org/jbpm/graph/node/TaskNode.hbm.xml" />
<!-- context.def mapping files -->
<mapping resource="org/jbpm/context/def/ContextDefinition.hbm.xml" />
<mapping resource="org/jbpm/context/def/VariableAccess.hbm.xml" />
<!-- bytes mapping files -->
<mapping resource="org/jbpm/bytes/ByteArray.hbm.xml" />
<!-- module.def mapping files -->
<mapping resource="org/jbpm/module/def/ModuleDefinition.hbm.xml" />
<!-- file.def mapping files -->
<mapping resource="org/jbpm/file/def/FileDefinition.hbm.xml" />
<!-- taskmgmt.def mapping files -->
<mapping resource="org/jbpm/taskmgmt/def/TaskMgmtDefinition.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/def/Swimlane.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/def/Task.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/def/TaskController.hbm.xml" />
<!-- scheduler.def mapping files -->
<mapping resource="org/jbpm/scheduler/def/CreateTimerAction.hbm.xml" />
<mapping resource="org/jbpm/scheduler/def/CancelTimerAction.hbm.xml" />
<!-- graph.exe mapping files -->
<mapping resource="org/jbpm/graph/exe/Comment.hbm.xml" />
<mapping resource="org/jbpm/graph/exe/ProcessInstance.hbm.xml" />
<mapping resource="org/jbpm/graph/exe/Token.hbm.xml" />
<mapping resource="org/jbpm/graph/exe/RuntimeAction.hbm.xml" />
<!-- module.exe mapping files -->
<mapping resource="org/jbpm/module/exe/ModuleInstance.hbm.xml" />
<!-- context.exe mapping files -->
<mapping resource="org/jbpm/context/exe/ContextInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/TokenVariableMap.hbm.xml" />
<mapping resource="org/jbpm/context/exe/VariableInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/ByteArrayInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/DateInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/DoubleInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/HibernateLongInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/HibernateStringInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/LongInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/NullInstance.hbm.xml" />
<mapping resource="org/jbpm/context/exe/variableinstance/StringInstance.hbm.xml" />
<!-- job mapping files -->
<mapping resource="org/jbpm/job/Job.hbm.xml" />
<mapping resource="org/jbpm/job/Timer.hbm.xml" />
<mapping resource="org/jbpm/job/ExecuteNodeJob.hbm.xml" />
<mapping resource="org/jbpm/job/ExecuteActionJob.hbm.xml" />
<mapping resource="org/jbpm/job/CleanUpProcessJob.hbm.xml" />
<!-- taskmgmt.exe mapping files -->
<mapping resource="org/jbpm/taskmgmt/exe/TaskMgmtInstance.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/exe/TaskInstance.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/exe/PooledActor.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/exe/SwimlaneInstance.hbm.xml" />
<!-- logging mapping files -->
<mapping resource="org/jbpm/logging/log/ProcessLog.hbm.xml" />
<mapping resource="org/jbpm/logging/log/MessageLog.hbm.xml" />
<mapping resource="org/jbpm/logging/log/CompositeLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/ActionLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/NodeLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/ProcessInstanceCreateLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/ProcessInstanceEndLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/ProcessStateLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/SignalLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/TokenCreateLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/TokenEndLog.hbm.xml" />
<mapping resource="org/jbpm/graph/log/TransitionLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/VariableLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/VariableCreateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/VariableDeleteLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/VariableUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/ByteArrayUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/DateUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/DoubleUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/HibernateLongUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/HibernateStringUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/LongUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/context/log/variableinstance/StringUpdateLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/TaskLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/TaskCreateLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/TaskAssignLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/TaskEndLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/SwimlaneLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/SwimlaneCreateLog.hbm.xml" />
<mapping resource="org/jbpm/taskmgmt/log/SwimlaneAssignLog.hbm.xml" />
<!-- ################## -->
<!-- # cache settings # -->
<!-- ##################-->
<!-- strategy="nonstrict-read-write" can be used with HashtableCacheProvider -->
<class-cache class="org.jbpm.context.def.VariableAccess" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.file.def.FileDefinition.processFiles" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.action.Script.variableAccesses" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.graph.def.Action" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.graph.def.Event" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Event.actions" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.graph.def.ExceptionHandler" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.ExceptionHandler.actions" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.graph.def.Node" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Node.events" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Node.exceptionHandlers" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Node.leavingTransitions" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Node.arrivingTransitions" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.graph.def.ProcessDefinition" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.ProcessDefinition.events" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.ProcessDefinition.exceptionHandlers" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.ProcessDefinition.nodes" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.ProcessDefinition.actions" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.ProcessDefinition.definitions" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.SuperState.nodes" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.graph.def.Transition" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Transition.events" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.def.Transition.exceptionHandlers" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.node.Decision.decisionConditions" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.node.ProcessState.variableAccesses" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.graph.node.TaskNode.tasks" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.instantiation.Delegation" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.module.def.ModuleDefinition" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.taskmgmt.def.Swimlane.tasks" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.taskmgmt.def.TaskController" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.taskmgmt.def.TaskController.variableAccesses" usage="nonstrict-read-write" />
<class-cache class="org.jbpm.taskmgmt.def.Task" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.taskmgmt.def.Task.events" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.taskmgmt.def.Task.exceptionHandlers" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.taskmgmt.def.TaskMgmtDefinition.swimlanes" usage="nonstrict-read-write" />
<collection-cache collection="org.jbpm.taskmgmt.def.TaskMgmtDefinition.tasks" usage="nonstrict-read-write" />
</session-factory>
</hibernate-configuration>
#!/usr/bin/env groovy
import groovy.util.logging.Slf4j
import org.hibernate.Query
import org.hibernate.Session
import org.jbpm.JbpmConfiguration
import org.jbpm.JbpmContext
import org.jbpm.graph.def.ProcessDefinition
import org.jbpm.graph.exe.ProcessInstance
@Grapes([
@Grab(group='ch.qos.logback', module='logback-classic', version='1.0.13'),
@Grab(group='org.hibernate', module='hibernate', version='3.2.6.ga'),
@Grab(group='org.jbpm.jbpm3', module='jbpm-jpdl', version='3.2.6.GA'),
@Grab(group='org.jbpm.jbpm3', module='jbpm-identity', version='3.2.6.GA'),
@Grab(group='dom4j', module='dom4j', version='1.6'),
@Grab(group='cglib', module='cglib-nodep', version='2.1_3'),
@Grab(group='bsh', module='bsh', version='2.0b1'),
@Grab(group='antlr', module='antlr', version='2.7.7'),
@Grab(group='javax.transaction', module='jta', version='1.1'),
@Grab(group='com.oracle', module='ojdbc6', version='11.2.0.1.0'),
@GrabExclude('net.sf.ehcache:ehcache'),
])
@Slf4j
class Purge {
boolean dryRun
static main(args) {
def purge = new Purge()
def cli = new CliBuilder(
usage: 'purge.groovy -[hda] -p <process-definition> -i <process-instance>',
header:'\nOptions:\n',
footer: '\nInformation provided via above options is used to generate printed string.\n')
cli.h(longOpt: 'help' , 'usage information', required: false )
cli.d(longOpt: 'dry-run' , 'do not purge anything, just dry-run', required: false )
cli.a(longOpt: 'all' , 'purge all process definition', required: false )
cli.p(longOpt: 'process-definition' , 'process definition name to purge', required: false , args: 1 )
cli.i(longOpt: 'process-instance' , 'process instance to purge', required: false , args: 1 )
OptionAccessor opt = cli.parse(args)
if(!opt) return
if(opt.h) {
cli.usage()
return
}
if (opt.a && opt.p || opt.a && opt.i) {
println "options -a and -p or -i are mutually exclusive"
return
}
if(opt.p && opt.i) {
println "options -p and -i are mutually exclusive"
return
}
if(opt.d) {
log.info "dry-run set to true."
purge.dryRun = true
}
if (opt.a) {
purge.deleteProcessDefinition()
} else if(opt.p) {
purge.deleteProcessDefinition(opt.p)
} else if(opt.i) {
purge.deleteProcessInstance(opt.i)
} else {
println "Nothing to do - missing options"
}
}
void deleteProcessDefinition(process) {
def configuration = JbpmConfiguration.getInstance()
def context = configuration.createJbpmContext()
def sql
if (process) {
log.info "Removing [$process] versioned process definitions. Cascading."
sql = "select id_ from jbpm_processdefinition where name_ = '$process'"
} else {
log.info "Removing all versioned process definitions. Cascading."
sql = "select id_ from jbpm_processdefinition"
}
try {
def hibernateSession = context.getSession()
def query = hibernateSession.createSQLQuery(sql)
List<BigDecimal> definitions = query.list()
if (definitions) {
log.debug "Found " + definitions.size() + " definitions to remove."
for (BigDecimal definitionId : definitions) {
log.debug "Removing definition: " + definitionId
def definition = context.getGraphSession().getProcessDefinition(definitionId.longValue())
log.debug "Definition Name: " + definition.getName() + ", Version: " + definition.getVersion()
if (this.dryRun) {
log.debug "[dry-run mode] Not deleting"
} else {
context.getGraphSession().deleteProcessDefinition(definition)
}
}
} else {
log.debug "No process definitions found to remove."
}
} finally {
context.close()
}
log.debug "Completed removal process."
}
void deleteProcessInstance(pinstance) {
log.info "Removing [$pinstance] process instance."
def configuration = JbpmConfiguration.getInstance()
def context = configuration.createJbpmContext()
try {
def hibernateSession = context.getSession()
def processQuery = hibernateSession.createQuery("from org.jbpm.graph.exe.ProcessInstance pi where pi.id = $pinstance")
List instances = (List) processQuery.list()
if (instances) {
for (ProcessInstance instance : instances) {
long id = instance.getId()
if (this.dryRun) {
log.debug "[dry-run mode] Not deleting"
} else {
instance.end()
context.getGraphSession().deleteProcessInstance(instance)
}
}
} else {
log.debug "No instances found to remove."
}
} finally {
context.close()
}
log.debug "Completed removal process."
}
}
#!/usr/bin/env groovy
import groovy.util.logging.Slf4j
import org.hibernate.Query
import org.hibernate.Session
import org.jbpm.JbpmConfiguration
import org.jbpm.JbpmContext
import org.jbpm.graph.def.ProcessDefinition
import org.jbpm.graph.exe.ProcessInstance
@Grapes([
@Grab(group='ch.qos.logback', module='logback-classic', version='1.0.13'),
@Grab(group='org.hibernate', module='hibernate', version='3.2.6.ga'),
@Grab(group='org.jbpm.jbpm3', module='jbpm-jpdl', version='3.2.6.GA'),
@Grab(group='org.jbpm.jbpm3', module='jbpm-identity', version='3.2.6.GA'),
@Grab(group='dom4j', module='dom4j', version='1.6'),
@Grab(group='cglib', module='cglib-nodep', version='2.1_3'),
@Grab(group='bsh', module='bsh', version='2.0b1'),
@Grab(group='antlr', module='antlr', version='2.7.7'),
@Grab(group='javax.transaction', module='jta', version='1.1'),
@Grab(group='com.oracle', module='ojdbc6', version='11.2.0.1.0'),
@Grab(group='es.tid.panda', module='panda-common', version='1.x-SNAPSHOT'),
@GrabExclude('net.sf.ehcache:ehcache'),
])
@Slf4j
class Signal {
boolean dryRun
String pinstance
static main(args) {
def signal = new Signal()
def cli = new CliBuilder(
usage: 'signal.groovy [options] -t <transition> -i <process-instance>',
header:'\nOptions:\n',
footer: '\nInformation provided via above options is used to generate printed string.\n')
cli.h(longOpt: 'help' , 'usage information', required: false )
cli._(longOpt: 'dry-run' , 'do not apply any changes', required: false )
cli._(longOpt: 'end' , 'ends this process instance and all the tokens in it')
cli.t(longOpt: 'transition', 'specified transition on the current node', args:1, argName:'transition')
cli.i(longOpt: 'process-instance' , 'process instance to signal', args: 1 )
OptionAccessor opt = cli.parse(args)
if(!opt) return
if(opt.h) {
cli.usage()
return
}
if(!opt.i) {
cli.usage()
return
} else {
log.info "process-instance set to ${opt.i}."
signal.pinstance = opt.i
}
if(opt.'dry-run') {
log.info "dry-run set to true."
signal.dryRun = true
}
if(opt.'end') {
log.info "end set to true -> will try to end pinstance"
signal.end_process_instance()
return
}
def transition = opt.t ?: null
log.info "transition set to ${opt.t} -> will try to signal pinstance"
signal.signal_process_instance(transition)
}
def end_process_instance() {
wrapInTransaction({ instance, context=null ->
log.info "ending process instance ${instance.getId()}"
// TODO remove tasks
// context.getJobSession().deleteJobsForProcessInstance(instance)
instance.end()
})
}
def signal_process_instance(transition) {
wrapInTransaction({ instance, context=null ->
log.info "signalling process instance ${instance.getId()} - has ended? ${instance.hasEnded()}"
if (transition) instance.signal()
else instance.signal(transition)
})
}
def wrapInTransaction(command) {
def context = setupContext()
try {
def hibernateSession = context.getSession()
def processQuery = hibernateSession.createQuery("from org.jbpm.graph.exe.ProcessInstance pi where pi.id = ${this.pinstance}")
List instances = (List) processQuery.list()
if (instances) {
for (ProcessInstance instance : instances) {
long id = instance.getId()
if (this.dryRun) {
log.info "[dry-run mode] Not performing command on ${this.pinstance}"
} else {
log.info "Performing command on ${this.pinstance}"
command(instance, context)
}
}
} else {
log.info "No instances found."
}
} finally {
context.close()
}
}
def setupContext() {
def configuration = JbpmConfiguration.getInstance()
return configuration.createJbpmContext()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment