随笔 - 441  文章 - 4  评论 - 84  阅读 - 109万 

在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的参数传入。

posted on   自由港  阅读(515)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2008-01-12 sqlldr自定义函数调用
点击右上角即可分享
微信分享提示