Though Spring is similar… I’ve completely switched over to the CDI model for Java development - includes Camunda.
So, in a scenario using CDI - goal being process instance scoped objects:
NOTE: I’m doing most of this example free-hand - there may be typos.
Define a java-bean using @BusinessProcessScoped…
@BusinessProcessScoped
@Named("myProcessScopedBean")
As a ‘sanity’ check… I include a constructor with a log (during early development) just to make sure it’s working correctly.
public MyProcessScopedBean() {
super();
LOGGER.info("*** MyProcessScopedBean created");
}
Then, from within your process model, call on the bean as an expression:
#{myProcessScopedBean.setSomeValueProperty(execution,"someValue")}
Camunda should find the bean, and then it’s up to the bean itself to add your value. If the bean isn’t available, then it’s created per each process instance. I tend to always pass in the DelegateExecution context - but, this is a matter of preference since it’s also available via one of the other built-in Camunda services (I think it’s RuntimeService - if I’m remembering correctly).
// inside the bean: basic setter
public void setSomeValueProperty(String someValueProperty) {
this.someValueProperty = someValueProperty;
}
Now, on the other hand, If I’m directly managing process variables within the bean, I use the following:
// NOTE: example setting JMS reply-to temp' queue
@Inject @ProcessVariable("jMSReplyToName")
private Object jMSReplyToName;
One slight catch when injecting process variables is that I haven’t been able to directly set values - don’t know why. But, workaround is:
execution.setVariable("yetAnotherProcessVariable", "foo");
Getting the value is easy. I only use the “setVariable” workaround as needed. Not sure if this represents a bug or simply my error).
Camunda, or container, will passivate the objects as needed. And, for this reason I tend to avoid using BusinessProcessScoped beans. This avoidance based on my concerns regarding scalability. I also like using CDI or any event infrastructure for that matter… And, I prefer to avoid worrying about objects streaming back-and-forth, into/out of stores. This is a matter of preference - my preference. There are good reasons for using objects (the obvious being general object-oriented design principles).
To summarize, I prefer basic Camunda’s Process Variables for passing context/values between task implementations (as a general rule - there’s also singletone for such things as config/timer/etc…). Structured data (i.e. JSON) follows JSON-infrastructure such as Jackson. If you’re considering scaling to server clustering, it’s something to keep in mind.