jbpm4 java活動如何使用動態參數

來源:互聯網
上載者:User

在jbpm4中使用java活動的時候我們需要從外部傳入參數,在例子中沒有說明如何?。

下面以jbpm的內建的例子為例:

首先我們修改例子的設定檔:

 

代碼

<?xml version="1.0" encoding="UTF-8"?>

<process name="Java" xmlns="http://jbpm.org/4.4/jpdl">

  <start g="20,20,48,48">
    <transition to="greet" />
  </start>

  <java name="greet"
        class="org.jbpm.examples.java.JohnDoe"
        method="hello"
        var="answer"
        g="96,16,83,52">

    <field name="state"><string value="fine"/></field>
        <arg>
            <ref object="msg"/>
        </arg>
        <!-- 
            <string value="Hi, how are you?"/></arg>
         -->
    <transition to="shake hand" />
  </java>

  <java name="shake hand"
        expr="#{hand}"
        method="shake"
        var="hand"
        g="215,17,99,52">

    <arg><object expr="#{joesmoe.handshakes.force}"/></arg>
    <arg><object expr="#{joesmoe.handshakes.duration}"/></arg>

    <transition to="wait" />
  </java>

  <state name="wait" g="352,17,88,52"/>

</process>

 

 

 

我們修改了xml檔案,在arg把 <string value="Hi, how are you?"/></arg>
 修改為 <ref object="msg"/>
java代碼修改為:

 

代碼

package org.jbpm.examples.java;

import java.util.HashMap;
import java.util.Map;

import org.jbpm.api.ProcessInstance;
import org.jbpm.test.JbpmTestCase;

/**
 * @author Tom Baeyens
 */
public class JavaInstantiateTest extends JbpmTestCase {

  String deploymentId;
  
  protected void setUp() throws Exception {
    super.setUp();
    
    deploymentId = repositoryService.createDeployment()
        .addResourceFromClasspath("org/jbpm/examples/java/process.jpdl.xml")
        .deploy();
  }

  protected void tearDown() throws Exception {
    repositoryService.deleteDeploymentCascade(deploymentId);
    
    super.tearDown();
  }

  public void testJavaInstantiate() {
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("hand", new Hand());
    variables.put("joesmoe", new JoeSmoe());
    variables.put("msg", "Hi, how are you?");
    
    ProcessInstance processInstance = executionService.startProcessInstanceByKey("Java", variables);
    String pid = processInstance.getId();
    
    String answer = (String) executionService.getVariable(pid, "answer");
    assertEquals("I'm fine, thank you.", answer);

    Hand hand = (Hand) executionService.getVariable(pid, "hand");
    assertTrue(hand.isShaken());
  }
}

 

我們在java代碼中加入了 variables.put("msg", "Hi, how are you?"); 其中msg作為java的參數傳入。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.