I had this requirement where jBPM process was supposed to be triggered at specified time. I would like to share it here.
1)- 2 methods for testing it :
| public void deploy() |
|
| { |
|
|
| ProcessEngine processEngine = configuration.buildProcessEngine(); |
|
| RepositoryService repositoryService = processEngine.getRepositoryService(); |
|
| repositoryService.createDeployment().addResourceFromClasspath("test.jpdl.xml").deploy(); |
| } |
|
|
|
|
| public void startProcess(String key) |
|
| { |
|
|
| ProcessEngine processEngine = configuration.buildProcessEngine(); |
|
| ExecutionService executionService = processEngine.getExecutionService(); |
|
| executionService.startProcessInstanceByKey(key); |
|
|
|
| } |
|
2)- Creating a job that needs to be executed at the specified time :
| public void execute(JobExecutionContext arg0) throws JobExecutionException { |
|
|
|
|
| Methods methods = new Methods(); |
|
| methods.startProcess("testS"); |
|
|
| System.out.println("*******************STARTED******************"); |
| } |
3)- Creating a scheduler and a Trigger that would trigger the method in step 2.
| public void startupScheduler() throws SchedulerException, ParseException |
| { |
|
| SchedulerFactory schedulerFactory = new StdSchedulerFactory(); |
|
| Scheduler scheduler = schedulerFactory.getScheduler(); |
|
| scheduler.start(); |
|
|
|
|
| JobDetail jobDetail = new JobDetail("startProcessJob", Scheduler.DEFAULT_GROUP, StartProcessJob.class); |
|
|
|
|
| CronTrigger ct=new CronTrigger("cronTrigger","group2","00 49 13 22 * ?"); |
|
| scheduler.scheduleJob(jobDetail, ct); |
| } |
note 1: Here, the 3rd parameter in jobDetail is the class StartProcessJob implementing interface Job, that contains overridden method execute that is in step 2.
note 2: Last parameter in CronTrigger represents the date-time that is matched with system time for triggering it. (secs,mins,hours,date,month,year).
4)- Finally calling process deploy() and then calling method startupScheduler().
Hope this is helpful.
regds.
Comments