Dependency injection in JavaDelegate

My engine requires to access some shared services but I am not using CDI or Spring context. Is it possible to perform dependency injection in a JavaDelegate without making access of those frameworks?

You could perhaps expose these services via webservices, RMI, … ?

I have not been stressing enough that what I am trying to do is dependency injection, i.e. being able to pass the dependency (the service) inside the Java Delegate, ideally through constructor injection so to make the delegate implementation immutable.

Another strategy I might consider is to use lookup (JNDI) to retrieve my service, but that would be more exposed to runtime failures (NullPointerException / ClassCastException).

Hi @Edmondo_Porcu,

To do dependency injection and have that working during process execution, you need two things:

  1. A dependency injection framework/library, like Spring or CDI or Google Guice. Camunda itself is not such a thing.
  2. A component that integrates the dependency injection context with the process engine. That means, the process engine must be able to access the objects managed by the dependency injection component (for example when you write a service task expression #{managedBean.doSomething()}). By default, Camunda has such components for Spring and CDI.

If you want to build a component as described in 2, I can refer you to interfaces to implement and extension points to use.

Cheers,
Thorben

Looking to point 1, how would that work? Camunda would call GUICE to instantiate my delegate?

Hi Edmondo

Yes. It would ask the Dependency Injection Framework to get the corresponding bean.
Camunda only offers integration for Spring and CDI out-of-the-box, so no support for Guice.

Cheers,
Christian

Hi @thorben, can you point me towards the interfaces and extension points to use that you mentioned in point 2?

Many thanks,

Matthew

Hi Matthew,

Is your use case a shared engine with process applications (where the dependency context is managed on application level) or an embedded engine where engine and dependency context are both managed in the application?

Is your primary use case dependency injection in Java Delegate implementations or something else?

Cheers,
Thorben

Hi @thorben,

Thank you for your reply.

In this case it is an embedded engine. Primary use is in Java Delegate implementations.

Many thanks,

Matthew

Hi Matthew,

Have a look at the interface org.camunda.bpm.engine.ArtifactFactory and the corresponding engine configuration property artifactFactory. This property is used to resolve instances of Java delegates when you reference them by class name. See org.camunda.bpm.engine.spring.SpringArtifactFactory and its usage for an example (https://github.com/camunda/camunda-bpm-platform/blob/master/engine-spring/src/main/java/org/camunda/bpm/engine/spring/SpringArtifactFactory.java).

If you want to resolve delegates via expressions, you can extend the class org.camunda.bpm.engine.impl.javax.el.ELResolver and wire it with the engine via the configuration property expressionManager (which requires an instance of org.camunda.bpm.engine.impl.el.ExpressionManager). As an example, see the class org.camunda.bpm.engine.spring.SpringExpressionManager and how it is used (https://github.com/camunda/camunda-bpm-platform/blob/master/engine-spring/src/main/java/org/camunda/bpm/engine/spring/SpringExpressionManager.java).

Hope that helps.

Cheers,
Thorben

How do you lookup for beans from the DelegateExecution?