Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Parallel execution in workflow

Let's create simple SpringBoot application which will download documents from web in parallel way

SpringBoot configuration

SpringBoot configuration file defines WorkflowEngine and SpringContextInitStrategy which allows to initialize component inside workflow Spring's BeanFactory

public class AppConfig {

	SpringContextInitStrategy initStrategy;

	public WorkflowEngine getWorkflowEngine() {
		return new WorkflowEngine(new ConfigBuilder().withCustomBlockInitStrategy(initStrategy));



Controller will process "/download" requests and run "org.neuro4j.workflow.DownloadPages-Start" workflow

public class WelcomeController {

	private WorkflowEngine engine;

	public String welcome(Map<String, Object> model) {
		return "welcome";

	public String download(Map<String, Object> model) {

		WorkflowRequest request = new WorkflowRequest(model);
		request.addParameter("url1", "");
		request.addParameter("url2", "");
		request.addParameter("url3", "");
		ExecutionResult result = engine.execute("org.neuro4j.workflow.DownloadPages-Start", request);
		return "download";


Workflow after Fork node will run sub-flows in 3 different threads and will wait at Join node. After Join node it will continue execution in 1 "main" thread.


CustomBlock DownloadPage

This block defines business functionality and will call DownloadService to get content from remote server. DownloadPage has @Component annotation which allows to use Spring's BeanFactory to initialize all services defined in block (like DownloadService).

@ParameterDefinitionList(input = {
		@ParameterDefinition(name = IN_URL, isOptional = true, type = "java.lang.String") }, output = {
				@ParameterDefinition(name = OUT_DOCUMENT, isOptional = true, type = "java.lang.String") })
@CachedNode(type = SINGLETON)
public class DownloadPage implements ActionBlock {

	private static final Logger Logger = LoggerFactory.getLogger(DownloadPage.class);

	static final String IN_URL = "url";

	static final String OUT_DOCUMENT = "document";

	private DownloadService service;

	public int execute(FlowContext ctx) throws FlowExecutionException {

		String url = (String) ctx.get(IN_URL);

		Logger.debug("Downloading page {}", url);

		String content = "";

		try {

			content =;

		} catch (IOException e) {
			Logger.error("Error during downloading url {}", url, e);

		ctx.put(OUT_DOCUMENT, content);

		return NEXT;



Will be initialized by Spring and assigned to DownloadPage block

public class JsoupDownloadService implements DownloadService{

	public String download(String url) throws IOException {
		Document doc = Jsoup.connect(url).validateTLSCertificates(false).get();
		if (doc != null) {
			String content = doc.html();
			return content;
		return "";


Running application

Run application with command

 mvn clean install spring-boot:run

Open http://localhost:8080/download


You can’t perform that action at this time.