cancel
Showing results for 
Search instead for 
Did you mean: 

Integration of own activiti tasks with Spring

mpriess
Champ in-the-making
Champ in-the-making
Hi,

I like to extend activiti with a custom task which receive a message and forward this message to a JMX queue. After this step the task is going in a wait state and get a signal from another component if the work is done. So I developed a task with this behavior.

Actually I have the problem that im unable to inject the JMS Template into ImpactServiceTask if activiti:class is used. If I changed the serviceTask manual to activiti:delegateExpression everything is fine.

When i drag and drop my component in the eclipse modeller the service task is generated with the activiti:class attribute. Is there a way to generate a serviceTask with a delegateExpression or a way to inject my JMS Template into activiti:class?

ServiceTask which use activiti:class

<serviceTask id="servicetask2" name="ServiceTask" activiti:class="com.ptvag.activiti.task.ImpactServiceTask" activiti:extensionId="com.ptvag.activiti.task.ImpactServiceTaskUi"></serviceTask>

ServiceTask which use activiti:expression

<serviceTask id="servicetask2" name="ServiceTask" activiti:delegateExpression="${serviceCall}" activiti:extensionId="com.ptvag.activiti.task.ImpactServiceTaskUi"></serviceTask>

Userinterface Eclipse Modeler

package com.foo.activiti.task;

import org.activiti.designer.integration.servicetask.AbstractCustomServiceTask;
import org.activiti.designer.integration.servicetask.PropertyType;
import org.activiti.designer.integration.servicetask.annotation.Help;
import org.activiti.designer.integration.servicetask.annotation.Property;
import org.activiti.designer.integration.servicetask.annotation.Runtime;


@Runtime(delegationClass = "com.foo.activiti.task.ServiceTask")
public class ImpactServiceTaskUi extends AbstractCustomServiceTask {

  private static final String HELP_JMX_LONG = "Name of the JMX worker queue.";

  @Property(type = PropertyType.TEXT, displayName = "JMX Queue", defaultValue = "jmx example queue")
  @Help(displayHelpShort = "JMX Queue", displayHelpLong = HELP_JMX_LONG)
  private String jmxQueue;

  /*
   * (non-Javadoc)
   *
   * @see
   * org.activiti.designer.integration.servicetask.AbstractCustomServiceTask
   * #contributeToPaletteDrawer()
   */
  @Override
  public String contributeToPaletteDrawer() {
    return "Foo";
  }

  @Override
  public String getName() {
    return "ServiceTask";
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.activiti.designer.integration.servicetask.AbstractCustomServiceTask
   * #getSmallIconPath()
   */
  @Override
  public String getSmallIconPath() {
    return "icons/icon.png";
  }
}

Task implementation

package com.foo.activiti.task;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.activiti.engine.impl.bpmn.behavior.TaskActivityBehavior;
import org.activiti.engine.impl.el.Expression;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class ServiceTask extends TaskActivityBehavior {
   
    @Autowired
    private JmsTemplate jmsTemplate;
   
    /**
     * Class logger.
     */
    private static final Logger LOG = Logger.getLogger(ServiceTask.class);
   
   
    public Expression jmxQueue;
   
    @Override
    public void execute(ActivityExecution execution)
    {      
        LOG.info("Executing proceess with id " + execution.getProcessInstanceId());
        String executionId = execution.getId();
        String processInstanceId = execution.getProcessInstanceId();
        StringBuilder textMessage = new StringBuilder();
        textMessage.append("ProcessInstanceId=");
        textMessage.append(processInstanceId).append("\n");
        textMessage.append("ExecutionId=");
        textMessage.append(executionId).append("\n");
        final String textMessageString = textMessage.toString();
        MessageCreator message = new MessageCreator() {

            @Override
            public Message createMessage(Session session) throws JMSException
            {
                TextMessage message = session.createTextMessage(textMessageString);
                return message;
            }
        };
         
        jmsTemplate.send(jmxQueue.getExpressionText(), message);
    }

    @Override
    public void signal(ActivityExecution execution, String signalName, Object signalData) throws Exception
    {
       leave(execution);
    }
}
8 REPLIES 8

tiesebarrell
Champ in-the-making
Champ in-the-making
Hi Michael,

the reason it's not injected is that the Spring injection is not used for activiti:class . That explains why setting it to delegateExpression manually resolves the problem. But I guess you figured that out already Smiley Happy

It's pretty easy to make this available I suppose - there was simply never a request for it before. I would propose that instead of

@Runtime(delegationClass = "com.foo.activiti.task.ServiceTask")
we also allow

@Runtime(delegationExpression = "${myBeanExpression.method(execution)}")
This would then be generated as expected in the XML.

Slightly harder to figure out is what to do about injection of the other @Property fields of the extension class. But maybe that could be added later. Could you file a request for this in Jira and report back the number here so I can look into it?

mpriess
Champ in-the-making
Champ in-the-making
Thanks, but which version support delegationExpression? If I add DelegateExpression to the Runtime annotation eclipse say this element is unknown.

tiesebarrell
Champ in-the-making
Champ in-the-making
This attribute name is part of the extension you would create. We use "delegationClass", so using "delegationExpression" seems a logical name for the attribute we would add. The actual BPMN XML generated would have the "activiti:delegateExpression" attribute as defined here: http://activiti.org/userguide/index.html#bpmnJavaServiceTask

mpriess
Champ in-the-making
Champ in-the-making
I understand how the xml generation works but Runtime actually doesn't support element delegationExpression.

https://github.com/Activiti/Activiti-Designer/blob/master/org.activiti.designer.integration/src/main...

tiesebarrell
Champ in-the-making
Champ in-the-making
Sorry, I don't think I made myself clear. We don't support it now, but I'm suggesting the feature be added. We need to build this. Can you create a Jira issue?

mpriess
Champ in-the-making
Champ in-the-making

tiesebarrell
Champ in-the-making
Champ in-the-making
Thanks.

tiesebarrell
Champ in-the-making
Champ in-the-making
This has been added for the next release and documented in the userguide. I've resolved the issue in Jira.

Thanks Michael for the work on this.