AnsweredAssumed Answered

Integration of own activiti tasks with Spring

Question asked by mpriess on Feb 13, 2013
Latest reply on Mar 3, 2013 by tiesebarrell
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);
    }
}

Outcomes