不使用Conductor Spring
和之前版本差不多,但使用TaskRunnerConfigurer 替代WorkflowTaskCoordinator
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.netflix.conductor.client.automator.TaskRunnerConfigurer;
import com.netflix.conductor.client.http.TaskClient;
import com.netflix.conductor.client.worker.Worker;
import com.netflix.conductor.common.metadata.tasks.Task;
import com.netflix.conductor.common.metadata.tasks.TaskResult;
/**
* The Class Application.
*
* Class to configure the WorkFlow example.
*
* @author Carlos Salas
* @since 1.0
* @version 1.0
*/
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
TaskClient taskClient = new TaskClient();
taskClient.setRootURI("http://192.168.157.140:8080/api/"); //Point this to the server API
int threadCount = 2; //number of threads used to execute workers. To avoid starvation, should be same or more than number of workers
Worker worker1 = new LeaderRatifyWorker("leaderRatify");
Worker worker2 = new ManagerRatifyWorker("managerRatify");
ArrayList<Worker> works=new ArrayList<Worker>();
works.add(worker2);
works.add(worker1);
// Create TaskRunnerConfigurer
TaskRunnerConfigurer configurer = new TaskRunnerConfigurer.Builder(taskClient, works)
.withThreadCount(threadCount)
.build();
// Start the polling and execution of tasks
configurer.init();
}
}
使用conductor-client-spring
POM.XML内容如下:
<dependency>
<groupId>com.netflix.conductor</groupId>
<artifactId>conductor-common</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.netflix.conductor</groupId>
<artifactId>conductor-client</artifactId>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.netflix.conductor/conductor-client-spring -->
<dependency>
<groupId>com.netflix.conductor</groupId>
<artifactId>conductor-client-spring</artifactId>
<version>3.2.0</version>
</dependency>
spring主函数不用写如何代码,需要修改worker,增加annotation
public class ManagerRatifyWorker implements Worker {
private String taskDefName="managerRatify";
public ManagerRatifyWorker() {
}
配置application.properties,注意api后面一定要加"/"
conductor.worker.pollingInterval=2
conductor.client.rootURI=http://192.168.157.140:8080/api/
conductor.client.threadCount=3
最后增加启动测试一个工作流的主代码如下:
@SpringBootApplication
public class SpringApplicationMain implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringApplicationMain.class, args);
}
@Autowired
TaskRunnerConfigurer taskRunnerConfigurer;
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
// ClientConfig config=new ClientConfig();
// 没有找到伪同步模式获取返回
WorkflowClient workflowClient = new WorkflowClient();
workflowClient.setRootURI("http://192.168.157.140:8080/api/");
StartWorkflowRequest request = new StartWorkflowRequest().withName("Leave").withVersion(3);
String workflowId = workflowClient.startWorkflow(request);
Thread.sleep(1000);
System.err.println(workflowId);
Workflow workflow_json = workflowClient.getWorkflow(workflowId, false);
while (workflow_json != null) {
if (workflow_json.getStatus().equals(WorkflowStatus.RUNNING)) {
Thread.sleep(10);
workflow_json = workflowClient.getWorkflow(workflowId, false);
} else {
break;
}
}
System.err.println("workflow_json:" + workflow_json.getStatus().name() + workflow_json.getOutput().toString());
System.exit(0);
}