INTSAMPLES-90 added MongoDb Samples

This commit is contained in:
Oleg Zhurakousky
2012-09-24 04:46:28 -04:00
parent e6af41ac8f
commit 901f621795
11 changed files with 665 additions and 0 deletions

72
basic/mongodb/README.md Normal file
View File

@@ -0,0 +1,72 @@
Spring Integration - JDBC Sample
================================
# Overview
This sample provides example of how the Jdbc Adapters can be used.
The example presented covers the following use cases
* Find a Person detail from the database based on the name provided
* Create a new Person record in the table
The first example demonstrates the use of outbound gateway to search for a person record using the
spring integration's jdbc outbound gateway
The second example on other hand demonstrates how the jdbc outbound gateway be used to create a new
Person record and then return the newly created Person record.
This example demonstrates how to make use of the sql parameter source factory to extract
the required values to be inserted/updated/selected in the query provided.
# Getting Started
You can run the application by either
* running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application)
* or from the command line:
- mvn package
- mvn exec:java
Make an appropriate choice for searching a Person or creating a Person
For creating the person record, select the appropriate steps as prompted by the application
On creation of Person records, you may choose the option of selecting the created person records and view their details
#Some details about the sample "Person Outbound Gateway"
We use the outbound gateway to insert records in a Person table based on the values contained
in the message payload that is received over the channel to the adapter.
The following are used to configure the gateway
* The request and reply channels
* The data source for the database
* The the update/insert statement to be executed.
* Optional request SQL Parameter source factory
* The select query to be executed after the insert is done
* Optional reply SQL Parameter source factory
* RowMapper if you intend to map the ResultSet to your custom object
The following sequence of events happen when we invoke the createPerson method on the gateway
* The parameter of type Person is sent as a payload of a message over the reply-channel
* The outbound gateway reads this message and extracts the payload
* It then executes the given insert statement, the values to be inserted are derived from the payload
request-sql-parameter-source-factory provided is used to generate a parameter source.
* Since we expect an identity column to be generated, keys-generated is set to true
* The result of the insert is then use to create a reply sql parameter source,
the factory reply-sql-parameter-source-factory generates the required parameter source
* The select query provided is executed
* The ResultMapper is used to convert this ResultSet to a Person object
* The Person object is then sent as a payload of the Message over the reply channel.
* The Person payload is extracted from the Message and returned to the calling application.
For executing the program and see the results, execute the junit test case
org.springframework.integration.samples.jdbc.OutboundGatewayTest
# Resources
For help please take a look at the Spring Integration documentation:
http://www.springsource.org/spring-integration

143
basic/mongodb/pom.xml Normal file
View File

@@ -0,0 +1,143 @@
<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.springframework.integration.samples</groupId>
<artifactId>mongodb</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Samples (Basic) - MongoDb</name>
<url>http://www.springsource.org/spring-integration</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.2.0.RC1</spring.integration.version>
<log4j.version>1.2.16</log4j.version>
<junit.version>4.10</junit.version>
</properties>
<repositories>
<repository>
<id>repo.springsource.org.milestone</id>
<name>Spring Framework Maven Milestone Repository</name>
<url>https://repo.springsource.org/milestone</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<mainClass>org.springframework.integration.samples.jdbc.Main</mainClass>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<versionRange>
[2.3.2,)
</versionRange>
<goals>
<goal>testCompile</goal>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring Integration -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mongodb</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mongodb.domain;
/**
*
* @author Oleg Zhurakousky
*/
public class Address {
private String street;
private String city;
private String zip;
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mongodb.domain;
/**
*
* @author Oleg Zhurakousky
*/
public class Person {
private String fname;
private String lname;
private Address address;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mongodb.inbound;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.samples.mongodb.outbound.MongoDbOutboundAdapterDemo;
import org.springframework.integration.samples.mongodb.util.DemoUtils;
/**
*
* @author Oleg Zhurakousky
*/
public class MongoDbInboundAdapterDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
DemoUtils.prepareMongoFactory(); // will clean up MongoDb
new MongoDbOutboundAdapterDemo().runDefaultAdapter(); // will load data into MongoDb
new MongoDbInboundAdapterDemo().runDefaultAdapter();
}
public void runDefaultAdapter() throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("mongodb-in-config.xml", MongoDbInboundAdapterDemo.class);
Thread.sleep(3000);
context.stop();
}
}

View File

@@ -0,0 +1,24 @@
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
<int-mongodb:inbound-channel-adapter id="simpleInboundAdapter" channel="splittingChannel"
query="{address.state : 'CA'}">
<int:poller fixed-rate="60000" max-messages-per-poll="1"/>
</int-mongodb:inbound-channel-adapter>
<int:splitter input-channel="splittingChannel" output-channel="logger"/>
<int:logging-channel-adapter id="logger" level="WARN"/>
</beans>

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mongodb.outbound;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.samples.mongodb.domain.Address;
import org.springframework.integration.samples.mongodb.domain.Person;
import org.springframework.integration.samples.mongodb.util.DemoUtils;
/**
*
* @author Oleg Zhurakousky
*/
public class MongoDbOutboundAdapterDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
DemoUtils.prepareMongoFactory(); // will clean up MOngoDb
new MongoDbOutboundAdapterDemo().runDefaultAdapter();
// new MongoDbOutboundAdapterDemo().runAdapterWithConveter();
}
public void runDefaultAdapter() throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("mongodb-out-config.xml", MongoDbOutboundAdapterDemo.class);
MessageChannel messageChannel = context.getBean("deafultAdapter", MessageChannel.class);
messageChannel.send(new GenericMessage<Person>(this.createPersonA()));
messageChannel.send(new GenericMessage<Person>(this.createPersonB()));
messageChannel.send(new GenericMessage<Person>(this.createPersonC()));
}
public void runAdapterWithConveter() throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("mongodb-out-config.xml", MongoDbOutboundAdapterDemo.class);
MessageChannel messageChannel = context.getBean("adapterWithConverter", MessageChannel.class);
messageChannel.send(new GenericMessage<String>("John, Dow, Palo Alto, 3401 Hillview Ave, 94304, CA"));
}
private Person createPersonA(){
Address address = new Address();
address.setCity("Palo Alto");
address.setStreet("3401 Hillview Ave");
address.setZip("94304");
address.setState("CA");
Person person = new Person();
person.setFname("John");
person.setLname("Doe");
person.setAddress(address);
return person;
}
private Person createPersonB(){
Address address = new Address();
address.setCity("San Francisco");
address.setStreet("123 Main st");
address.setZip("94115");
address.setState("CA");
Person person = new Person();
person.setFname("Josh");
person.setLname("Doe");
person.setAddress(address);
return person;
}
private Person createPersonC(){
Address address = new Address();
address.setCity("Philadelphia");
address.setStreet("2323 Market st");
address.setZip("19152");
address.setState("PA");
Person person = new Person();
person.setFname("Jane");
person.setLname("Doe");
person.setAddress(address);
return person;
}
}

View File

@@ -0,0 +1,28 @@
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
<int-mongodb:outbound-channel-adapter id="deafultAdapter"/>
<int-mongodb:outbound-channel-adapter id="adapterWithConverter"/>
<!-- mongo-converter="stringConverter" -->
<bean id="stringConverter" class="org.springframework.integration.samples.mongodb.util.StringConverter">
<constructor-arg ref="mongoDbFactory"/>
<constructor-arg>
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mongodb.util;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.Mongo;
/**
*
* @author Oleg Zhurakousky
*/
public class DemoUtils {
public static MongoDbFactory prepareMongoFactory(String... additionalCollectionToDrop) throws Exception{
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new Mongo(), "test");
MongoTemplate template = new MongoTemplate(mongoDbFactory);
template.dropCollection("messages");
template.dropCollection("data");
for (String additionalCollection : additionalCollectionToDrop) {
template.dropCollection(additionalCollection);
}
return mongoDbFactory;
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.mongodb.util;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
*
* @author Oleg Zhurakousky
*/
public class StringConverter extends MappingMongoConverter {
public StringConverter(
MongoDbFactory mongoDbFactory,
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
super(mongoDbFactory, mappingContext);
}
@Override
public void write(Object source, DBObject target) {
String strPerson = (String) source;
String[] parsedStrPerson = StringUtils.tokenizeToStringArray(strPerson, ",");
target.put("fname", parsedStrPerson[0]);
target.put("lname", parsedStrPerson[1]);
DBObject innerObject = new BasicDBObject();
innerObject.put("city", parsedStrPerson[2]);
innerObject.put("street", parsedStrPerson[3]);
innerObject.put("zip", parsedStrPerson[4]);
innerObject.put("state", parsedStrPerson[5]);
target.put("address", innerObject);
}
@SuppressWarnings("unchecked")
@Override
public <S> S read(Class<S> clazz, DBObject source) {
StringBuffer buffer = new StringBuffer();
buffer.append(source.get("fname") + ", ");
buffer.append(source.get("lname") + ", ");
buffer.append(source.get("city") + ", ");
buffer.append(source.get("street") + ", ");
buffer.append(source.get("zip") + ", ");
buffer.append(source.get("state") + ", ");
return (S) buffer.toString();
}
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework.integration">
<level value="warn" />
</logger>
<logger name="org.springframework.integration.samples">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>