MongoDBDao 工具類

來源:互聯網
上載者:User

標籤:password   source   ase   bug   lin   資料庫連接   reads   lte   nim   

工具類public class MongoDBDao {    /**     * 擷取MONGODB用戶端執行個體     *      * @return     * @throws Exception     */    public static MongoClient getMongoClient() throws Exception {        try {            // 解密用密鑰            String sKey = SysPropertyJdbc.getProperty("jdbc.deskey")                    .substring(4, 28);            String sIp = DESUtil.decryptMode(sKey,                    MongoDBProperty.getProperty("mip"));            int iPort = Integer.valueOf(DESUtil.decryptMode(sKey,                    MongoDBProperty.getProperty("mport")));            String sUser = DESUtil.decryptMode(sKey,                    MongoDBProperty.getProperty("muser"));            String sPasword = DESUtil.decryptMode(sKey,                    MongoDBProperty.getProperty("mpassword"));            String sDbNm = DESUtil.decryptMode(sKey,                    MongoDBProperty.getProperty("mdb"));            // ===================================================//            List<ServerAddress> serverList = new ArrayList<ServerAddress>();            serverList.add(new ServerAddress(sIp, iPort));            // ===================================================//            List<MongoCredential> mcList = new ArrayList<MongoCredential>();            mcList.add(MongoCredential.createCredential(sUser, sDbNm,                    sPasword.toCharArray()));            // ===================================================//            MongoClientOptions.Builder builder = MongoClientOptions.builder();            // 與目標資料庫能夠建立的最大connection數量為50            builder.connectionsPerHost(50);            // 如果當前所的connection都在使用中,則每個connection上可以50個線程排隊等待            builder.threadsAllowedToBlockForConnectionMultiplier(50);            // 一個線程訪問資料庫的時候,在成功擷取到一個可用資料庫連接之前的最長等待時間為2分鐘            // 這裡比較危險,如果超過maxWaitTime都沒擷取到這個串連的話,該線程就會拋出Exception            // 故這裡設定的maxWaitTime應該足夠大,以免由於排隊線程過多造成的資料庫訪問失敗            builder.maxWaitTime(1000 * 60 * 2);            // 與資料庫建立串連的timeout設定為1分鐘            builder.connectTimeout(1000 * 60 * 1);            // ===================================================//            MongoClientOptions mco = builder.build();            return new MongoClient(serverList, mcList, mco);        } catch (Exception e) {            throw e;        }    }}操作類logger.debug("START...");        int page = 0;// 滾動式載入頁碼        int page10 = 0;        if (this.getPara("pagetimer") != null) {            page = Integer.parseInt(this.getPara("pagetimer"));// 滾動式載入頁碼            page10 = page * 10;        }        String umac = this.getPara("umac");        Calendar c = Calendar.getInstance();        c.add(Calendar.MONTH, -1);        // System.out.println(c.getTime()); 上個月時間        MongoDBDao mongoDBDao = new MongoDBDao();        MongoClient mongoClient = mongoDBDao.getMongoClient();        MongoDatabase database = mongoClient                .getDatabase(DESUtil.decryptMode(SysPropertyJdbc.getProperty("jdbc.deskey").substring(4, 28), MongoDBProperty.getProperty("mdb")));        MongoCollection<Document> collection = database.getCollection("user_timeline");        // 查詢條件        MongoCursor<Document> mongoCursor = collection.find(Filters.and(Filters.eq("umac", umac), Filters.gt("datetime", c.getTime()))).skip(page10)                .limit(10).iterator();        String timerShaft = "";// 返回顯示項目        while (mongoCursor.hasNext()) {            Iterator<Entry<String, Object>> iter = mongoCursor.next().entrySet().iterator();            Date datetime = null;            String host = "";            String resultdate = "";            while (iter.hasNext()) {                Entry eTmp = iter.next();                String sKeyTmp = eTmp.getKey().toString();                switch (sKeyTmp) {                case "datetime":                    datetime = (Date) eTmp.getValue();                    break;                case "host":                    host = eTmp.getValue().toString();                    break;                default:                    break;                }                if (datetime != null) {                    resultdate = dateDeal(datetime);                }            }            timerShaft = timerShaft + "<li><div class=\"animationTime\">" + resultdate + "</div><div class=\"animationTimeN\">" + host                    + "</div></li>";        }        this.setAttr("pagetimer", page);        this.setAttr("timerShaft", timerShaft);工具類public class SysPropertyJdbc {    /**     * 初期化     */    private SysPropertyJdbc() throws Exception {        this.props = new Properties();        props.load(this.getClass().getClassLoader()                .getResourceAsStream(PROPS_NAME));    }    private static final String PROPS_NAME = "jdbc.properties";    private static SysPropertyJdbc sysProps;    private Properties props;    public static String getProperty(String key) throws Exception {        return SysPropertyJdbc.getInstance().getProperties().getProperty(key);    }    private Properties getProperties() {        return this.props;    }    private static synchronized SysPropertyJdbc getInstance() throws Exception {        if (sysProps == null) {            sysProps = new SysPropertyJdbc();        }        return sysProps;    }}public class MongoDBProperty {    private MongoDBProperty() throws Exception {        this.props = new Properties();        props.load(this.getClass().getClassLoader()                .getResourceAsStream(PROPS_NAME));    }    private static final String PROPS_NAME = "mongodb.properties";    private static MongoDBProperty sysProps;    private Properties props;    public static String getProperty(String key) throws Exception {        return MongoDBProperty.getInstance().getProperties().getProperty(key);    }    private Properties getProperties() {        return this.props;    }    private static synchronized MongoDBProperty getInstance() throws Exception {        if (sysProps == null) {            sysProps = new MongoDBProperty();        }        return sysProps;    }} 

 

#mongodb.properties
mip=D9iw8GElYiWgjBrhRZgTRQ==
mport=3YtpA2QmgVM=
muser=RiT52btdOdCcVP34zzX6Xg==
mpassword=HjCIDeJQfMmo9w1ghPC+UPiE88wPzOLh
mdb=SiO7ZySzZh3l2G2+OCVjMw==

 

MongoDBDao 工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.