diff --git a/basic/mongodb/README.md b/basic/mongodb/README.md new file mode 100644 index 00000000..d065f02c --- /dev/null +++ b/basic/mongodb/README.md @@ -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 + diff --git a/basic/mongodb/pom.xml b/basic/mongodb/pom.xml new file mode 100644 index 00000000..65514998 --- /dev/null +++ b/basic/mongodb/pom.xml @@ -0,0 +1,143 @@ + + 4.0.0 + + org.springframework.integration.samples + mongodb + 2.1.0.BUILD-SNAPSHOT + jar + + Samples (Basic) - MongoDb + http://www.springsource.org/spring-integration + + + UTF-8 + 2.2.0.RC1 + 1.2.16 + 4.10 + + + + + repo.springsource.org.milestone + Spring Framework Maven Milestone Repository + https://repo.springsource.org/milestone + + + + + + + maven-eclipse-plugin + 2.8 + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + -Xlint:all + true + true + + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + org.springframework.integration.samples.jdbc.Main + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.apache.maven.plugins + + + maven-compiler-plugin + + + [2.3.2,) + + + testCompile + compile + + + + + + + + + + + + + + + + + + + + junit + junit + ${junit.version} + test + + + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + + org.springframework.integration + spring-integration-mongodb + ${spring.integration.version} + + + + + + log4j + log4j + ${log4j.version} + + + + org.slf4j + slf4j-log4j12 + 1.6.1 + + + diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/domain/Address.java b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/domain/Address.java new file mode 100644 index 00000000..da81e64c --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/domain/Address.java @@ -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; + } + +} diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/domain/Person.java b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/domain/Person.java new file mode 100644 index 00000000..f64ecd47 --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/domain/Person.java @@ -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; + } + +} diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/inbound/MongoDbInboundAdapterDemo.java b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/inbound/MongoDbInboundAdapterDemo.java new file mode 100644 index 00000000..287a5eda --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/inbound/MongoDbInboundAdapterDemo.java @@ -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(); + } +} diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/inbound/mongodb-in-config.xml b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/inbound/mongodb-in-config.xml new file mode 100644 index 00000000..348ba872 --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/inbound/mongodb-in-config.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/outbound/MongoDbOutboundAdapterDemo.java b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/outbound/MongoDbOutboundAdapterDemo.java new file mode 100644 index 00000000..978e9f3d --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/outbound/MongoDbOutboundAdapterDemo.java @@ -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(this.createPersonA())); + messageChannel.send(new GenericMessage(this.createPersonB())); + messageChannel.send(new GenericMessage(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("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; + } +} diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/outbound/mongodb-out-config.xml b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/outbound/mongodb-out-config.xml new file mode 100644 index 00000000..c224bd31 --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/outbound/mongodb-out-config.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/util/DemoUtils.java b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/util/DemoUtils.java new file mode 100644 index 00000000..1150977c --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/util/DemoUtils.java @@ -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; + } +} diff --git a/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/util/StringConverter.java b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/util/StringConverter.java new file mode 100644 index 00000000..fb3e377d --- /dev/null +++ b/basic/mongodb/src/main/java/org/springframework/integration/samples/mongodb/util/StringConverter.java @@ -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, 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 read(Class 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(); + } +} + diff --git a/basic/mongodb/src/main/resources/log4j.xml b/basic/mongodb/src/main/resources/log4j.xml new file mode 100644 index 00000000..40d94ca5 --- /dev/null +++ b/basic/mongodb/src/main/resources/log4j.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file