cancel
Showing results for 
Search instead for 
Did you mean: 

Java Service Task implementation

prabhjot
Champ in-the-making
Champ in-the-making
Trying to implement a workflow in which it will have a Java service task in which i am fetching data from mysql DB and then i have user task which will have form where i need to populate all fields with data coming from Service task . Please tell me how to forward data from Service task to user task.

Pasting my BPMN file for reference :-

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlnsSmiley Surprisedmgdc="http://www.omg.org/spec/DD/20100524/DC" xmlnsSmiley Surprisedmgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="LoanActiviti" name="LoanActiviti">
    <documentation>Place documentation for the 'LoanActiviti' process here.</documentation>
    <startEvent id="startevent1" name="Start"></startEvent>
    <serviceTask id="servicetask1" name="Service Task" activiti:class="org.activiti.examples.bpmn.servicetask.SysoutDelegate">
      <extensionElements>
        <activiti:field name="customerNameVar">
          <activiti:expression>${customerNameVar}</activiti:expression>
        </activiti:field>
      </extensionElements>
    </serviceTask>
    <userTask id="usertask1" name="User Task" activiti:assignee="management">
      <extensionElements>
        <activiti:formProperty id="customerName" name="Customer name" type="string" variable="${customerNameVar}"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <sequenceFlow id="flow2" name="" sourceRef="servicetask1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow3" name="" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
  </process>
</definitions>


Also when i put field value in service task it gives below error :-

com.vaadin.event.ListenerMethod$MethodException
Cause: org.activiti.engine.ActivitiException: Field definition uses unexisting field 'customerNameVar' on class org.activiti.examples.bpmn.servicetask.SysoutDelegate
   at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
   at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
   at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1193)
   at com.vaadin.ui.Button.fireClick(Button.java:539)

Please tell me if i am doing it in correct way.
3 REPLIES 3

swapnonil
Champ in-the-making
Champ in-the-making
Does your class SysoutDelegate have a field named "customerNameVar".

From what I understand you are trying to inject a value into a field of this class, but this field does not exist.

From the user guide.
The following code snippet shows how to inject a constant value into a field. Field injection is supported when using the 'class' attribute. Note that we need to declare a 'extensionElements' XML element before the actual field injection declarations, which is a requirement of the BPMN 2.0 XML Schema.

<serviceTask id="javaService"
    name="Java service invocation"
    activiti:class="org.activiti.examples.bpmn.servicetask.ToUpperCaseFieldInjected">
    <extensionElements>
      <activiti:field name="text" stringValue="Hello World" />
  </extensionElements>          
</serviceTask>       
         

The class ToUpperCaseFieldInjected has a field text which is of type org.activiti.engine.delegate.Expression. When calling text.getValue(execution), the configured string value Hello World will be returned.

prabhjot
Champ in-the-making
Champ in-the-making
What i am trying to do is Getting Data from Mysql database in Java service task (Sysoutdelegate.java) and then trying to print those data to User task . how can i achieve this ?

Like i got name in customerNameVar variable from database and i want that to print that name in User Task but i am not able to do it . What approach i should follow for the same .

aziesi
Champ in-the-making
Champ in-the-making
Hi. As I understand you retrieve data from MySQL in the Service Task Delegate and want to assign the data to a process variable, which data you later access in the User Task.
In the UserGuide there is documentation how to assign data to the process Execution, see BPMN 2.0 Constructs - Java Service Task - Implementation (execution.setVariable("myData",  var)). You might also work with JPA entities if you do anyway.
To access the process variables in following external code (like your User Task UI), perhaps you can use something like
Object data = runtimeService.getVariable(processInstance.getId(), "myData"); (not sure if that's the best usage).

Regarding your exception, @swapnonil already pointed you to the possible problem: ensure you already have set the process variable "customerNameVar" before the Service Task and that a your Delegate has a field of name "customerNameVar" of type org.activiti.engine.delegate.Expression, so the injection as defined in your process is possible.