AnsweredAssumed Answered

Spring integration - @AutoWiring Service Task ClassDelegate

Question asked by jhuntsman on Nov 9, 2015
Hello,

I am new to Activiti, so please excuse any ignorance. I am using some custom Java service tasks, and have went with the Class Delegate approach by deriving from TaskActivityBehavior. My custom service needs to call out using Spring AMQP (e.g. RabbitTemplate) to signal an external system. I was not having any luck using @AutoWire inside my custom derived ActivityBehavior class. Is this a supported out-of-the-box for Spring?

In the interim, I have come up with a workable solution, but would someone please validate my approach.

I did the following:

1. extended ClassDelegate with a custom implementation that uses the Spring ApplicationContext to AutoWire the instance of the Class Delegate

public class SpringClassDelegate extends ClassDelegate {

    private final ApplicationContext applicationContext;

    public SpringClassDelegate(ApplicationContext applicationContext, String className, List<FieldDeclaration> fieldDeclarations, Expression skipExpression, List<MapExceptionEntry> mapExceptions) {
        super(className, fieldDeclarations, skipExpression, mapExceptions);

        this.applicationContext = applicationContext;
    }

    @Override
    protected ActivityBehavior getActivityBehaviorInstance(ActivityExecution execution) {
        ActivityBehavior instanceBehavior = super.getActivityBehaviorInstance(execution);

        // Autowire the class delegate instance
        applicationContext.getAutowireCapableBeanFactory().autowireBean(instanceBehavior);

        return instanceBehavior;
    }
}


2. extended DefaultActivityBehaviorFactory with a custom implementation of createClassDelegateServiceTask which returns the custom Class Delegate instance which has been AutoWired

public class SpringActivityBehaviorFactory extends DefaultActivityBehaviorFactory {

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public ClassDelegate createClassDelegateServiceTask(ServiceTask serviceTask) {
        Expression skipExpression;
        if(StringUtils.isNotEmpty(serviceTask.getSkipExpression())) {
            skipExpression = this.expressionManager.createExpression(serviceTask.getSkipExpression());
        } else {
            skipExpression = null;
        }

        return new SpringClassDelegate(applicationContext, serviceTask.getImplementation(), this.createFieldDeclarations(serviceTask.getFieldExtensions()), skipExpression, serviceTask.getMapExceptions());
    }
}


3. called setActivityBehaviorFactory on my Activiti ProcessEngineConfiguration using my custom ActivityBehaviorFactory

It seems to be working as I am following in my debugger, but have I violated anything that I shouldn't have as far as Activiti is concerned?

Thank you

Jeff

Outcomes