Example usage for com.mongodb MongoClient close

List of usage examples for com.mongodb MongoClient close

Introduction

In this page you can find the example usage for com.mongodb MongoClient close.

Prototype

public void close() 

Source Link

Document

Closes all resources associated with this instance, in particular any open network connections.

Usage

From source file:com.kurniakue.data.KurniaKueDb.java

public static void stopAll() {
    for (MongoClient client : allClients) {
        client.close();
    }
}

From source file:com.maoyan.pf.webcollector.spider.AttendrateCrawler.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override//from  w w w .j  av  a 2  s  .com
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            // ?
            // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); 
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            Set<String> colls = db.getCollectionNames();
            for (String s : colls) {
                // Collection(?"")
                if (s.equals("attend_rate")) {
                    db.getCollection(s).drop();
                }
            }
            DBCollection dbCollection = db.getCollection("attend_rate");
            HtmlUnitDriver driver = new HtmlUnitDriver();
            driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            //                System.out.println(driver.getPageSource());
            WebElement element = driver.findElementByCssSelector("div#seat_table");
            List<WebElement> movie_name = element.findElements(By.className("c1 lineDot"));
            List<WebElement> boxoffice_rate = element.findElements(By.className("c2 red"));
            List<WebElement> visit_pershow = element.findElements(By.className("c3 gray"));
            WebElement cityarea = driver.findElementByCssSelector("span[class='today']");
            System.out.println(cityarea.getText());
            for (int i = 0; i < movie_name.size(); i++) {
                System.out.println(movie_name.get(i).getText());
                System.out.println(boxoffice_rate.get(i).getText());
                System.out.println(visit_pershow.get(i).getText());
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", cityarea.getText()).append("movie_name", movie_name.get(i).getText())
                        .append("boxoffice_rate", boxoffice_rate.get(i).getText())
                        .append("visit_pershow", visit_pershow.get(i).getText());
                dbCollection.insert(dbObject);
            }
            mongoClient.close();
        }
    };

    //DBDBManager
    DBManager manager = new BerkeleyDBManager("maoyan");
    //Crawler?DBManagerExecutor
    Crawler crawler = new Crawler(manager, executor);
    crawler.addSeed("http://pf.maoyan.com/attend/rate");
    crawler.start(1);
}

From source file:com.maoyan.pf.webcollector.spider.MoviestoreCrawler.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/*from   w w  w  .  ja v a  2s.co  m*/
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
            Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
            mongoLogger.setLevel(Level.ERROR);
            // ?
            // DBCollection dbCollection =
            // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am");
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            DBCollection dbCollection = db.getCollection("movie_store");
            HtmlUnitDriver driver = new HtmlUnitDriver();
            driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            // System.out.println(driver.getPageSource());
            System.out.println(driver.findElement(By.xpath("//html/body/header/h1")).getText());
            // ProfilesIni pi = new ProfilesIni();
            // FirefoxProfile profile = pi.getProfile("default");
            // WebClient webClient = new
            // WebClient(BrowserVersion.FIREFOX_38);
            // driver.setJavascriptEnabled(false);
            // webClient.getOptions().setCssEnabled(true);
            // HtmlPage page = webClient.getPage(datum.getUrl());
            // System.out.println(page.asXml());
            // List<?> title =
            // page.getByXPath("//html/body/header/h1/text()");
            // List<?> score =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[1]/i/text()");
            // List<?> score_num =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[1]/span/text()");
            // List<?> want_num =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[2]/i/text()");
            // List<?> type =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[1]/text()");
            // List<?> timing =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[2]/text()");
            // List<?> system =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[3]/text()");
            // List<?> onscreendate =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[4]/text()");
            // List<?> total_rev =
            // page.getByXPath("//html/body/section[1]/article[2]/span[1]/text()");
            // List<?> firstweek_rev =
            // page.getByXPath("//html/body/section[1]/article[2]/span[2]/text()");
            // List<?> ahead_rev =
            // page.getByXPath("//html/body/section[1]/article[2]/span[3]/text()");
            // List<?> director =
            // page.getByXPath("//*[@id='infoContent']/article[1]/div/div[1]/div/div[2]/div[1]/div[2]/text()");
            // List<?> actor =
            // page.getByXPath("//*[@id='infoContent']/article[1]/div/div[1]/div/div[2]/div[2]/div[2]/text()");
            // List<?> production =
            // page.getByXPath("//*[@id='infoContent']/article[2]/div/div[1]/div/div[2]/text()");
            // List<?> distribution =
            // page.getByXPath("//*[@id='infoContent']/article[4]/div/div[1]/div/div[2]/text()");
            // List<?> technique =
            // page.getByXPath("//*[@id='infoContent']/article[5]/div/div[1]/div/div[2]/text()");
            // List<?> desc =
            // page.getByXPath("//*[@id='infoContent']/article[6]/div/div[2]/text()");
            // List<?> want_by_city =
            // page.getByXPath("//*[@id='wantCity']/ul/li[2]/b/text()");
            // List<?> want_val=
            // page.getByXPath("//*[@id='wantCity']/ul/li[3]/div/@style");
            // System.out.println(title);
            //// System.out.println(page.getByXPath("//span[@class='today']/em/text()"));
            //// System.out.println(page.getByXPath("//span[@class='today']/text()"));
            //// List<?> movie_name =
            // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c1
            // lineDot']/text()");
            //// List<?> boxoffice_rate =
            // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c2
            // red']/text()");
            //// List<?> visit_pershow =
            // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c3
            // gray']/text()");
            //// for(int i = 0;i<movie_name.size();i++){
            //// System.out.println(movie_name.get(i));
            //// System.out.println(boxoffice_rate.get(i));
            //// System.out.println(visit_pershow.get(i));
            //// }
            // List<?> hbList = page.getByXPath("//div");
            // HtmlDivision hb = (HtmlDivision)hbList.get(0);
            // System.out.println(hb.toString());
            // System.out.println(ReturnString(driver,By.xpath("//*[@id='infoContent']/article[4]/div/div[1]/div/div[2]")));
            if (ReturnString(driver, (By.xpath("//html/body/header/h1"))).length() != 0) {
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", ReturnString(driver, (By.xpath("//html/body/header/h1"))))
                        .append("url", driver.getCurrentUrl())
                        .append("score",
                                ReturnString(driver,
                                        By.xpath(
                                                "//html/body/section[1]/article[1]/aside/hgroup/article[1]/i")))
                        .append("score_num",
                                ReturnString(driver, By.xpath(
                                        "//html/body/section[1]/article[1]/aside/hgroup/article[1]/span")))
                        .append("want_num",
                                ReturnString(driver,
                                        By.xpath(
                                                "//html/body/section[1]/article[1]/aside/hgroup/article[2]/i")))
                        .append("type",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[1]")))
                        .append("timing",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[2]")))
                        .append("system",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[3]")))
                        .append("onscreendate",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[4]")))
                        .append("total_rev",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[1]")))
                        .append("firstweek_rev",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[2]")))
                        .append("ahead_rev",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[3]")))
                        .append("director", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='m-info-crews m-info-section']/div/div[1]/div/div[2]/div[1]/div[2]")))
                        .append("actor", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='m-info-crews m-info-section']/div/div[1]/div/div[2]/div[2]/div[2]")))
                        .append("production", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='production-companies m-info-section']/div/div[1]/div/div[2]")))
                        .append("jointproduction", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='joint-production-companies m-info-section']/div/div[1]/div/div[2]")))
                        .append("distribution", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='distribution-firm m-info-section']/div/div[1]/div/div[2]")))
                        .append("techpara", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[tech-params m-info-section]/div/div[1]/div/div[2]")))
                        .append("infodrama", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[m-info-drama m-info-section]/div/div[2]")))
                        .append("want_by_city", ReturnString(driver, By.xpath("//*[@id='wantCity']")));
                dbCollection.insert(dbObject);
                driver.close();
                driver.quit();
                mongoClient.close();
            }
        }
    };
    MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
    // ?
    // DBCollection dbCollection =
    // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am");
    DB db = mongoClient.getDB("maoyan_crawler");
    // ?????
    Set<String> colls = db.getCollectionNames();
    for (String s : colls) {
        // Collection(?"")
        if (s.equals("movie_store")) {
            db.getCollection(s).drop();
        }
    }

    // DBDBManager
    DBManager manager = new BerkeleyDBManager("maoyan");
    // Crawler?DBManagerExecutor
    for (int round = 200; round < 300; round++) {
        System.out.println("Round " + round + " crawling.../n");
        Crawler crawler = new Crawler(manager, executor);
        for (int i = 1 + 100 * round; i <= 100 + 100 * round; i++) {
            crawler.addSeed("http://pf.maoyan.com/movie/" + i + "?_v_=yes");
        }
        /* ??visit */
        crawler.setVisitInterval(50);
        /* ?http? */
        crawler.setRetryInterval(100);
        crawler.setThreads(10);
        crawler.start(1);
    }
}

From source file:com.maoyan.pf.webcollector.spider.ShowrateCrawler.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/*w w w.j av a  2s . co  m*/
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            // ?
            // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); 
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            Set<String> colls = db.getCollectionNames();
            for (String s : colls) {
                // Collection(?"")
                if (s.equals("show_rate")) {
                    db.getCollection(s).drop();
                }
            }
            DBCollection dbCollection = db.getCollection("show_rate");
            //                ProfilesIni pi = new ProfilesIni();
            //                FirefoxProfile profile = pi.getProfile("default");
            WebDriver driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
            //                driver.setJavascriptEnabled(false);
            System.out.println("??\n");
            driver.get(datum.getUrl());
            //                System.out.println(driver.getPageSource());
            List<WebElement> movie_name = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c1 lineDot']"));
            List<WebElement> boxoffice_rate = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c2 red']"));
            List<WebElement> visit_pershow = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c3 gray']"));
            WebElement title = driver.findElement(By.xpath("//p[@id='pieTip']"));
            for (int i = 0; i < movie_name.size(); i++) {
                String movie_name_val = movie_name.get(i).getText();
                String boxofficerate_val = boxoffice_rate.get(i).getText();
                String visit_pershow_val = visit_pershow.get(i).getText();
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title.getText()).append("is_gold", "?")
                        .append("show_type", "?").append("movie_name", movie_name_val)
                        .append("boxoffice_rate", boxofficerate_val).append("visit_pershow", visit_pershow_val);
                dbCollection.insert(dbObject);
            }
            System.out.println("?\n");
            WebElement click_gold = driver.findElement(By.id("playPlan_time"));
            click_gold.click();
            String gold_seat = driver.getWindowHandle();
            driver.switchTo().window(gold_seat);
            List<WebElement> movie_name_gold = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c1 lineDot']"));
            List<WebElement> boxoffice_rate_gold = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c2 red']"));
            List<WebElement> visit_pershow_gold = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c3 gray']"));
            WebElement title_gold = driver.findElement(By.xpath("//p[@id='pieTip']"));
            for (int i = 0; i < movie_name_gold.size(); i++) {
                String movie_name_val = movie_name_gold.get(i).getText();
                String boxofficerate_val = boxoffice_rate_gold.get(i).getText();
                String visit_pershow_val = visit_pershow_gold.get(i).getText();
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title_gold.getText()).append("is_gold", "")
                        .append("show_type", "?").append("movie_name", movie_name_val)
                        .append("boxoffice_rate", boxofficerate_val).append("visit_pershow", visit_pershow_val);
                dbCollection.insert(dbObject);
            }
            System.out.println("?\n");
            WebElement click_vist = driver.findElement(By.xpath("//*[@id='show--type']"));
            click_vist.click();
            String gold_vist = driver.getWindowHandle();
            driver.switchTo().window(gold_vist);
            List<WebElement> movie_name_gold_visit = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c1 lineDot']"));
            List<WebElement> boxoffice_rate_gold_visit = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c2 red']"));
            List<WebElement> visit_pershow_gold_visit = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c3 gray']"));
            WebElement title_gold_visit = driver.findElement(By.xpath("//p[@id='pieTip']"));
            for (int i = 0; i < movie_name_gold_visit.size(); i++) {
                String movie_name_val = movie_name_gold_visit.get(i).getText();
                String boxofficerate_val = boxoffice_rate_gold_visit.get(i).getText();
                String visit_pershow_val = visit_pershow_gold_visit.get(i).getText();
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title_gold_visit.getText()).append("is_gold", "")
                        .append("show_type", "").append("movie_name", movie_name_val)
                        .append("boxoffice_rate", boxofficerate_val).append("visit_pershow", visit_pershow_val);
                dbCollection.insert(dbObject);
            }
            System.out.println("?\n");
            click_gold.click();
            String normal_seat = driver.getWindowHandle();
            driver.switchTo().window(normal_seat);
            List<WebElement> movie_name_normal_seat = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c1 lineDot']"));
            List<WebElement> boxoffice_rate_normal_seat = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c2 red']"));
            List<WebElement> visit_pershow_normal_seat = driver
                    .findElements(By.xpath("//div[@id='playPlan_table']/ul/li[@class='c3 gray']"));
            WebElement title_normal_seat = driver.findElement(By.xpath("//p[@id='pieTip']"));
            for (int i = 0; i < movie_name_normal_seat.size(); i++) {
                String movie_name_val = movie_name_normal_seat.get(i).getText();
                String boxofficerate_val = boxoffice_rate_normal_seat.get(i).getText();
                String visit_pershow_val = visit_pershow_normal_seat.get(i).getText();
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title_normal_seat.getText()).append("is_gold", "?")
                        .append("show_type", "").append("movie_name", movie_name_val)
                        .append("boxoffice_rate", boxofficerate_val).append("visit_pershow", visit_pershow_val);
                dbCollection.insert(dbObject);
            }

            driver.close();
            driver.quit();
            mongoClient.close();
        }
    };

    //DBDBManager
    DBManager manager = new BerkeleyDBManager("maoyan");
    //Crawler?DBManagerExecutor
    Crawler crawler = new Crawler(manager, executor);
    crawler.addSeed("http://pf.maoyan.com/show/rate");
    crawler.start(1);
}

From source file:com.mycompany.citysearchnosql.Executioner.java

public static void main(final String[] args) {

    // 1. Connect to MongoDB instance running on localhost
    MongoClient mongoClient = new MongoClient();

    // Access database named 'test'
    MongoDatabase database = mongoClient.getDatabase("test");

    // Access collection named 'restaurants'
    MongoCollection<Document> collection = database.getCollection("restaurants");

    // 2. Insert 
    List<Document> documents = asList(
            new Document("name", "Sun Bakery Trattoria").append("stars", 4).append("categories",
                    asList("Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")),
            new Document("name", "Blue Bagels Grill").append("stars", 3).append("categories",
                    asList("Bagels", "Cookies", "Sandwiches")),
            new Document("name", "Hot Bakery Cafe").append("stars", 4).append("categories",
                    asList("Bakery", "Cafe", "Coffee", "Dessert")),
            new Document("name", "XYZ Coffee Bar").append("stars", 5).append("categories",
                    asList("Coffee", "Cafe", "Bakery", "Chocolates")),
            new Document("name", "456 Cookies Shop").append("stars", 4).append("categories",
                    asList("Bakery", "Cookies", "Cake", "Coffee")));

    collection.insertMany(documents);/*from ww  w .ja v  a 2  s .  com*/

    // 3. Query 
    List<Document> results = collection.find().into(new ArrayList<>());

    // 4. Create Index 
    collection.createIndex(Indexes.ascending("name"));
    // 5. Perform Aggregation
    collection.aggregate(asList(match(eq("categories", "Bakery")), group("$stars", sum("count", 1))));

    mongoClient.close();
}

From source file:com.mycompany.database.MongodbDataBaseClass.java

public void dududu() {
    System.out.println("not error");
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("testdb");
    System.out.println("not error");
    MongoCollection<Document> col = database.getCollection("cars");

    try (MongoCursor<Document> cur = col.find().iterator()) {
        while (cur.hasNext()) {

            Document doc = cur.next();

            List list = new ArrayList(doc.values());
            System.out.print(list.get(1));
            System.out.print(": ");
            System.out.println(list.get(2));
        }// www.  ja va 2  s . c o m
    }

    mongoClient.close();
}

From source file:com.mysweethome.TemperaturePersisterTimerTask.java

public synchronized void persistDataOnMongolab() {
    //disable console logging
    //Logger mongoLogger = Logger.getLogger("org.mongodb.driver"); 
    //mongoLogger.setLevel(Level.SEVERE);

    iStoredTemperatures = iTemperatureStore.getTemperatures();
    if (iStoredTemperatures.isEmpty()) {
        logger.info("Nothing to persist. Exiting");
        return;/*w ww  . ja v  a 2 s. com*/
    }
    logger.info("Prepairing to persist [{}] Temps in the cloud", iStoredTemperatures.size());
    MongoCollection<Document> mongoCollection = null;
    MongoClient client = null;
    List<Document> documents = new ArrayList<Document>();

    for (TemperatureMeasure tTemp : iStoredTemperatures) { //Exception in thread "Timer-2" java.util.ConcurrentModificationException
        Document doc = new Document();
        doc.put("Location", tTemp.getLocation()); //Location
        doc.put("Group", tTemp.getGroup()); //Group
        doc.put("Date", Helper.getDateAsString(tTemp.getDate())); //Date
        doc.put("Day", Helper.getDayAsString(tTemp.getDate()));
        doc.put("Time", Helper.getTimeAsString(tTemp.getDate()));
        doc.put("Temp", Helper.getTempAsString(tTemp.getTemp())); //Temp
        documents.add(doc);
        iPersistedTemperatures.add(tTemp);
    }

    try {
        MongoClientURI uri = new MongoClientURI(MySweetHomeProperties.ML_URL);
        client = new MongoClient(uri);
        MongoDatabase database = (MongoDatabase) client.getDatabase(uri.getDatabase());
        mongoCollection = database.getCollection("dailytemps");
        mongoCollection.insertMany(documents);
        //eliminate stored Temps from the collection
        iTemperatureStore.removeAll(iPersistedTemperatures);
        client.close();
        logger.info("Temperatures persisted on mongolab: [{}]. Exiting.", iPersistedTemperatures.size());
        iPersistedTemperatures.clear();
    } catch (Throwable e) {
        logger.error("Failed to store Temps in the cloud. Stacktrace: [{}]. Exiting.", e);
        iPersistedTemperatures.clear();
        e.printStackTrace();
    } finally {
        if (client != null) {
            client.close();
        }
        iPersistedTemperatures.clear();
    }
}

From source file:com.nec.strudel.tkvs.store.tokumx.TokumxLifecycle.java

License:Apache License

@Override
public void initialize() {
    for (int i = 0; i < mongoDs.length; i++) {
        try {//from w  ww  .java 2  s. c  o  m
            MongoClient client = new MongoClient(mongoDs[i]);
            client.getDB(dbName).getCollection(dbName).createIndex(new BasicDBObject(DOCNAME, 1),
                    new BasicDBObject("unique", true));
            client.close();
        } catch (UnknownHostException e) {
            throw new TkvStoreException("unknown host:" + mongoDs[i]);
        }
    }
}

From source file:com.owly.srv.config.ApplicationListener.java

License:Apache License

/**
 * This methond checks status of the stadistics server database
 * //from w  w w . j  a  va 2  s .c  om
 * @param StadisticsServer
 * @param listInitRemoteServer
 * @return a bolean true in case parameters of database are OK, and database
 *         is up.
 */
public boolean checkDatabaseStatus(StatsServer StadisticsServer, ArrayList<RemoteServer> listInitRemoteServer) {

    final String CollectionDB = "StatsSrvCfg";

    // logger.debug("Configuration of the Stats Server : " +
    // StadisticsServer.toString());

    if (!StadisticsServer.isEnabled()) {
        logger.error("Statistics server is disabled, check configuration file");
        RuntimeException ex = new RuntimeException("Error starting the Application ");
        throw ex;

    } else {
        if (!StadisticsServer.getStatsDatabaseType().equals("MONGODB")) {
            logger.error(
                    "Statistics server database should be MongoDB (others not implemented), check configuration file");
            RuntimeException ex = new RuntimeException("Error starting the Application ");
            throw ex;
        } else {
            if (!StadisticsServer.getStatsIpAddress().equals("127.0.0.1")) {
                logger.info("Statistics server IP is not 127.0.0.1 --> Database is a remote server");
            } else {
                logger.info("Statistics server IP is  127.0.0.1 --> Database is local server");
            }

            logger.debug("Checking if MongoDB is up and running for IP : "
                    + StadisticsServer.getStatsIpAddress() + " and port : " + StadisticsServer.getStatsPort());
            try {

                /**** Connect to MongoDB ****/
                // Since 2.10.0, uses MongoClient
                MongoClient mongoClient = new MongoClient(StadisticsServer.getStatsIpAddress(),
                        StadisticsServer.getStatsPort());

                try {
                    Socket socket = mongoClient.getMongoOptions().socketFactory.createSocket();
                    socket.connect(mongoClient.getAddress().getSocketAddress());
                    socket.close();
                } catch (IOException e) {
                    logger.error("mongoDB Error : " + e.getMessage());
                    logger.error("Exception ::", e);
                    RuntimeException ex = new RuntimeException("Error starting the Application ");
                    throw ex;

                }

                /**** Get database ****/
                // if database doesn't exists, MongoDB will create it
                // for you
                DB statsDB = mongoClient.getDB(StadisticsServer.getStatsDatabase());

                StatsServerMongoDAOImpl statServerCfg = new StatsServerMongoDAOImpl(statsDB);

                RemoteServerMongoDAOImpl remoteServerCfg = new RemoteServerMongoDAOImpl(statsDB);

                // Drop all previous configuration in the database

                statServerCfg.deleteAllStatsServer();
                //remoteServerCfg.deleteAllRemoteServer();

                // Create new configuration in database
                int numberStats = statServerCfg.numberStatsServer();

                if (numberStats == 0) {
                    logger.debug("Actual Server Stats configuration NOT in database");
                    logger.debug("Insert configuration in Database");

                    // Insert Stats Server in Database
                    statServerCfg.insertStatsServer(StadisticsServer);

                    // Insert Remote Servers in the Database
                    for (int srv = 0; srv < listInitRemoteServer.size(); srv++) {
                        logger.debug("Remote Server to Insert in database (" + srv + ") : "
                                + listInitRemoteServer.get(srv).toString());
                        RemoteServer remoteServer = listInitRemoteServer.get(srv);
                        String nameSrv = remoteServer.getName();
                        String ipSrv = remoteServer.getNodeIPAddress();

                        // Drop the server from database is already exists
                        remoteServerCfg.deleteRemoteServer(nameSrv, ipSrv);

                        // Insert the remote server. 
                        remoteServerCfg.insertRemoteServer(listInitRemoteServer.get(srv));
                        logger.debug("Remote Serverinserted");

                    }

                    // Add a unique index based on IP and name
                    remoteServerCfg.addUniqueIndex("NodeIPAddress", "Name");

                } else {
                    logger.error("Configuration in database " + CollectionDB
                            + " is not OK, manual action is required");
                    // Close the conection to database
                    logger.info("Closing MongoClient");
                    mongoClient.close();
                    RuntimeException ex = new RuntimeException("Error starting the Application ");
                    throw ex;
                }

                // Close the conection to database
                logger.info("Closing MongoClient");
                mongoClient.close();

            } catch (UnknownHostException e) {
                logger.error("mongoDB Error : " + e.getMessage());
                logger.error("Exception ::", e);
                RuntimeException ex = new RuntimeException("Error starting the Application ");
                throw ex;
            }

        }
    }

    return true;

}

From source file:com.owly.srv.Job.OwlySrvCleanJob.java

License:Apache License

void CleanStats(String dbName, String ipServer, int portServer, String typeDatabase) {

    //Get date for this executin
    GregorianCalendar cal = new GregorianCalendar();
    Date nowDate = cal.getTime();

    SimpleDateFormat actualDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    logger.debug("Actual date is " + actualDateFormat.format(nowDate));

    TimeZone tz = TimeZone.getDefault();
    int offset = tz.getOffset(new Date().getTime());

    logger.debug("This is my offset " + offset);

    /**** Connect to MongoDB ****/
    // Since 2.10.0, uses MongoClient
    try {/* w  w  w  .jav  a  2  s  .c o  m*/
        MongoClient mongoClient = new MongoClient(ipServer, portServer);
        /**** Get database ****/
        logger.info("MONGODB : Start MongoClient");
        DB statsDB = mongoClient.getDB(dbName);

        // Object to get the stats server in database
        StatsServerMongoDAOImpl statsServerDAO = new StatsServerMongoDAOImpl(statsDB);
        StatsServer srv = new StatsServer();
        srv = statsServerDAO.getStatsServer(dbName, ipServer, portServer, typeDatabase);

        //Number of days to keep in database 
        int number_days_to_save = srv.getSavedDays();
        logger.debug("Days to save in database = " + number_days_to_save);

        //GEt the past date
        cal.add(Calendar.DAY_OF_MONTH, -number_days_to_save);
        cal.add(Calendar.MILLISECOND, +offset);
        Date pastDate = cal.getTime();

        logger.debug("Date to drop before = " + pastDate);

        String StatsCollection = srv.getStatsCollect();
        logger.debug("Collection is  = " + StatsCollection);

        //Get an instance of the stadistics dao 
        RemoteBasicStatMongoDAOImpl basicStatDAO = new RemoteBasicStatMongoDAOImpl(statsDB, StatsCollection);

        //Get number of stats in databe for this condition
        long nb_stats = basicStatDAO.getNumberStatsBefore(pastDate);

        logger.info("Number of stats to drop = " + nb_stats);

        if (nb_stats > 0) {
            basicStatDAO.cleanStatsbefore(pastDate);
        }

        // Close the connection to database
        logger.info("MONGODB : Closing MongoClient");
        mongoClient.close();

    } catch (UnknownHostException e) {
        logger.error("mongoDB Error : " + e.getMessage());
        logger.error("Exception ::", e);

    }

}