百度上做网站,湖南长沙特色简介,wordpress网站布置,上海装修公司哪家比较划算几个月前#xff0c;在处理一个公司项目时#xff0c;我们需要开发REST服务#xff0c;该服务用于根据客户端应用程序发送的数据发送电子邮件。 在开发此服务期间#xff0c;我们决定创建简单的工作流引擎#xff0c;该引擎将为发送电子邮件收费#xff0c;但该引擎也可用… 几个月前在处理一个公司项目时我们需要开发REST服务该服务用于根据客户端应用程序发送的数据发送电子邮件。 在开发此服务期间我们决定创建简单的工作流引擎该引擎将为发送电子邮件收费但该引擎也可用于任何类型的简单流。 在本文中我将逐步说明如何实现可处理序列流的简单工作流引擎。 为了实现此工作流引擎我们使用了spring框架但是无论使用哪种框架也可以不使用任何框架如何在任何框架上实现该想法都应相同。 我们将从对序列工作流模式的简短介绍开始然后我们将研究所需的接口最后我们将从使用Spring实现工作流引擎开始。 序列工作流程模式 序列工作流程模式描述了其中每个步骤动作一步一步地完成的工作流程。 在下一张图片上您可以看到它的外观 流中要处理的每个动作都共享相同的上下文这使流的参与者之间可以共享信息。 使用公共上下文的想法是因为每个步骤都应该彼此独立并且应该将它们作为其他流程的一部分轻松添加。 如果要获取有关序列工作流程模式的更多信息请访问 序列模式 。 定义所需的界面 下一步是创建一组接口使我们可以轻松创建工作流程并定义工作流程操作。 我们可以从Workflow界面开始。 此接口负责处理工作流程操作实际上它定义了我们的工作流程引擎应该执行的操作。 这是一个非常简单的界面只有一种方法“ processWorkflow”。 此方法由工作流引擎调用用于为工作流提供可在工作流内部使用的初始对象它表示每个工作流的起点。 package ba.codecentric.workflow;import java.util.Map;/*** Process email workflow.** author igor.madjeric**/public interface Workflow {/*** Method for processing workflow.** param parameters* maps of object which are needed for workflow processing* return true in case that workflow is done without errors otherwise false*/public boolean processWorkflow(MapString, Object parameters);}Next what we need is interface used for defining workflow action. This is also simple interface whit only one method too.package ba.codecentric.workflow;
/*** Define workflow action** author igor.madjeric**/public interface WorkflowAction {/*** Execute action.** param context* throws Exception*/public void doAction(Context context) throws Exception;}So this interface define only doAction method which will be called by workflow implementation.Last interface which we need to define is Context interface. This interface define two methods, one for setting object in context and another for retrieving it.package ba.codecentric.workflow;/*** Context interface.** Class which extend this interface should be able to provide mechanism for keeping object in context.br /* So they can be shared between action inside workflow.** author igor.madjeric**/public interface Context {/*** Set value with specified name in context.* If value already exist it should overwrite value with new one.** param name of attribute* param value which should be stored for specified name*/public void setAttribute(String name, Object value);/*** Retrieve object with specified name from context,* if object does not exists in context it will return null.** param name of attribute which need to be returned* return Object from context or null if there is no value assigned to specified name*/public Object getAttribute(String name);} 这是我们为简单工作流程需要定义的所有接口 实施简单的工作流引擎 定义接口后我们可以从实现工作流引擎开始。 引擎应具备的功能有一些要求。 该引擎应支持顺序工作流程这意味着一个接一个地执行动作。 发动机也应该能够进动多于一个的流量。 工作流操作应该能够彼此共享信息。 如我们所见并没有很多要求所以我们应该从实现它开始。 首先我们可以创建上下文类该上下文类将用于处理动作之间的信息。 此类实现Context接口并且不执行其他任何操作。 package ba.codecentric.workflow.impl;import java.util.HashMap;
import java.util.Map;
import ba.codecentric.workflow.Context;/**
* Save states between different workflow action.
*
* author igor.madjeric
*
*/
public class StandardContext implements Context {private MapString, Object context;/*** Create context object based.
*
* param parameters
*/
public StandardContext(MapString, Object parameters) {
if (parameters null) {
this.context new HashMapString, Object();
} else {
this.context parameters;
}
}Override
public Object getAttribute(String name) {
return context.get(name);
}Override
public void setAttribute(String name, Object value) {
context.put(name, value);
}} 第二步是创建实现Workflow接口的类。 我们称此类为StandardWorkflow。 除了实现Workflow接口之外该类还实现了ApplicationContextAware接口因为需要访问spring bean存储库。 如果您不使用spring则不需要实现它。 我们已经说过工作流应该支持一个以上的流程。 因此可以将一个工作流程的操作定义为一个列表并且每个列表都应分配一个逻辑名称。 因此对于动作注册我们可以使用诸如Map StringList WorkflowAction 之类的东西。 首先我们将看到StandardWorkflow和一个自定义流程的spring bean定义然后我们将看到StandardWorkflow的实现。 Bean的StandardWorkflow定义 bean idstandardWorkflowclassde.codecentric.oev.external.services.workflow.standard.StandardWorkflowproperty nameworkflowActionsmap!-- entry keyCID_actionref beanCID_action//entry--!-- OEVBS --entry keyaction1_actionref beanaction1_action //entry!-- PVN --entry keyaction2_actionref beanaction2_action //entry!-- WPV --entry keyaction3_actionref beanaction3_action //entry/map/property/bean 从这个bean定义中我们可以看到我们为每个客户定义了操作并且在引用bean中定义了操作列表。 这是其中一个客户Bean的示例 bean idaction1_action classjava.util.ArrayListconstructor-arg!-- List of Actions --list value-typeba.codecentric.workflow.WorkflowAction ref localcreateEmailAction/ref beansendEmailAction//list/constructor-arg/bean 现在我们可以看到StandardWorkflow的样子 package ba.codecentric.workflow.impl;import java.util.List;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import ba.codecentric.workflow.Context;import ba.codecentric.workflow.Workflow;import ba.codecentric.workflow.WorkflowAction;/*** Define standard workflow for sending email.** see Workflow** author igor.madjeric**/public class StandardWorkflow implements Workflow,ApplicationContextAware {private final Log LOG LogFactory.getLog(StandardWorkflow.class);private static final String ACTION action;private MapString, ListWorkflowAction workflowActions;private ApplicationContext applicationContext;/***see de.codecentric.oev.external.services.workflow.Workflow#processWorkflow(java.util.Map)*/Overridepublic boolean processWorkflow(String workflofName, MapString, Object parameters) {Context context new StandardContext(parameters);ListWorkflowAction actions getWorkflowActions(workflofName);for (WorkflowAction action : actions) {try {action.doAction(context);} catch (Exception e) {StringBuilder message new StringBuilder(
Failed to complete action: action.toString());message.append(\n);message.append(e.getMessage());LOG.error(message.toString());return false;}}return true;}
private ListWorkflowAction getWorkflowActions(String actionName) {ListWorkflowAction actions workflowActions.get(actionName);if (actions null || actions.isEmpty()) {LOG.error(There is no defined action for actionName);throw new IllegalArgumentException(
There is no defined action for actionName);}return actions;}
Overridepublic void setApplicationContext(ApplicationContext applicationContext)
throws BeansException{
this.applicationContext applicationContext;}
// Getter/Setterpublic MapString, ListWorkflowAction getWorkflowActions() {return workflowActions;}
public void setWorkflowActions(MapString, ListWorkflowAction workflowActions) {this.workflowActions workflowActions;}
} 再次您可以看到这也是一个简单的类所有工作都在processWorkflow方法中完成我们向其提供流程名称和输入参数。 此方法使用指定的参数创建Context然后尝试加载为指定的流定义的操作如果存在具有指定名称的流它将开始运行流。 如何开始流程 这取决于您的需要。 您可以使用我们这样的休息服务也可以使用其他任何机制例如MBean预定作业也可以直接从某些服务中进行呼叫。 您需要做的就是调用processWorkflow方法。 参考来自ICG Madjeric博客的JCG合作伙伴 Igor Madjeric的Spring提供的简单工作流引擎 。 翻译自: https://www.javacodegeeks.com/2012/11/simple-workflow-engine-with-spring.html