Jump to: navigation, search

Difference between revisions of "Stardust/Knowledge Base/Integration/Spring Integration"

Line 50: Line 50:
 
<property name="archiver" ref="ippDmsFileArchiver" />
 
<property name="archiver" ref="ippDmsFileArchiver" />
 
</bean>
 
</bean>
 
 
 
</pre><pre>&lt;bean id="ippDmsFileArchiver"
 
</pre><pre>&lt;bean id="ippDmsFileArchiver"
  
Line 59: Line 57:
 
&lt;property name="documentType" value="Text File"/&gt;
 
&lt;property name="documentType" value="Text File"/&gt;
 
&lt;/bean&gt;
 
&lt;/bean&gt;
 
 
 
</pre>
 
</pre>
  

Revision as of 07:12, 27 November 2011

Spring Integration

Spring Integration is a light-weight framework that follows the Spring programming model and aims to implement well-known Enterprise Integration Patterns. It allows you to declaratively create a chain of message consuming and message producing components with capabilities for message routing, filtering, etc.

You can easily use existing Pojos as parts of this 'message bus' without any need to change them or you can extend the framework's adapters to have greater control over the message handling behavior.The following is an example for using Spring Integration with Stardust involving the handling of incoming files and a document repository.

File Archiver Example

This example consists of a Spring Integration solution with 3 components: The first is capable of monitoring a directory for incoming files, the second archives the file into a folder inside a JCR content repository (using Stardust's Document Management Service), and the third component starts a process in the Stardust runtime passing in the ID of the archived document to create an attachment.

Inbound File Adapter

This is an out-of-the-box Spring Integration adapter which monitors a directory for new files. Here's a configuration example in which a directory is polled every 3 seconds for new files starting with the name "test*" and every file processed is cached in a filter so it's not picked up a again in the next polling interval.. You can also see that every detected file is sent to the "fileMessages" channel.

<!-- Inbound adapter reading files from a directory -->
<file:inbound-channel-adapter
	directory="file:C:/temp/incoming"
	channel="fileMessages"
	filter="incomingFileFilter" />
<!-- Poller for incoming files -->

<si:poller default="true" max-messages-per-poll="10"> <si:interval-trigger interval="3" time-unit="SECONDS" />


</si:poller>
<!-- Customized composite filter for incoming files -->

<bean id="incomingFileFilter" class="org.springframework.integration.file.CompositeFileListFilter"> <constructor-arg> <list> <bean class="org.springframework.integration.file.AcceptOnceFileListFilter" /> <bean class="org.springframework.integration.file.PatternMatchingFileListFilter"> <constructor-arg value="^test.*$"/> </bean> </list> </constructor-arg> </bean>


JCR File Archive Service

In the below example, first of all, you see how a Spring Integration service-activator is defined to consume messages on the input-channel "fileMessages" using the custom jcrFileArchiveService. The output (wich consists merely of the JCR ID of the archived document) is then sent on to the output-channel "jcrFileMessages".
The JcrFileArchiveService class is a custom component which provides a thin wrapper around the Stardust DMS File Archiver and is capable of handling a message with a file payload. The wrapped Stardust DMS File Archiver connects to Stardust via Spring Remoting or EJB Remoting and it uses the DocumentManagementService to archive a document in a specific, configurable location.

<!-- JCR file archiving service -->
<si:service-activator
	input-channel="fileMessages"
	output-channel="jcrFileMessages"
	ref="jcrFileArchiveService"/>
<bean id="jcrFileArchiveService"

class="com.infinity.bpm.spring.integration.file.JcrFileArchiveService"> <property name="archiver" ref="ippDmsFileArchiver" /> </bean>

<bean id="ippDmsFileArchiver"

class="com.infinity.bpm.spring.integration.IppDmsFileArchiver"> <property name="ippServiceWrapper" ref="ippServiceWrapper"/> <property name="createOperator" value="Automatic Archival"/> <property name="documentType" value="Text File"/> </bean>

Stardust Integration Controller

The last service in the sequence is configured to consume the messages on the "jcrFileMessages" channel containing the unique JCR document ID which was the output of the JCR File Archive Service. Since the payload of the message is a String, a simple POJO is used which takes the JCR document ID and passes it into Stardust as an argument when starting a process.

<!-- Startdust start process service -->
<si:service-activator
	input-channel="jcrFileMessages"
	ref="ippIntegrationController"
	method="startProcessWithDocumentId"/>
<bean id="ippIntegrationController"

class="com.infinity.bpm.spring.integration.IppIntegrationController"> <property name="startProcessDefinitionId" value="FileDemo"/> <property name="dataIdDocumentId" value="JCRDocumentID"/> <property name="ippServiceController" ref="ippServiceWrapper"/> </bean>


Installation How-To

To install and run this example, follow these steps:

  • Create a Stardust Dynamic Web Project in Eclipse
  • Create a Spring Integration application context file as below and place it into the directory <your-project-name>/carnot-processportal/WEB-INF/config/ipp/spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:si="http://www.springframework.org/schema/integration"
	   xmlns:file="http://www.springframework.org/schema/integration/file"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/integration
           http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
           http://www.springframework.org/schema/integration/file
		   http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
	<bean id="integrationPropertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:ipp-integration.properties" />
	</bean>
    <!-- Inbound adapter reading files from a directory -->
	<file:inbound-channel-adapter
		directory="file:${inboundFileAdapter.directory}"
		channel="fileMessages"
		filter="incomingFileFilter" />
	<!-- Poller for incoming files -->
	<si:poller default="true" max-messages-per-poll="10">
		<si:interval-trigger interval="3" time-unit="SECONDS" />
	    <!-- si:transactional transaction-manager="txManager"
                  propagation="REQUIRES_NEW"
                  isolation="REPEATABLE_READ"
                  timeout="10000"
                  read-only="false"/-->		
	</si:poller>
	<!-- Queue channel for file messages -->
	<si:channel id="fileMessages">
		<si:queue capacity="10" />
	</si:channel>
	<!-- Queue channel for archived file messages -->
	<si:channel id="jcrFileMessages">
		<si:queue capacity="10" />
	</si:channel>
	<!-- Customized composite filter for incoming files -->
	<bean id="incomingFileFilter"
		class="org.springframework.integration.file.CompositeFileListFilter">
		<constructor-arg>
			<list>
				<bean class="org.springframework.integration.file.AcceptOnceFileListFilter" />
				<bean class="org.springframework.integration.file.PatternMatchingFileListFilter">
					<constructor-arg value="^test.*$"/>
				</bean>
			</list>
		</constructor-arg>
	</bean>              
	<!-- JCR file archiving service -->
	<si:service-activator input-channel="fileMessages"
                      output-channel="jcrFileMessages"
                      ref="jcrFileArchiveService"/>
	<!-- Stardust start process service -->
	<si:service-activator input-channel="jcrFileMessages"
                      ref="ippIntegrationController"
                      method="startProcessWithDocumentId"/>
	<!-- Custom Beans -->
	<bean id="jcrFileArchiveService"
		class="com.infinity.bpm.spring.integration.file.JcrFileArchiveService">
		<property name="archiver" ref="ippDmsFileArchiver" />
	</bean>
	<bean id="ippDmsFileArchiver"
		class="com.infinity.bpm.spring.integration.IppDmsFileArchiver">
		<property name="ippServiceWrapper" ref="ippServiceWrapper"/>
		<property name="createOperator" value="${ippDmsFileArchiver.createOperator}"/>
		<property name="documentType" value="${ippDmsFileArchiver.documentType}"/>
		<property name="rootFolderPath" value="${ippDmsFileArchiver.rootFolderPath}" />
	</bean>
	<bean id="ippIntegrationController"
		class="com.infinity.bpm.spring.integration.IppIntegrationController">
		<property name="startProcessDefinitionId" value="${ippIntegrationController.startProcessDefinitionId}"/>
		<property name="dataIdDocumentId" value="${ippIntegrationController.dataIdDocumentId}"/>
		<property name="ippServiceController" ref="ippServiceWrapper"/>
	</bean>
	<bean id="ippServiceWrapper" class="com.infinity.bpm.spring.integration.IppServiceWrapper">
		<property name="user" value="${ippServiceWrapper.user}" />
		<property name="password" value="${ippServiceWrapper.password}" />
	</bean>
</beans>
  • To avoid that Stardust's Spring application context complains about the placeholders that were added via the ipp-integration-context.xml when starting the server, you should add the property <property name="ignoreUnresolvablePlaceholders" value="true" /> to the property placeholder in the file <your-project-name>/carnot-processportal/WEB-INF/config/ipp/spring/carnot-spring-context.xml, so it looks like this
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="location" value="classpath:carnot.properties" />
</bean>
  • Create an ipp-integration.properties file into the directory <your-project-name>/carnot-processportal/WEB-INF/classes and change properties if you like, for example the location of the incoming directory of the files, as below.
ippServiceWrapper.user=motu
ippServiceWrapper.password=motu
ippDmsFileArchiver.createOperator=Spring Integration
ippDmsFileArchiver.documentType=Test File
ippDmsFileArchiver.documentIdPrefix=Document-
ippDmsFileArchiver.rootFolderPath=/test-root
ippIntegrationController.startProcessDefinitionId=FileDemo
ippIntegrationController.dataIdDocumentId=JCRDocumentID
inboundFileAdapter.directory=C:/tmp/carnot.in
  • Unzip and copy the Spring Integration libraries ipp-spring-integration-0.0.1-SNAPSHOT.jar, spring-integration-core-1.0.3.RELEASE.jar, spring-integration-file-1.0.3.RELEASE.jar, into the directory <your-project-name>/WebContent/WEB-INF/lib.
  • Deploy the web project to a server like Tomcat and start it up.
  • Deploy the FileDemo process model(Attached Project archive) into the Stardust runtime. This model contains a single process definition with a data object called "JCR Document ID" which is used to pass the JCR ID of the archived document into the process. Once you have it in your Stardust process, it can be used to lookup the document handle as shown in the following screenshot.

Stardust Spring Integration

  • Drop a file "test1.txt" into the directory configured in the ipp-integration.properties file. You should see log output of how it is picked up and used to start a process in the Eclipse Console.

Download

The complete Eclipse project containing the source code can be downloaded here.

Note: In order to run the JUnit TestCase in this project, you need an existing Stardust server (e.g. Tomcat RAD deployment). You also need to add the config/ directory to the classpath of the test run and configure the contained carnot.properties according to your environment.