JBoss 系列三十五:jBPM5示例之 Logging

在实际应用的BPM流程中经常需要日志记录某些信息,jBPM5中的Log Service Tasks可以完成这一任务。Logging流程示意图如下:

JBoss 系列三十五:jBPM5示例之 Logging_第1张图片

如图Logging流程就包括一个Service Tasks节点Log,用来记录日志输出,在设计流程时我们设计日志输出消息为#{msg},即输出流程中的msg变量。

  • Logging流程流程运行代码:https://github.com/kylinsoong/.../LoggingStart.java
  • Logging流程: https://github.com/kylinsoong/.../logging.bpmn
运行 org.jbpm.quickstarts.logging.LoopingStart可运行LoggingStart流程,LoggingStart代码如下

package org.jbpm.quickstarts.logging;

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

import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.quickstarts.QuickStartBase;

public class LoggingStart extends QuickStartBase{

	
	public void test() {
		StatefulKnowledgeSession ksession = createKnowledgeSession("quickstarts/logging.bpmn");
		ksession.getWorkItemManager().registerWorkItemHandler("Log", new LoggingWorkItemHandler());
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("msg", "Test Logging Message");
		ksession.startProcess("org.jbpm.quickstarts.logging", params);
	}
	
	public static void main(String[] args) {
		new LoggingStart().test();
	}

}

如上我们注册WorkItemManager来在运行时完成日志记录工作,jBPM设计的一大特点就是灵活性,本示例我们通过自己实现的日志记录Hander(LoggingWorkItemHandler)来完成运行时日志记录,LoggingWorkItemHandler内容如下:

package org.jbpm.quickstarts.logging;

import java.util.logging.Logger;

import org.drools.process.instance.WorkItemHandler;
import org.drools.runtime.process.WorkItem;
import org.drools.runtime.process.WorkItemManager;

public class LoggingWorkItemHandler implements WorkItemHandler {
	
	private static final Logger logger = Logger.getLogger(LoggingWorkItemHandler.class.getName());
			
	public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
		logger.info(workItem.toString());
		manager.completeWorkItem(workItem.getId(), null);
	}

	public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
		manager.abortWorkItem(workItem.getId());
	}

}

如上我们自定义的 LoggingWorkItemHandler实现了WorkItemHandler接口,日志记录是在executeWorkItem方法中完成,我们使用java.util.logging.Logger完成日志记录,运行LoggingStart会有如下日志输出:

Oct 29, 2013 2:29:10 PM org.jbpm.quickstarts.logging.LoggingWorkItemHandler executeWorkItem
INFO: WorkItem 1 [name=Log, state=0, processInstanceId=1, parameters{Message=Test Logging Message}]


你可能感兴趣的:(jboss,jbpm,logging,BPM,JBPM5)