osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo

生命周期中关键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状态



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值