Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jangalinski
Created September 9, 2013 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jangalinski/6497803 to your computer and use it in GitHub Desktop.
Save jangalinski/6497803 to your computer and use it in GitHub Desktop.
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import org.camunda.bpm.engine.ManagementService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.ProcessEngineImpl;
import org.camunda.bpm.engine.impl.context.Context;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.interceptor.CommandExecutor;
import org.camunda.bpm.engine.impl.jobexecutor.JobHandler;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.MessageEntity;
import org.camunda.bpm.engine.impl.pvm.process.ProcessElementImpl;
import org.camunda.bpm.engine.impl.test.TestHelper;
import org.camunda.bpm.engine.runtime.Job;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
/**
* POC for custom Job creation in the job executor.
* @see http://is.gd/sK36Fj
* @author Jan Galinski, Holisticon AG
*/
public class CustomJobExecutionSpike {
private ManagementService managementService;
private CommandExecutor commandExecutor;
public static class CustomJobHandler implements JobHandler {
public static final String TYPE = "customJobHandler";
@Override
public String getType() {
return TYPE;
}
@Override
public void execute(final String configuration, final ExecutionEntity execution, final CommandContext commandContext) {
System.out.println("execute");
System.out.println("configuration=" + configuration);
System.out.println("execution=" + execution);
System.out.println("command=" + commandContext);
}
}
public class CreateJobCommand implements Command<String> {
@Override
public String execute(final CommandContext commandContext) {
final MessageEntity message = new MessageEntity();
message.setJobHandlerType(CustomJobHandler.TYPE);
final String config = "some string you want to pass to the handler";
message.setJobHandlerConfiguration(config);
Context.getCommandContext().getJobManager().send(message);
return message.getId();
}
}
@Before
public void setUp() throws Exception {
final ProcessEngine processEngine = TestHelper.getProcessEngine("camunda.cfg.xml");
managementService = processEngine.getManagementService();
commandExecutor = ((ProcessEngineImpl)processEngine).getProcessEngineConfiguration().getCommandExecutorSchemaOperations();
}
@Test
public void shouldCreateAndExecuteJob() {
Job findJob = findJob();
assertNull(findJob = findJob());
commandExecutor.execute(new CreateJobCommand());
assertNotNull(findJob = findJob());
managementService.executeJob(findJob.getId());
assertNull(findJob = findJob());
}
private Job findJob() {
return managementService.createJobQuery().singleResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment