Camel usable outside Camel Task? (e.g. in JavaDelegate or Expression)

Hi @wberges,

It’s hard to say why you don’t get the camel context without seeing your spring/camel configuration.

As an alternative you could try creating your own ActivityBehavior based on the original CamelBehavior and add the triggerable behavior. I havent had time to test it but something like this might work for you:

package mypackage;

import org.apache.camel.Exchange;
import org.flowable.camel.ExchangeUtils;
import org.flowable.camel.FlowableEndpoint;
import org.flowable.camel.impl.CamelBehaviorDefaultImpl;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.impl.context.Context;
import org.flowable.engine.compatibility.Flowable5CompatibilityHandler;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.util.Flowable5Util;

public class MyTriggerableCamelBehavior extends CamelBehaviorDefaultImpl {

    @Override
    public void execute(DelegateExecution execution) {
        setAppropriateCamelContext(execution);

        final FlowableEndpoint endpoint = createEndpoint(execution);
        final Exchange exchange = createExchange(execution, endpoint);

        try {
            endpoint.process(exchange);
        } catch (Exception e) {
            throw new FlowableException("Exception while processing exchange", e);
        }
        execution.setVariables(ExchangeUtils.prepareVariables(exchange, endpoint));

        boolean isV5Execution = false;
        if ((Context.getCommandContext() != null && Flowable5Util.isFlowable5ProcessDefinitionId(Context.getCommandContext(), execution.getProcessDefinitionId())) ||
                (Context.getCommandContext() == null && Flowable5Util.getFlowable5CompatibilityHandler() != null)) {

            isV5Execution = true;
        }

        handleCamelException(exchange, execution, isV5Execution);
    }

    @Override
    public void trigger(DelegateExecution execution, String signalName, Object signalData) {
        boolean isV5Execution = false;
        if ((Context.getCommandContext() != null && Flowable5Util.isFlowable5ProcessDefinitionId(Context.getCommandContext(), execution.getProcessDefinitionId())) ||
                (Context.getCommandContext() == null && Flowable5Util.getFlowable5CompatibilityHandler() != null)) {

            isV5Execution = true;
        }

        if (isV5Execution) {
            Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();
            compatibilityHandler.leaveExecution(execution);
            return;
        }
        leave(execution);
    }
}

You can then reference this class in your serviceTask (but note that the triggerable flag on it will be ignored, this servicetask will always be triggerable).
Alternatively you can use a CamelTask and set the camelBehaviorClass field extension (available in BPMN but not in modeler) to your class.

You then can trigger the servicetask by using the runtime API or by finishing a camel route with a call to the Flowable producer to("flowable://my-process/myactivityid").

Hope that this helps.

Regards,
Paul