モノリシックモード(Mongoオブジェクトの作成)

14042 ワード


単一モード:
餓漢式単例
// . ,    

public class Singleton1 {  

    //   

    private Singleton1() {}  

    //    

    private static final Singleton1 single = new Singleton1();  

    //    

    public static Singleton1 getInstance() {  

        return single;  

    }  

}

怠け者式単例
// .    

public class Singleton2 {  

    //   

    private Singleton2() {}  

    // , final      

    private static Singleton2 single=null;  

    //    

    public synchronized  static Singleton2 getInstance() {  

         if (single == null) {    

             single = new Singleton2();  

         }    

        return single;  

    }  

}  

Mongoオブジェクトの簡単な例:
単一のクラスでは、その属性も単一の例にすることができます.例えば、この例のwMongodbは、プライベートな構造関数でこの属性を初期化すれば、属性の単一の例化を実現できます.
 1 public class MongoDB {

 2 

 3     private Mongo wMongodb;

 4     

 5     private static MongoDB instance = null;

 6     

 7     private static synchronized MongoDB GetInstance()

 8     {

 9         if(!isInstanceAlive())

10         {

11             instance = new MongoDB();

12         }

13         return instance;

14     }

15 

16     /**

17      *  

18      * @return true: <br/>

19      *        false: 

20      */

21     private static boolean isInstanceAlive() {

22         boolean retBool = false;

23         try {

24             //  

25             DBCollection col = instance.wMongodb.getDB("olacloud").getCollection("noexist");

26             col.find().count();

27             retBool = true;

28         } catch (Exception e) {

29             try {

30                 instance.wMongodb.close();

31             } catch (Exception ex) {}

32         }

33         return retBool;

34     }

35     

36     private MongoDB()

37     {

38         try {

39             MongoClientOptions.Builder voicedbBuilder = MongoClientOptions.builder();

40             voicedbBuilder.connectTimeout(3000);

41             voicedbBuilder.socketTimeout(6000);

42             voicedbBuilder.autoConnectRetry(true);

43             voicedbBuilder.connectionsPerHost(5);

44             voicedbBuilder.readPreference(ReadPreference.secondaryPreferred());

45             voicedbBuilder.socketKeepAlive(true);

46             MongoClientOptions voicedbOptions = voicedbBuilder.build();

47 

48 //            wMongodb = new MongoClient(new ServerAddress("172.16.10.15", 27020),voicedbOptions);

49             wMongodb = new MongoClient(new ServerAddress("wmongo.olavoice.com", 27020),voicedbOptions);

50             

51             DB db = wMongodb.getDB("olacloud");

52 //            DB db = wMongodb.getDB("olacloud_internal");

53             db.authenticate("olacloud", "olacloud".toCharArray());

54             db.setWriteConcern(WriteConcern.SAFE);

55         } catch (Exception e) {

56             // TODO Auto-generated catch block

57             e.printStackTrace();

58         }

59         

60     }

61 

62     /**

63      *  

64      * @return  

65      */

66     public static DB getDB() {

67         DB db = MongoDB.GetInstance().wMongodb.getDB("olacloud");

68 //        DB db = MongoDB.GetInstance().wMongodb.getDB("olacloud_internal");

69         return db;

70     }

71 

72     /**

73      *  table 

74      * @param tableName table 

75      * @return table 

76      */

77     public static DBCollection getDBCollection(String tableName) {

78         DB db = MongoDB.getDB();

79         DBCollection col = db.getCollection(tableName);

80         return col;

81     }

82     

83     /**

84      *  Mongodb 

85      */

86     public static void close() {

87         if (instance != null) {

88             instance.wMongodb.close();

89         }

90     }

91 }