我有问题
我正在使用 jelastic 来托管 java 和 mongoDB 应用程序。而且我的应用程序与 Jelasctic 提供的 mongoDB 之间的连接存在问题。
他们的配置文件如下所示:
公共类 MongoManager {
static String host, dbname, user, password;
public void addData(int repeats) {
try {
DBCollection dbc = null;
Properties prop = new Properties();
prop.load(new FileInputStream(System.getProperty("user.home") + "/mydb.cfg"));
host = prop.getProperty("host").toString();
dbname = prop.getProperty("dbname").toString();
user = prop.getProperty("user").toString();
password = prop.getProperty("password").toString();
System.out.println("host: " + host + "\ndbname: " + dbname + "\nuser: " + user + "\npassword: " + password);
Mongo m = new Mongo(host, 27017);
DB db = m.getDB(dbname);
if (db.authenticate(user, password.toCharArray())) {
System.out.println("Connected!");
} else {
System.out.println("Connection failed");
}
try {
db.getCollection("mycollection");
} catch (Exception e) {
db.createCollection("mycollection", null);
} finally {
System.out.println("Repeats: " + repeats);
for (int i = 1; i <= repeats; i++) {
BasicDBObject data = new BasicDBObject("data", new Date());
db.getCollection("mycollection").save(data);
System.out.println("INFO: row added " + data);
}
}
} catch (IOException ex) {
}
}
和我的
公共类 MongodbUtil {
private static Mongo mongo = null;
private static Morphia morphia = null;
private static Datastore ds = null;
private MongodbUtil() {};
public static synchronized DB getDB(String str) throws Exception {
if(mongo == null) {
mongo = new Mongo();
}
return mongo.getDB(str);
}
public static synchronized Mongo getMongo() throws Exception {
if(mongo == null) {
mongo = new Mongo("localhost", 27017);
}
return mongo;
}
public static synchronized Morphia getMorphia() throws Exception {
if(morphia == null) {
mongo = getMongo();
morphia = new Morphia();
morphia.mapPackage("com.sogeti.simulator.entity");
}
return morphia;
}
public static synchronized Datastore getDataStore(){
if(ds == null){
try {
morphia = getMorphia();
ds = morphia.createDatastore(mongo, "Simulator");
} catch (Exception e) {
e.printStackTrace();
}
}
return ds;
}
是同一个文件吗?如何输入我的配置文件属性,如 host ?密码 ?和别的 ?!
我的 MongoDB.cfg.xml 看起来像这样,但我认为这很糟糕,因为我不使用 SPRING 或 MAVEN:我在网络上没有看到任何简单的 MongoDB.cfg.xml 示例。
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd;">
<dependency>
<groupId>com.morphia.Morphia</groupId>
<artifactId>morphia</artifactId>
<version>0.99</version>
</dependency>
<dependency>
<groupId>com.mongodb.Mongo</groupId>
<artifactId>mongo</artifactId>
<version>2.10.1</version>
</dependency>