What is it a CommandContext?

What is it a CommandContext?

I was trying to use the method task.getCandidates() to get the list of candidate groups but it failed because the method is expecting to get a CommandContext.

If the command context is null, is it safe to manually initialise it? If yes, what command context should I instantiate?

Is it safe to do something like:

boolean addCommandContext = Context.getCommandContext() == null;
try {
	if (addCommandContext) {
		Context.setCommandContext(new CommandContext(new Command<Void>() {
			@Override
			public Void execute(CommandContext commandContext) {

				return null;
			}

		}, processEngineConfiguration));
	}
	(TaskEntity)task.getCandidates();
	....
} finally {
	if (addCommandContext) {
		Context.removeCommandContext();
	}
}

Why a task is expecting to be in a CommandContext? Given that I can get an instance of Task using its taskId, I was expecting to get an object that is just an object representation of the task stored in the database.
Why does it depend on a context ?

Thanks

yes, to have a command context initialized, execute your code inside a command. See org.flowable.engine.ManagementService#executeCommand(org.flowable.common.engine.impl.interceptor.Command<T>).

No, use code similar to:

String executionId = processEngine.getManagementService().executeCommand(new Command<String>() {
            @Override
            public String execute(CommandContext commandContext) {
                EventSubscriptionQueryImpl q = new EventSubscriptionQueryImpl(commandContext);
                q.processInstanceId(processInstance.getProcessInstanceId());

                List<EventSubscription> subs = CommandContextUtil
                        .getEventSubscriptionEntityManager()
                        .findEventSubscriptionsByQueryCriteria(q);

                assertEquals(1, subs.size());
                EventSubscription sub = subs.get(0);
                assertEquals("testmessage", sub.getEventName());

                return sub.getExecutionId();
            }
        });

Regards
Martin

4 Likes

This is such important and usefull info it should be in the Docs. If you have any code common to your delegates and need that DelegateExecution this will solve your problems.