我正在开发 OSGi Mongodb bundle ,我还添加了以下依赖项
- com.mongodb
- org.apache.felix.fileinstal
- org.amdatu.mongo
- org.apache.felix.configadmin
以及所有依赖管理器,但在 gogo 控制台中我收到以下错误消息
org.amdatu.mongo
org.osgi.service.cm.ManagedServiceFactory(service.pid=org.amdatu.mongo) registered
org.osgi.service.log.LogService service optional unavailable
[11] agenda.mongodb.mongo_gfs
agenda.mongo.inter.AgendaMongo() unregistered
org.amdatu.mongo.MongoDBService service required unavailable
主要问题是MongoDBService不可用我必须需要这个服务来解决这个问题我已经根据他们阅读了这本书
From a development perspective, everything seems fine, but when you run the appli‐ cation, it will complain that the MongoDBService is unavailable. You can figure this out with the dmcommand in the shell. We did however set up MongoDB on our system and deployed the necessary dependencies in our runtime. Still, the MongoDBService was unable to start. How come? This is because the MongoDBService needs some mandatory configuration in order to know to what database to connect to. The Amdatu MongoDB Serviceuses the Managed Service Factory pattern (see Chapter 4), and in order to bootstrap it, we need to supply a configuration file. In order to supply the configuration file, we need to create a new folder in our agendaproject. Create a new folder called load. This is the default name that the runtime will look for in order to spot configuration files. Next, add an empty text file and call it something like org.amdatu.mongo-demo.xml. The configuration file needs at least the following information: dbName=demo
我也应用了这个,但仍然不可用。
这是界面:
package agenda.mongo.inter;
import java.io.InputStream;
public interface AgendaMongo {
public String store_in_db();
public InputStream getData(Object file_id);
}
这是 Mongodb 的实现:
package agenda.mongodb.gridfs;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import org.amdatu.mongo.MongoDBService;
import org.bson.types.ObjectId;
import agenda.mongo.inter.AgendaMongo;
import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
public class Gridfs_Mongodb implements AgendaMongo{
GridFSInputFile gfsinput=null;
private volatile MongoDBService mongoservice;
public String store_in_db() {
/*try {
GridFS gfsHandler;
gfsHandler = new GridFS(mongoservice.getDB(), "rest_data");// database
File uri = new File("f:\\get1.jpg"); // name and
gfsinput = gfsHandler.createFile(uri);
gfsinput.saveChunks(1000);
gfsinput.setFilename("new file");
gfsinput.save();
//System.out.println(gfsinput.getId());
//save_filepath("file",gfsinput.getId());
Object get_id = gfsinput.getId();//get_filename();
//System.out.println(getData(get_id));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
//System.out.println("Exception");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//System.out.println("Exception");
e.printStackTrace();
}*/
System.out.println("DB:" + mongoservice.getDB());
return mongoservice.getDB()+"";
}
/*
* Retrieving the file
*/
public InputStream getData(Object file_id) {
GridFS gfsPhoto = new GridFS(mongoservice.getDB(), "rest_data");
GridFSDBFile dataOutput = gfsPhoto.findOne((ObjectId) file_id);
DBCursor cursor = gfsPhoto.getFileList();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println(dataOutput);
return dataOutput.getInputStream();
}
void start(){
System.out.println("hello");
System.out.println(store_in_db());
}
}
这里我只是想获取数据库名称,因为之后所有的事情都可以完成,但我返回了 NULL,因为 MongoDBService
不可用。
这是Activator
类
package agenda.mongodb.gridfs;
import org.amdatu.mongo.MongoDBService;
import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;
import agenda.mongo.inter.AgendaMongo;
public class Activator extends DependencyActivatorBase {
@Override
public void init(BundleContext arg0, DependencyManager manager)
throws Exception {
manager.add(createComponent()
.setInterface(AgendaMongo.class.getName(), null)
.setImplementation(Gridfs_Mongodb.class)
.add(createServiceDependency()
.setService(MongoDBService.class)
.setRequired(true)));
}
@Override
public void destroy(BundleContext arg0, DependencyManager arg1)
throws Exception {
// TODO Auto-generated method stub
}
}
接口(interface)包是导出包,实现包是私有(private)的。
最佳答案
配置文件应具有 .cfg 扩展名(而不是 .xml)。
关于java - 如何让MongoDB服务可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863893/