Technologies used in this article :

  1. Neuro4j Workflows 2.0.1 (spring-jms-adapter plugin)
  2. Spring JMS
  3. ActiveMQ
  4. Neuro4j Studio 1.0 (based on Eclipse Kepler)
  5. JDK 1.6

Expected result: Flow to receive and send message.

Expected result

1. Download project.

Download project and unzip it.

Download java project – SpringJmsExample.zip

 

2. Import project into Studio.

3. Source code:

  • pom.xml - includes spring-jms-adapter plugin and spring-jms
    • spring-jms-adapter provides following blocks:
      • org.neuro4j.springframework.jms.JMSMessageListener
      • org.neuro4j.springframework.jms.JMSQueueSender
      • org.neuro4j.springframework.jms.GetTextMessageCreator
      • org.neuro4j.springframework.jms.CloseApplicationContext
 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.neuro4j.workflow.tutorial</groupId>
	<version>1.0-SNAPSHOT</version>
	<name>SpringJmsExample</name>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>4.1.0.RC1</org.springframework-version>
		<org.aspectj-version>1.8.1</org.aspectj-version>
		<org.slf4j-version>1.6.1</org.slf4j-version>
		<org.neuro4j-version>2.0.1</org.neuro4j-version>
	</properties>
	<repositories>
		<repository>
			<id>spring-releases</id>
			<url>http://repo.spring.io/libs-release</url>
		</repository>
		<repository>
			<id>neuro4j-maven-milestone</id>
			<name>Neuro4J Maven Repository</name>
			<url>http://static.neuro4j.org/maven2</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-releases</id>
			<url>http://repo.spring.io/libs-release</url>
		</pluginRepository>
	</pluginRepositories>

	<artifactId>SpringJmsExample</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.neuro4j</groupId>
			<artifactId>neuro4j-workflow-common</artifactId>
			<version>${org.neuro4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.neuro4j</groupId>
			<artifactId>spring-jms-adapter</artifactId>
			<version>${org.neuro4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<version>1.1.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>4.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
			<version>5.9.1</version>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.n4j</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>

	</build>

</project>
  • Application's configuration file- org.neuro4j.springframework.jms.example.Application.java
 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.neuro4j.springframework.jms.example;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.jms.ConnectionFactory;
import javax.jms.MessageListener;

import org.neuro4j.springframework.context.WorkflowBeanFactoryPostProcessor;
import org.neuro4j.springframework.jms.JMSMessageListener;
import org.neuro4j.springframework.jms.JMSQueueSender;
import org.neuro4j.workflow.common.FlowInitializationException;
import org.neuro4j.workflow.common.TriggerNodeFactory;
import org.neuro4j.workflow.common.WorkflowEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.listener.SimpleMessageListenerContainer;
import org.springframework.util.FileSystemUtils;

@Configuration
@EnableAutoConfiguration
public class Application {

    static String mailboxDestination = "mailbox-destination";

    
    @Bean
    JMSMessageListener receiver(ConfigurableApplicationContext context) throws FlowInitializationException {    	
        return  (JMSMessageListener) TriggerNodeFactory.initTriggerNode("org.neuro4j.springframework.jms.flows.MessageFlow-JMSMessageListener");

    }
    
    @Bean
   static WorkflowBeanFactoryPostProcessor getWorkflowBeanFactoryPostProcessor(ConfigurableApplicationContext context)
    {
    	WorkflowBeanFactoryPostProcessor strategy = new WorkflowBeanFactoryPostProcessor(context);        
    	return strategy;
    }

    @Bean
    JMSQueueSender sender(ConnectionFactory connectionFactory) {    	
    	JMSQueueSender sender =  new JMSQueueSender();
    	sender.setConnectionFactory(connectionFactory);
       return sender;
    }

    @Bean
    SimpleMessageListenerContainer container(MessageListener messageListener,
                                             ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setMessageListener(messageListener);
        container.setConnectionFactory(connectionFactory);
        container.setDestinationName(mailboxDestination);
        return container;
    }

    public static void main(String[] args) {

        FileSystemUtils.deleteRecursively(new File("activemq-data"));

        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        
        parameters.put("messageDestination", mailboxDestination);
        parameters.put("message", "Hi Mister!");      
        
        WorkflowEngine.run("org.neuro4j.springframework.jms.flows.MessageFlow-SendMessage", parameters);


        
    }

}
  • Some business code - org.neuro4j.springframework.jms.example.DoSomething.java
        
 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
package org.neuro4j.springframework.jms.example;

import static org.neuro4j.springframework.jms.example.DoSomething.IN_MESSAGE;

import javax.jms.Message;

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.log.Logger;
import org.neuro4j.workflow.node.CustomBlock;

@ParameterDefinitionList(input = { @ParameterDefinition(name = IN_MESSAGE, isOptional = true, type = "javax.jms.Message") }, output = {})
public class DoSomething extends CustomBlock {

	static final String IN_MESSAGE = "message";

	public int execute(FlowContext ctx) throws FlowExecutionException {

		Message message = (Message) ctx.get(IN_MESSAGE);

		Logger.info(this, "Working with message {}", message);

		return NEXT;
	}

}
  • Code to clean out jms files - org.neuro4j.springframework.jms.example.RemoveJmsTmpFiles.java.
 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
package org.neuro4j.springframework.jms.example;

import java.io.File;

import org.neuro4j.workflow.FlowContext;
import org.neuro4j.workflow.common.FlowExecutionException;
import org.neuro4j.workflow.common.ParameterDefinitionList;
import org.neuro4j.workflow.node.CustomBlock;
import org.springframework.util.FileSystemUtils;

/**
 * Remove tmp files.
 * 
 */
@ParameterDefinitionList(input = {}, output = {})
public class RemoveJmsTmpFiles extends CustomBlock {

	public int execute(FlowContext ctx) throws FlowExecutionException {

		FileSystemUtils.deleteRecursively(new File("activemq-data"));

		return NEXT;
	}

}
  • Flow - org.neuro4j.springframework.jms.flows.MessageFlow.n4j

Expected result

 

4. Build project.

mvn clean install

5. Run Application.

mvn spring-boot:run
15:22:38.064  INFO 41020 --- [ Session Task-1] o.n.s.jms.example.DoSomething            : Working with message ActiveMQT
extMessage {commandId = 5, responseRequired = true, messageId = ID:60334-1416867757645-3:2:1:1:1, originalDestination = n
ull, originalTransactionId = null, producerId = ID:60334-1416867757645-3:2:1:1, destination = queue://mailbox-destination
, transactionId = null, expiration = 0, timestamp = 1416867758056, arrival = 0, brokerInTime = 1416867758057, brokerOutTime = 14168
67758061, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, ta
rgetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeli
veryCounter = 0, size = 1044, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstF
orConsumer = false, text = Hi Mister!}

Download Source Code

Download java project – SpringJmsExample.zip