生命周期中关键3个类:
BundleActivator
入口点,类似main方法
BundleContext
Bundle上下文对象,在运行期间,为应用程序提供操作osgi框架的方法
Bundle
代表一个已安装的Bundle
接口说明:
BundleActivator:
public interface BundleActivator { public void start(BundleContext context) throws Exception; public void stop(BundleContext context) throws Exception; }BundleContext:
public interface BundleContext { ... String getProperty(String key); Bundle getBundle(); Bundle installBundle(String location, InputStream input) throws BundleException; Bundle installBundle(String location) throws BundleException; Bundle getBundle(long id); Bundle[] getBundles(); void addBundleListener(BundleListener listener); void removeBundleListener(BundleListener listener); void addFrameworkListener(FrameworkListener listener); void removeFrameworkListener(FrameworkListener listener); ... }Bundle:
... BundleContext getBundleContext(); long getBundleId(); Dictionary getHeaders(); Dictionary getHeaders(String locale); String getLocation(); int getState(); String getSymbolicName(); Version getVersion(); void start(int options) throws BundleException; void start() throws BundleException; void stop(int options) throws BundleException; void stop() throws BundleException; void update(InputStream input) throws BundleException; void update() throws BundleException; void uninstall() throws BundleException;
Bundle状态图:
源代码下载
demo:
准备:
在karaf_home下新建config、logs文件夹将my-shell下的server.properties复制到karaf_home/config下
加入log4j日志配置,参考
部署Bundle
bundle:install -s mvn:com.demo.shell/my-shell/1.0.0-SNAPSHOT打开window命令行输入:
telnet localhost 7080输入help,可以查看所有命令帮助
输入install命令,部署外部Bundle:
install http://localhost:9090/testweb/bundle/user-model-0.0.1-SNAPSHOT.jar在karaf中输入list,可以查看已经部署的bundle id
在window命令行输入命令,启动bundle
start bundle_id在karaf中输入list,查看已部署的Bundle处于Active状态
osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo,布布扣,bubuko.com
时间: 2024-12-25 02:12:25