Technologies used in this article :

  1. Neuro4j Workflows 3.1.1
  2. Google Guice 3.0
  3. Maven 4.0
  4. Neuro4j Studio 2.0(based on Eclipse Neon)
  5. JDK 1.8

1. Generate project structure with Maven

Issue below Maven command to create a standard Java project structure.

mvn archetype:generate -DgroupId=org.neuro4j.workflow.guice -DartifactId=GuiceWorkflowExample 
    		-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2. Import project into Eclipse

3. Add Neuro4j Workflow 3.1.1 dependency

Add the Neuro4j Workflow 3.1.1 and Google Guice 3.0 dependencies listed below in Maven’s pom.xml file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.neuro4j.workflow.tutorial</groupId>
	<artifactId>GuiceWorkflowExample</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>GuiceWorkflowExample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.neuro4j.version>3.1.1</org.neuro4j.version>		
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.inject</groupId>
			<artifactId>guice</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.neuro4j</groupId>
			<artifactId>guice-workflow-adapter</artifactId>
			<version>${org.neuro4j.version}</version>
		</dependency>
	</dependencies>
	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.n4j</include>
				</includes>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4. Service Interface

package org.neuro4j.workflow.guice.service;

public interface MessageService {
	String sendMessage(String msg, String receipient);
}

5. Service Implementation.

package org.neuro4j.workflow.guice.service;

/**
 * Test service.
 *
 */
public class MyMessageService implements MessageService {

    public String sendMessage(String msg, String receipient) {
        String message = "Message to " + receipient + " = " + msg;
        System.out.println(message);
        return message;
    }

}

 

6. CustomBlockWithService.java

						
package org.neuro4j.workflow.guice.flows;

import static org.neuro4j.workflow.guice.flows.CustomBlockWithService.*;

import org.neuro4j.workflow.FlowContext;
import org.neuro4j.workflow.common.FlowExecutionException;
import org.neuro4j.workflow.common.ParameterDefinition;
import org.neuro4j.workflow.common.ParameterDefinitionList;
import org.neuro4j.workflow.guice.service.MessageService;
import org.neuro4j.workflow.node.CustomBlock;

import com.google.inject.Inject;

/**
 * This custom block uses Google Guice library for dependency injection.
 * 
 */

@ParameterDefinitionList(
		                 input = { @ParameterDefinition(name = IN_NAME, isOptional = true, type = "java.lang.String") },
                         output = { @ParameterDefinition(name = OUT_MESSAGE, isOptional = true, type = "java.lang.String") })
public class CustomBlockWithService extends CustomBlock {

	static final String IN_NAME = "name";

	static final String OUT_MESSAGE = "message";

	private MessageService service;

	public int execute(FlowContext ctx) throws FlowExecutionException {

		String name = (String) ctx.get(IN_NAME);

		String message = null;

		if (name != null) {
			message = this.service.sendMessage("Hi", name);
		}

		ctx.put(OUT_MESSAGE, message);
		return NEXT;
	}

	@Inject
	public void setService(MessageService svc) {
		this.service = svc;
	}

}

7. Workflow.

8. Client java code

Client's java code to run workflow.

                  
/**
 * 
 */
package org.neuro4j.workflow.guice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neuro4j.workflow.ExecutionResult;
import org.neuro4j.workflow.common.WorkflowEngine;
import org.neuro4j.workflow.guice.service.MessageService;
import org.neuro4j.workflow.guice.service.MyMessageService;

import com.google.inject.AbstractModule;
import com.google.inject.Module;

/**
 *
 *
 */
public class App {

    public static void main(String[] args) {
        
        AbstractModule module = new AbstractModule() {

            @Override
            protected void configure() {
                bind(MessageService.class).to(MyMessageService.class);

            }

        };

        List<Module> modules = new ArrayList<Module>();
        modules.add(module);

        
        GuiceCustomBlockInitStrategy initStrategy = new GuiceCustomBlockInitStrategy(modules);
        WorkflowEngine.setCustomBlockInitStrategy(initStrategy);

        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("name", "Mister");

        ExecutionResult result = WorkflowEngine.run("org.neuro4j.workflow.guice.flows.Flow-Start",    parameters);

        if (result.getException() == null) {
            String message = (String) result.getFlowContext().get("message");
            System.out.println("Message: " + message);

        } else {
            result.print();
        }

	}

}

6. Run code

Run App.java using Eclipse.

Result:

Message to World = Hi
Message: Message to World = Hi

Download Source Code

Download java project – GuiceWorkflowExample.zip