---
title: "关于osgi中的2种BundleListener"
author: "codecook"
date: "2010-12-06T09:36:03Z"
tags: []
categories: ["企业架构"]
slug: "osgi-2-bundlelistener"
draft: false
lastmod: "2010-12-06T09:58:41Z"
id: 834202
url: "https://codecook.iteye.com/blog/834202"
---

BundleListener分为2种,一种是异步的,类名就叫BundleListener;另一种是同步的,类名叫SynchronousBundleListener.

2者都可监听当bundle 被installed, resolved, starting, started, stopping, stopped, updated, unresolved,  uninstalled.时触发的事件.只不过一个是同步,一个是异步的.

同步的可以阻挠Bundle状态的变迁,因为是在同一个线程中.比如当运行install bundle命令时,框架会先触发SynchronousBundleListener,再返回安装好的bundle.如果在SynchronousBundleListener中加入一个无限循环,则安装就不能继续下去.

SynchronousBundleListener在Felix中可以配合FileInstall这个Bundle实现Bundle更新前对Bundle进行备份等预处理操作.