Skip Navigation Links



Mongodb Paging Item Reader

Author : Nitin Tripathi      Blog :ZERO      Date: 6/7/2012 3:56:00 PM



Here, is a sample..

package org.zero.index.batch.read;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.database.AbstractPagingItemReader;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.util.Assert;

public class MongodbPagingItemReader<T> extends AbstractPagingItemReader<T> {
  private MongoOperations mongoOperations;
  private QueryContext<T> queryContext;
  private boolean strict;

  private static final Logger LOG = LoggerFactory
      .getLogger(MongodbPagingItemReader.class);

  @Override
  protected void doReadPage() {
    if (results == null) {
      results = new CopyOnWriteArrayList<T>();
    } else {
      results.clear();
    }
    final List<T> result = mongoOperations.find(
        queryContext.getQuery().limit(getPageSize()).skip(getPage()),
        queryContext.getEntityClassName(), queryContext.getCollectionName());
    if ((getPage() == 0) && result.isEmpty()) {
      if (strict) {
        throw new IllegalStateException(
            "No matching documents found (reader is in 'strict' mode)");
      }
      LOG.warn("No matching documents found");
      return;
    }
    results.addAll(result);
  }

  @Override
  protected void doJumpToPage(final int itemIndex) {
    // do nothing
  }

  /**
   * @param mongoOperations
   *          the mongoOperations to set
   */
  public void setMongoOperations(final MongoOperations mongoOperations) {
    this.mongoOperations = mongoOperations;
  }

  /**
   * @param queryContext
   *          the queryContext to set
   */
  public void setQueryContext(final QueryContext<T> queryContext) {
    this.queryContext = queryContext;
  }

  /**
   * @param strict
   */
  public void setStrict(final boolean strict) {
    this.strict = strict;
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();
    Assert.notNull(mongoOperations, "mongoOperations is null");
    Assert.notNull(queryContext, "queryContext is null");
  }
}

Bloggers

active bloggers in the last 24 hrs. Number shown in the bracket represents number of posts published in past 24 hrs,


other authors(91)

Firoze Shakir Photographerno1(6)

3.14-eater(3)

Abhay Naxalrevolution(3)

Vashi Chandiramani(3)

abrachan(2)

Artist ANIKARTICK(2)

KUNJUBI VARGHESE(2)

mad madrasi(2)

pratish(2)

RWABhagidari.blogspot.com(2)

Sakshi Garg(2)

Sam Hindus Blog.(2)

Shruthi(2)

Vikram Karve(2)

aanondo(1)

Aarthi(1)

AbhiLaSH RuHeLa(1)

Ajithkumar Krishnakumar(1)

AK(1)

Alice(1)

Amrutha Technologies(1)

Anil(1)

Anshu Bhatnagar(1)

Anuj Srivastava(1)

Anuradha Shankar(1)

Aslam B(1)

Bhagyashree(1)

blixm(1)

Champa(1)

churumuri(1)

Cloud Nine(1)

Cynical(1)

Devi(1)

Dew(1)

DIGITALASIAN(1)

Divenita(1)

divorceddoodling(1)

Divya Kudua(1)

DMenon(1)

Eenzy Beenzy(1)

Fire Crystals(1)

Gayatri(1)

Govind Kumar(1)

Hari Chandana P(1)

Harini Padmanabhan(1)

Hawk Eye(1)

Itchingtowrite(1)

Iti(1)

Jabberwock(1)

Javin Paul(1)

John Mathew(1)

Kalyani(1)

Kannada Cuisine(1)

Karvy Private Wealth(1)

Kollemcodeonline(1)

Krithi Karthi(1)

krzna(1)

Lakshmi Andra(1)

Lucius Lobo(1)

Madhavi Madhurakavi(1)

Mahesh Kalaal(1)

Mandira(1)

Meera Sundararajan(1)

Mohammad Khalid Siddiqui(1)

Nadhiya Mali(1)

Neha Kumari(1)

Nisa Homey(1)

P.(1)

parashar pandya(1)

parth joshi(1)

parvathy gopalakrishnan(1)

Pooja Lokhande(1)

Pooja Naresh(1)

prakashf(1)

Prashanth(1)

Preety(1)

Priya Srinivasan(1)

Reema(1)

RNA Corp(1)

Roli Bhushan-Malhotra(1)

Rup(1)

sangeeta(1)

Sangeetha M(1)

Sangitha(1)

Saravanan Sivaraman(1)

Saurabh Moondhra(1)

Saurabh Moondhra(1)

share consultants(1)

sharmila k(1)

Shobha(1)

Shyam Bhatawdekar(1)

sk(1)

sp.ajay(1)

Srivalli(1)

Suchithra(1)

Sugar and Spice(1)

Sujoy Das(1)

sumana Mukhopadhyay(1)

Sunthosh Kumar(1)

Swaram(1)

thadisan(1)

Thendral Manoharan(1)

Vignesh Natarajan(1)

Vinod Singh(1)

Vishal Velekar(1)

vishesh unni raghunathan(1)

Vivek Anandha(1)

VJ Sharma(1)

Zubair Abdul Jabbar(1)

अफ़लातून अफ़लू(1)


garland of Languages of India
an amalgamation of the diversified traditions
gracefully presented with novelty
http://www.haaram.com