Spring Batch Mongodb example not working -


i running spring batch example http://www.mkyong.com/spring-batch/spring-batch-example-xml-file-to-database/ without customization, when tried project getting following output. mongodb not shows collection. please guide me asap what's wrong going here.

slf4j: failed load class "org.slf4j.impl.staticloggerbinder". slf4j: defaulting no-operation (nop) logger implementation slf4j: see http://www.slf4j.org/codes.html#staticloggerbinder further details. exit status : unknown 

also mongodb not show collections

> use test switched db test > show collections system.indexes > 

reportconverter.java

package com.mkyong.converter;  import java.math.bigdecimal; import java.text.numberformat; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; import java.util.locale;  import com.mkyong.model.report; import com.thoughtworks.xstream.converters.converter; import com.thoughtworks.xstream.converters.marshallingcontext; import com.thoughtworks.xstream.converters.unmarshallingcontext; import com.thoughtworks.xstream.io.hierarchicalstreamreader; import com.thoughtworks.xstream.io.hierarchicalstreamwriter;  //http://x-stream.github.io/converter-tutorial.html public class reportconverter implements converter {      @override     public boolean canconvert(class type) {         //we need "report" object         return type.equals(report.class);     }      @override     public void marshal(object source, hierarchicalstreamwriter writer, marshallingcontext context) {         // todo auto-generated method stub      }      @override     public object unmarshal(hierarchicalstreamreader reader, unmarshallingcontext context) {          report obj = new report();          //get attribute         obj.setid(integer.valueof(reader.getattribute("id")));         reader.movedown(); //get date          date date = null;         try {             date = new simpledateformat("m/d/yyyy").parse(reader.getvalue());         } catch (parseexception e) {             e.printstacktrace();         }         obj.setdate(date);         reader.moveup();          reader.movedown(); //get impression          string impression = reader.getvalue();         numberformat format = numberformat.getinstance(locale.us);         number number = 0;         try {             number = format.parse(impression);         } catch (parseexception e) {             e.printstacktrace();         }         obj.setimpression(number.longvalue());          reader.moveup();          reader.movedown(); //get click         obj.setclicks(integer.valueof(reader.getvalue()));         reader.moveup();          reader.movedown(); //get earning         obj.setearning(new bigdecimal(reader.getvalue()));         reader.moveup();          return obj;     } } 

database.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:mongo="http://www.springframework.org/schema/data/mongo"      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/data/mongo         http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">      <!-- connect mongodb -->     <mongo:mongo host="127.0.0.1" port="27017" />     <mongo:db-factory dbname="test" />      <bean id="mongotemplate" class="org.springframework.data.mongodb.core.mongotemplate">         <constructor-arg name="mongodbfactory" ref="mongodbfactory" />     </bean>  </beans> 

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">      <!-- stored job-meta in database     <bean id="jobrepository"         class="org.springframework.batch.core.repository.support.jobrepositoryfactorybean">         <property name="datasource" ref="datasource" />         <property name="transactionmanager" ref="transactionmanager" />         <property name="databasetype" value="mysql" />     </bean>      <bean id="transactionmanager"         class="org.springframework.batch.support.transaction.resourcelesstransactionmanager" />     -->      <!-- stored job-meta in memory -->      <bean id="jobrepository"         class="org.springframework.batch.core.repository.support.mapjobrepositoryfactorybean">         <property name="transactionmanager" ref="transactionmanager" />     </bean>      <bean id="transactionmanager"         class="org.springframework.batch.support.transaction.resourcelesstransactionmanager" />       <bean id="joblauncher"         class="org.springframework.batch.core.launch.support.simplejoblauncher">         <property name="jobrepository" ref="jobrepository" />     </bean>  </beans> 

jobreport.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"     xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://www.springframework.org/schema/batch         http://www.springframework.org/schema/batch/spring-batch-2.2.xsd         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/util          http://www.springframework.org/schema/util/spring-util-3.2.xsd">      <batch:job id="reportjob">         <batch:step id="step1">             <batch:tasklet>                 <batch:chunk reader="xmlitemreader" writer="mongodbitemwriter"                     commit-interval="1">                 </batch:chunk>             </batch:tasklet>         </batch:step>     </batch:job>      <bean id="mongodbitemwriter" class="org.springframework.batch.item.data.mongoitemwriter">         <property name="template" ref="mongotemplate" />         <property name="collection" value="report" />     </bean>      <bean id="xmlitemreader" class="org.springframework.batch.item.xml.staxeventitemreader">         <property name="fragmentrootelementname" value="record" />         <property name="resource" value="classpath:xml/report.xml" />         <property name="unmarshaller" ref="reportunmarshaller" />     </bean>      <bean id="reportunmarshaller" class="org.springframework.oxm.xstream.xstreammarshaller">          <property name="aliases">             <util:map id="aliases">                 <entry key="record" value="com.mkyong.model.report" />                  <!--                  <entry key="date" value="java.lang.string" />                 <entry key="impression" value="java.lang.long" />                 <entry key="clicks" value="java.lang.integer" />                 <entry key="earning" value="java.math.bigdecimal" />                  -->             </util:map>         </property>          <property name="converters">             <array>                 <ref bean="reportconverter" />             </array>         </property>      </bean>      <bean id="reportconverter" class="com.mkyong.converter.reportconverter" />  </beans> 

pom.xml;

<properties>         <jdk.version>1.6</jdk.version>         <spring.version>3.2.2.release</spring.version>         <spring.batch.version>2.2.0.release</spring.batch.version>         <spring.data.version>1.2.1.release</spring.data.version>         <mongodb.driver.version>2.11.2</mongodb.driver.version>     </properties>      <dependencies>          <!-- spring core -->         <dependency>             <groupid>org.springframework</groupid>             <artifactid>spring-core</artifactid>             <version>${spring.version}</version>         </dependency>          <!-- spring xml to/back object -->         <dependency>             <groupid>org.springframework</groupid>             <artifactid>spring-oxm</artifactid>             <version>${spring.version}</version>         </dependency>          <!-- spring batch dependencies -->         <dependency>             <groupid>org.springframework.batch</groupid>             <artifactid>spring-batch-core</artifactid>             <version>${spring.batch.version}</version>         </dependency>         <dependency>             <groupid>org.springframework.batch</groupid>             <artifactid>spring-batch-infrastructure</artifactid>             <version>${spring.batch.version}</version>         </dependency>          <!-- spring batch unit test -->         <dependency>             <groupid>org.springframework.batch</groupid>             <artifactid>spring-batch-test</artifactid>             <version>${spring.batch.version}</version>         </dependency>          <!-- mongodb database driver -->         <dependency>             <groupid>org.mongodb</groupid>             <artifactid>mongo-java-driver</artifactid>             <version>${mongodb.driver.version}</version>         </dependency>          <!-- spring data mongodb -->         <dependency>             <groupid>org.springframework.data</groupid>             <artifactid>spring-data-mongodb</artifactid>             <version>${spring.data.version}</version>         </dependency>          <!-- junit -->         <dependency>             <groupid>junit</groupid>             <artifactid>junit</artifactid>             <version>4.11</version>             <scope>test</scope>         </dependency>          <!-- testng -->         <dependency>             <groupid>org.testng</groupid>             <artifactid>testng</artifactid>             <version>6.8.5</version>             <scope>test</scope>         </dependency>      </dependencies> 

i able run code making following changes. please see code pom.xml

<properties>          <!-- generic properties -->         <java.version>1.8</java.version>         <project.build.sourceencoding>utf-8</project.build.sourceencoding>         <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>          <spring.version>4.2.4.release</spring.version>         <spring.batch.version>3.0.6.release</spring.batch.version>         <spring.data.version>1.8.4.release</spring.data.version>         <mongodb.driver.version>3.1.1</mongodb.driver.version>          <!-- logging -->         <logback.version>1.0.13</logback.version>         <slf4j.version>1.7.5</slf4j.version>         <jcl.slf4j.version>1.7.12</jcl.slf4j.version>          <!-- test -->         <junit.version>4.11</junit.version>      </properties>      <dependencies>         <!-- spring core -->         <dependency>             <groupid>org.springframework</groupid>             <artifactid>spring-core</artifactid>             <version>${spring.version}</version>         </dependency>          <!-- spring xml to/back object -->         <dependency>             <groupid>org.springframework</groupid>             <artifactid>spring-oxm</artifactid>             <version>${spring.version}</version>         </dependency>          <!-- spring batch dependencies -->         <dependency>             <groupid>org.springframework.batch</groupid>             <artifactid>spring-batch-core</artifactid>             <version>${spring.batch.version}</version>         </dependency>         <dependency>             <groupid>org.springframework.batch</groupid>             <artifactid>spring-batch-infrastructure</artifactid>             <version>${spring.batch.version}</version>         </dependency>          <!-- spring batch unit test -->         <dependency>             <groupid>org.springframework.batch</groupid>             <artifactid>spring-batch-test</artifactid>             <version>${spring.batch.version}</version>         </dependency>          <!-- mongodb database driver -->         <dependency>             <groupid>org.mongodb</groupid>             <artifactid>mongo-java-driver</artifactid>             <version>${mongodb.driver.version}</version>         </dependency>          <!-- logging slf4j & logback -->         <dependency>             <groupid>org.slf4j</groupid>             <artifactid>slf4j-api</artifactid>             <version>${slf4j.version}</version>             <scope>compile</scope>         </dependency>         <dependency>             <groupid>ch.qos.logback</groupid>             <artifactid>logback-classic</artifactid>             <version>${logback.version}</version>             <scope>runtime</scope>         </dependency>         <dependency>             <groupid>org.slf4j</groupid>             <artifactid>jcl-over-slf4j</artifactid>             <version>${jcl.slf4j.version}</version>         </dependency>          <!-- spring data mongodb -->         <dependency>             <groupid>org.springframework.data</groupid>             <artifactid>spring-data-mongodb</artifactid>             <version>${spring.data.version}</version>         </dependency>          <!-- junit -->         <dependency>             <groupid>junit</groupid>             <artifactid>junit</artifactid>             <version>4.11</version>             <scope>test</scope>         </dependency>          <!-- testng -->         <dependency>             <groupid>org.testng</groupid>             <artifactid>testng</artifactid>             <version>6.8.5</version>             <scope>test</scope>         </dependency>     </dependencies> 

reportconverter.java

public class reportconverter implements converter {      @override     public boolean canconvert(class type) {         //we need "report" object         return type.equals(report.class);     }      @override     public void marshal(object source, hierarchicalstreamwriter writer, marshallingcontext context) {     }      @override     public object unmarshal(hierarchicalstreamreader reader, unmarshallingcontext context) {          report obj = new report();          //get attribute         obj.setid(integer.valueof(reader.getattribute("id")));         reader.movedown(); //get date          date date = null;         try {             date = new simpledateformat("mm/dd/yyyy").parse(reader.getvalue());         } catch (parseexception e) {             e.printstacktrace();         }         obj.setdate(date);         reader.moveup();          reader.movedown(); //get impression          string impression = reader.getvalue();         numberformat format = numberformat.getinstance(locale.us);         number number = 0;         try {             number = format.parse(impression);         } catch (parseexception e) {             e.printstacktrace();         }         obj.setimpression(number.longvalue());          reader.moveup();          reader.movedown(); //get click         obj.setclicks(integer.valueof(reader.getvalue()));         reader.moveup();          reader.movedown(); //get earning         obj.setearning(new bigdecimal(reader.getvalue()));         reader.moveup();         return obj;     } } 

report.java

public class report implements serializable{     private static final long serialversionuid = 1l;      private int id;     private date date;     private long impression;     private int clicks;     private bigdecimal earning;      public int getid() {         return id;     }      public void setid(int id) {         this.id = id;     }      public date getdate() {         return date;     }      public void setdate(date date) {         this.date = date;     }      public long getimpression() {         return impression;     }      public void setimpression(long impression) {         this.impression = impression;     }      public int getclicks() {         return clicks;     }      public void setclicks(int clicks) {         this.clicks = clicks;     }      public bigdecimal getearning() {         return earning;     }      public void setearning(bigdecimal earning) {         this.earning = earning;     }      @override     public string tostring() {         simpledateformat dateformatter = new simpledateformat("m/d/yyyy");          return "report [id=" + id + ", date=" + dateformatter.format(date) + ", impression=" + impression + ", clicks=" + clicks + ", earning="                 + earning + "]";     } } 

app.java

public class app {     public static void main(string[] args) {          string[] springconfig  =              {   "spring/batch/config/database.xml",                  "spring/batch/config/context.xml",                 "spring/batch/jobs/job-report.xml"              };          applicationcontext context =                  new classpathxmlapplicationcontext(springconfig);          joblauncher joblauncher = (joblauncher) context.getbean("joblauncher");         job job = (job) context.getbean("reportjob");          try {             jobexecution execution = joblauncher.run(job, new jobparameters());             system.out.println("exit status : " + execution.getstatus());         } catch (exception e) {             e.printstacktrace();         }         system.out.println("done");     } } 

database.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:mongo="http://www.springframework.org/schema/data/mongo"      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/data/mongo         http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">      <!-- connect mongodb -->     <mongo:mongo host="127.0.0.1" port="27017" />     <mongo:db-factory dbname="yourdb" />      <bean id="mongotemplate" class="org.springframework.data.mongodb.core.mongotemplate">         <constructor-arg name="mongodbfactory" ref="mongodbfactory" />     </bean> </beans> 

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">      <!-- stored job-meta in memory -->      <bean id="jobrepository"         class="org.springframework.batch.core.repository.support.mapjobrepositoryfactorybean">         <property name="transactionmanager" ref="transactionmanager" />     </bean>      <bean id="transactionmanager"         class="org.springframework.batch.support.transaction.resourcelesstransactionmanager" />      <bean id="joblauncher"         class="org.springframework.batch.core.launch.support.simplejoblauncher">         <property name="jobrepository" ref="jobrepository" />     </bean> </beans> 

job-report.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"     xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://www.springframework.org/schema/batch         http://www.springframework.org/schema/batch/spring-batch-2.2.xsd         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/util          http://www.springframework.org/schema/util/spring-util-3.2.xsd">      <batch:job id="reportjob">         <batch:step id="step1">             <batch:tasklet>                 <batch:chunk reader="xmlitemreader" writer="mongodbitemwriter" commit-interval="1">                 </batch:chunk>             </batch:tasklet>         </batch:step>     </batch:job>      <bean id="mongodbitemwriter" class="org.springframework.batch.item.data.mongoitemwriter">         <property name="template" ref="mongotemplate" />         <property name="collection" value="report" />     </bean>      <bean id="xmlitemreader" class="org.springframework.batch.item.xml.staxeventitemreader">         <property name="fragmentrootelementname" value="record" />         <property name="resource" value="classpath:xml/report.xml" />         <property name="unmarshaller" ref="reportunmarshaller" />     </bean>      <bean id="reportunmarshaller" class="org.springframework.oxm.xstream.xstreammarshaller">          <property name="aliases">             <util:map id="aliases">                 <entry key="record" value="com.mkyong.model.report" />                 <!--                  <entry key="date" value="java.lang.string" />                 <entry key="impression" value="java.lang.long" />                 <entry key="clicks" value="java.lang.integer" />                 <entry key="earning" value="java.math.bigdecimal" />                  -->             </util:map>         </property>          <property name="converters">             <array>                 <ref bean="reportconverter" />             </array>         </property>     </bean>      <bean id="reportconverter" class="com.mkyong.converter.reportconverter" /> </beans> 

report.xml

<?xml version="1.0" encoding="utf-8" ?> <report>     <record id="1">         <date>6/1/2013</date>         <impression>139,237</impression>         <clicks>40</clicks>         <earning>220.90</earning>     </record>     <record id="2">         <date>6/2/2013</date>         <impression>339,100</impression>         <clicks>60</clicks>         <earning>320.88</earning>     </record>     <record id="3">         <date>6/3/2013</date>         <impression>431,436</impression>         <clicks>76</clicks>         <earning>270.80</earning>     </record>     <record id="4">         <date>3/12/2016</date>         <impression>534,987</impression>         <clicks>43</clicks>         <earning>454.80</earning>     </record> </report> 

mongodb.properties

mongo.host=127.0.0.1 mongo.port=27017 mongo.db=yourdb 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -