activiti shell 任务

shell 任务用于执行shell 脚本,在Service TAsk 中配置 ,可以用javaf提供的api来执行shell命令
需要使用java.lang.Runtime为原exec()执行shell,也可用java.lang.ProcessBuilder来执行

ProcessBuilder例子:
public class JavaShellTest{
	public static void main(String[] args) throws IOException {
		List<String> list=new ArrayList<>();
		list.add("cmd");
		list.add("/c");
		list.add("echo");
		list.add("hello");
		
		ProcessBuilder builder=new ProcessBuilder(list);
		//执行命令
		Process process = builder.start();
		String result=convertStream(process.getInputStream());
		System.out.println(result);
	}
	
	public static String convertStream(InputStream is) throws IOException{
		if(is!=null) {
			Writer writer=new StringWriter();
			char[] buffer =new char[1024];
			try {
				Reader reader= new BufferedReader(new InputStreamReader(is, "UTF-8"));
				int n;
				while((n=reader.read(buffer))!=-1) {
					writer.write(buffer,0,buffer.length);
				}
			}finally {
				is.close();
			}
			return writer.toString();
		}else {
			return "";
		}
	}



2. 现在在bpmn中添加配置
<serviceTask id="serviceTask1" name="service task" activiti:type="shell">
    <extensionElement>
        <activiti:field name="command" stringValue="cmd"/>
        <activiti:field name="arg1" stringValue="/c"/>
        <activiti:field name="arg2" stringValue="echo"/>
        <activiti:field name="arg3" stringValue="%JAVA_HOME%"/>
        <activiti:field name="outputVariable" stringValue="javaHome"/>
    </extensionElement>
</serviceTask>


3. 通过runtimeService获取就行了
ProcessDefinition pd=rs.createProcessDefinitionQuery().deployment(deploy.getId()).singleResult();

ProcessInstance pi=runtimeService.startProcessInstanceById(pd.getId());

runtimeService.getVariable(pi.getId(),"javaHome");

猜你喜欢

转载自blog.csdn.net/ldqchat/article/details/81869084