יום חמישי, 27 ביוני 2013

Write and read from mongodb using spring integration

I extend the spring integration demo:Samples (Basic) – MongoDb and add a read capabilities to the MongoDbOutboundAdapterDemo class.

The spring context

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: <beans xmlns="http://www.springframework.org/schema/beans"
  3:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4:   xmlns:int="http://www.springframework.org/schema/integration"
  5:   xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
  6:   xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  7:         xmlns:beans="http://www.springframework.org/schema/beans"
  8:   xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
  9:     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
 10:     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 11:                 http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
 12: 
 13:   <mongo:db-factory id="mongoDbFactory" dbname="test"/>
 14: 
 15:   <int-mongodb:outbound-channel-adapter id="deafultAdapter" />
 16:       
 17:   <int-mongodb:outbound-channel-adapter id="adapterWithConverter" mongo-converter="stringConverter"/>
 18: 
 19:   <int-mongodb:inbound-channel-adapter id="simpleInboundAdapter" channel="outputChannel" 
 20:                        query="{address.state : 'CA'}">    
 21:             <int:poller fixed-rate="1000" max-messages-per-poll="1"/>
 22:   </int-mongodb:inbound-channel-adapter>
 23: 
 24:     <int:publish-subscribe-channel id="outputChannel">
 25:     </int:publish-subscribe-channel>
 26:  </beans>
 27: 

 The code


  1: package org.springframework.integration.samples.mongodb.outbound;
  2: 
  3: import org.springframework.context.ApplicationContext;
  4: import org.springframework.context.support.FileSystemXmlApplicationContext;
  5: import org.springframework.integration.Message;
  6: import org.springframework.integration.MessageChannel;
  7: import org.springframework.integration.core.MessageHandler;
  8: import org.springframework.integration.core.SubscribableChannel;
  9: import org.springframework.integration.message.GenericMessage;
 10: import org.springframework.integration.samples.mongodb.domain.Address;
 11: import org.springframework.integration.samples.mongodb.domain.Person;
 12: import org.springframework.integration.samples.mongodb.util.DemoUtils;
 13: /**
 14: *
 15: * @author of the orginal source Oleg Zhurakousky
 16: * @Add reading Zvika Peer 
 17: */
 18: public class MongoDbOutboundAdapterDemo {
 19: 
 20: 
 21:   public static void main(String[] args) throws Exception {
 22:     DemoUtils.prepareMongoFactory(); // will clean up MOngoDb
 23:     new MongoDbOutboundAdapterDemo().runDefaultAdapter();
 24:   }
 25: 
 26:   public void runDefaultAdapter() throws Exception {
 27: 
 28:                 ApplicationContext context  =
 29:         new FileSystemXmlApplicationContext(
 30:                 "C:\\learn\\spring\\integration\\samples\\basic\\mongodb\\src\\main\\java\\org\\springframework\\integration\\samples\\mongodb\\outbound\\mongodb-out-config.xml"
 31:                         );
 32:                         
 33:       MessageChannel messageChannel = context.getBean("deafultAdapter", MessageChannel.class);
 34:       messageChannel.send(new GenericMessage<Person>(this.createPersonA()));
 35:       messageChannel.send(new GenericMessage<Person>(this.createPersonB()));
 36:       messageChannel.send(new GenericMessage<Person>(this.createPersonC()));
 37: 
 38:              SubscribableChannel outputChannel = context.getBean("outputChannel", SubscribableChannel.class);
 39: 
 40:             outputChannel.subscribe(new MessageHandler()
 41:             {
 42:                 @Override
 43:                 public void handleMessage(Message<?> msg)
 44:                 {
 45:                    System.out.println("==> HelloWorldDemo Async message: " + msg.getPayload());
 46:                 }
 47:             });
 48:                 
 49:               Thread.sleep(4000);
 50:             System.out.println("Finished");
 51:         }
 52:   }

The result
Finished
==> HelloWorldDemo Async message: [{ "_id" : { "$oid" : "51c945b3fe3a5df7ab5f7a3d"} , "_class" : "org.springframework.integration.samples.mongodb.domain.Person" , "fname" : "John" , "lname" : "Doe" , "address" : { "street" : "3401 Hillview Ave" , "city" : "Palo Alto" , "zip" : "94304" , "state" : "CA"}}, { "_id" : { "$oid" : "51c945b3fe3a5df7ab5f7a3e"} , "_class" : "org.springframework.integration.samples.mongodb.domain.Person" , "fname" : "Josh" , "lname" : "Doe" , "address" : { "street" : "123 Main st" , "city" : "San Francisco" , "zip" : "94115" , "state" : "CA"}}]


Resources:
http://static.springsource.org/spring-integration/reference/html/mongodb.html

אין תגובות:

הוסף רשומת תגובה