List of usage examples for com.mongodb.client MongoCursor close
@Override
void close();
From source file:DS.Model.java
public List<Restaurant> getRestaurants() { MongoCollection<Document> collection = db.getCollection("restaurants"); MongoCursor<Document> cursor = collection.find().iterator(); resList = new ArrayList<Restaurant>(); try {// w ww. j a v a2 s.c o m //Iterate through all the returned restaurants and store the information into the resList list while (cursor.hasNext()) { String json = cursor.next().toJson(); JsonParser parser = new JsonParser(); JsonElement jsonTree = parser.parse(json); if (jsonTree.isJsonObject()) { Restaurant r = new Restaurant(); JsonObject j = jsonTree.getAsJsonObject(); r.setName(j.get("name").getAsString()); r.setLocation(j.get("address").getAsString()); r.setRating(Double.parseDouble(j.get("rating").getAsString())); resList.add(r); } } } finally { cursor.close(); } return resList; }
From source file:DS.Model.java
public List<Request> getRequests() { MongoCollection<Document> collection = db.getCollection("requests"); MongoCursor<Document> cursor = collection.find().iterator(); reqList = new ArrayList<Request>(); try {//w w w.jav a2 s . c o m //Iterate through all the requests to retrieve the information and add to the reqList list while (cursor.hasNext()) { String json = cursor.next().toJson(); JsonParser parser = new JsonParser(); JsonElement jsonTree = parser.parse(json); if (jsonTree.isJsonObject()) { Request r = new Request(); JsonObject j = jsonTree.getAsJsonObject(); String term = j.get("term").getAsString(); String location = j.get("location").getAsString(); r.setLocation(location); r.setTerm(term); r.setTimestamp(j.get("time").getAsString()); r.setPhone(j.get("phone").getAsString()); r.setDelay(Double.parseDouble(j.get("delay").getAsString())); reqList.add(r); //Add the term to the termCount map, used to find which term is the most popular //Check if the term already exists. IF yes, increment by one, else add as new term if (termCount.containsKey(term)) { int count = termCount.get(term); termCount.put(term, count + 1); } else { termCount.put(term, 0); } //Add the location to the locCount map, used to find which term is the most popular //Check if the location already exists. IF yes, increment by one, else add as new loc if (locCount.containsKey(location)) { int count = locCount.get(location); locCount.put(location, count + 1); } else { locCount.put(location, 0); } } } } finally { cursor.close(); } return reqList; }
From source file:dto.Dto.java
public String listarTesis() { String cadena = ""; MongoCollection<Document> col = c.getConnection("universo_tesis"); MongoCursor<Document> cursor = col.find().sort(Sorts.orderBy(Sorts.descending("ao"))).iterator(); Document doc;/*from www .j av a 2 s. c o m*/ try { while (cursor.hasNext()) { doc = cursor.next(); cadena += "<tr>" + "<td width='20%'>" + doc.getString("titulo").toUpperCase().trim() + "</td>" + "<td width='20%'>" + doc.getString("ao") + "</td>" + "<td width='20%'>" + doc.getString("estado").toUpperCase().trim() + "</td>" + "</tr>"; } } catch (Exception e) { System.out.println("listarTesis universo: " + e); } finally { cursor.close(); } return cadena; }
From source file:dto.Dto.java
public String listarActas() { String cadena = ""; MongoCollection<Document> col = c.getConnection("actas"); MongoCursor<Document> cursor = col.find(eq("estado", "pendiente")).iterator(); Document doc;//from w w w . jav a 2 s. c om try { while (cursor.hasNext()) { doc = cursor.next(); cadena += "<tr>" + "<td width='20%'>" + doc.getString("tema").toUpperCase().trim() + "</td>" + "<td width='20%'>" + doc.getString("temas") + "</td>" + "<td width='20%'>" + doc.getString("reco").toUpperCase().trim() + "</td>" + "<td width='20%'>" + getNombreAlumno(doc.getString("idAlumno")) + "</td>"; if (doc.getString("estado").equalsIgnoreCase("pendiente")) { cadena += "<td width='20%'><button onclick='aceptarActa(" + doc.getString("_id") + ")'>OK</button>" + "<button onclick='rechazarActa(" + doc.getString("_id") + ")'>X</button></td><tr>"; } else { cadena += "<td width='20%'></td><tr>"; } } } catch (Exception e) { cadena = "No tiene actas pendientes"; System.out.println("listarActas: " + e); } finally { cursor.close(); } return cadena; }
From source file:dto.Dto.java
public String listarTesisAsesor(String id) { String cadena = ""; MongoCollection<Document> col = c.getConnection("tesis_alumno_asesor"); MongoCursor<Document> cursor = col.find(and(eq("estadoA", "pendiente"), eq("idAsesor", id))).iterator(); Document doc;/*from w ww . j a v a 2 s . co m*/ try { while (cursor.hasNext()) { System.out.println("while asessor"); doc = cursor.next(); cadena += "<tr>" + "<td width='20%'>" + doc.getString("titulo").toUpperCase().trim() + "</td>" + "<td width='20%'>" + getNombreAlumno(doc.getString("idAlumno")) + "</td>"; if (doc.getString("estadoA").equalsIgnoreCase("pendiente")) { cadena += "<td width='20%'><button onclick='aceptarSolicitud(" + doc.getString("_id") + ")'>OK</button>" + "<button onclick='rechazarSolicitud(" + doc.getString("_id") + ")'>X</button></td><tr>"; } else { cadena += "<td width='20%'></td><tr>"; } //System.out.println(doc); } //p = doc.getString("nombre"); } catch (NullPointerException e) { System.out.println("tesisAsesor: " + e); } finally { cursor.close(); } return cadena; }
From source file:dto.Dto.java
public String listarTesisProfesor(String seccion) { MongoCollection<Document> col = c.getConnection("tesis_alumno_asesor"); MongoCursor<Document> cursor = col.find(eq("seccion", seccion)).iterator(); Document doc;/* w w w .ja va 2 s. co m*/ String cadena = ""; try { while (cursor.hasNext()) { doc = cursor.next(); cadena += "<tr>" + "<td width='20%'>" + doc.getString("titulo").toUpperCase().trim() + "</td>" + "<td width='20%'>" + getNombreAlumno(doc.getString("idAlumno")) + "</td>"; if (doc.getString("estadoP").equalsIgnoreCase("pendiente")) { cadena += "<td width='20%'><button onclick='aceptarTesis(" + doc.getString("_id") + ")'>OK</button>" + "<button onclick='rechazarTesis(" + doc.getString("_id") + ")'>X</button></td><tr>"; } else { cadena += "<td width='20%'></td><tr>"; } //System.out.println(doc); } //p = doc.getString("nombre"); } catch (NullPointerException e) { System.out.println("listar tesis: " + e); } finally { cursor.close(); } return cadena; }
From source file:dto.Dto.java
public String listarTesisSinAsesor(String seccion) { MongoCollection<Document> col = c.getConnection("tesis_alumno_asesor"); MongoCursor<Document> cursor = col.find(and(eq("seccion", (seccion)), eq("idAsesor", "0"))).iterator(); Document doc;/*from w w w .j ava 2 s .c o m*/ String cadena = ""; try { while (cursor.hasNext()) { doc = cursor.next(); cadena += "<tr>" + "<td width='20%'>" + doc.getString("titulo").toUpperCase().trim() + "</td>" + "<td width='20%'><select id='sel' style='display:inline'><option>*** Seleccione asesor ***</option></select></td>" + "<td><button onclick='enviarSolicitud(" + doc.getString("_id") + ")'>Enviar</button></td></tr>"; //System.out.println(doc); } //p = doc.getString("nombre"); } catch (NullPointerException e) { System.out.println("listar sin asesor: " + e); } finally { cursor.close(); } return cadena; }
From source file:dto.Dto.java
public String listarAsesores() { String cadena = ""; MongoCollection<Document> col = c.getConnection("profesores"); MongoCursor<Document> cursor = col.find().iterator(); Document doc;//from w ww. j a v a 2 s .co m try { while (cursor.hasNext()) { doc = cursor.next(); cadena += "<option value='" + doc.getString("_id") + "'>" + doc.getString("nombre") + "</option>"; } } catch (Exception e) { System.out.println("listarTesisAsesores: " + e); } finally { cursor.close(); } return cadena; }
From source file:es.omarall.mtc.TailingTask.java
License:Apache License
/** * TAILING TASK://from w ww .j a va 2 s . com * * 1. Builds a cursor 2. Fetch documents from that cursor till cursor closed * o documentHandler changes its state * * */ @Override public void run() { try { // Check start was called if (!getStatus().equals(ServiceStatus.STARTED)) throw new MTCExecutionException( "Trying to RUN a non started task. Please call start() method before running the tailing task. "); while (true) { // Work with cursor until lost or // documentHandler changes its state to not started. // hasNext throws IllegalStateException when cursor is closed // (not by documentHandler) MongoCursor<Document> cursor = buildCursor(); // "Await" for data if (cursor != null) { if (cursor.hasNext()) { // throws ChangedStateToNotStarted iterateCursor(cursor); // Cursor was LOST // wait to regenerate another cursor if configured so applyDelayToGenerateCursor(); } else { // hasNext returned with no data LOG.debug("Cursor returned no data"); cursor.close(); } } } // while(keepRunning) block } catch (IllegalStateException e) { // hasNext() throws IllegalStateException when cursor is close by // other thread. LOG.info("+ MONGOESB: Cursor was closed"); // STOP or Suspend } catch (NotStartedException e) { // Consumer changed its state LOG.info("+ MONGOESB: Consumer changed its state"); } finally { LOG.info("+ MONGOESB - STOP TAILING TASK"); } }
From source file:es.omarall.mtc.TailingTask.java
License:Apache License
/** * Cursor LOGIC. A built cursor can be iterated until lost or until the * state is changed to a no started state. * /*from ww w . jav a 2s. c o m*/ * @throws NotStartedException * to signal state changed to a non started state */ private void iterateCursor(final MongoCursor<Document> cursor) { if (cursor == null) return; // stores the id of the last document fetched by THIS cursor... ObjectId lastProcessedId = null; try { while (true) { // Is there a new document to be processed? Document next = cursor.tryNext(); if (next == null) { // No doc to be processed ... // It is likely we come from a burst of processing ... // This is a chance to persist last processed // id...go for it if (tracker != null && lastProcessedId != null) { tracker.persistLastTrackedEventId(lastProcessedId); lastTrackedId = lastProcessedId; } // Wait for a new document to be processed if (!cursor.hasNext()) { LOG.debug("INNER has NEXT returned no data"); if (cursor != null) cursor.close(); } } else { // There is a document to be processed try { documentHandler.handleDocument(next); lastProcessedId = next.getObjectId("_id"); } catch (Exception e) { LOG.error("DocumentHandler raised an exception", e); // Notifiy but keep going } } // Check whether to keep execution if (getStatus().equals(ServiceStatus.STOPPED)) throw new NotStartedException("Cursor Changed its state to not started"); } // while } catch (MongoSocketException e) { // The cursor was closed LOG.error("\n\nMONGOESB - NETWORK problems: Server Address: {}", e.getServerAddress().toString(), e); // Not recoverable. Do not regenerate the cursor throw new MTCException(String.format("Network Problemns detected. Server address: %s", e.getServerAddress().toString())); } catch (MongoQueryException e) { // MongoCursorNotFoundException // The cursor was closed // Recoverable: Do regenerate the cursor LOG.info("Cursor {} has been closed.", e); } catch (IllegalStateException e) { // .hasNext(): Cursor was closed by other THREAD (documentHandler // cleaningup)?) // Recoverable. Do regenerate the cursor. LOG.info("Cursor being iterated was closed", e); } catch (NotStartedException e) { // Not recoverable: Do not regenerate the cursor. throw e; } finally { // persist tracking state if (tracker != null && lastProcessedId != null) { tracker.persistLastTrackedEventId(lastProcessedId); lastTrackedId = lastProcessedId; } // Cleanup resources. if (cursor != null) cursor.close(); } }