我有一个使用 OSGi 构建的 Java 应用程序,我想在不同的模式下运行,比如远程和本地数据源。我希望能够构建和部署单个版本,以便我可以在远程模式下将应用程序作为服务运行,然后停止服务并在本地模式下尝试不同的操作。
我正在使用声明式服务。
有办法做到这一点吗?
# app -remote
Starting app in remote mode
Disabling com.example.data.local.FileStoreDao
Enabling com.example.data.remote.MySqlDao
...
反之亦然:
# app -local
Starting app in localmode
Disabling com.example.data.remote.MySqlDao
Enabling com.example.data.local.FileStoreDao
...
或者类似的东西。
最佳答案
引用理查德·霍尔的话:
The configuration of your application == The set of installed bundles.
最好和最可维护的解决方案是为每个运行时“模式”安装一组(稍微)不同的 bundle 。例如,大多数 bundle 都是相同的,但您要么部署 MySqlDao bundle ,要么部署 FileStoreDao。使用允许您轻松设置和启动不同 bundle 组合的工具或启动器至关重要。
如果您确实想在不更改 bundle 集的情况下执行此操作,则可以将 MySqlDao 和 FileStoreDao 打包到一个 bundle 中,并使用 DS 根据来自 Config Admin 的配置数据启用/禁用其中一个。
关于java - OSGi 下配置不同/运行 "modes"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8088070/