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

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>