在網上搜了下,相關的資料不多也不少,但都不是很詳細,所以記下來,方便大家參考一下。
例子用到了:
- apache的felix(版本4.0.2)
- springDM(版本1.2.1)中的log4j的jar包。
首先,實現一個bundle類:
public class Activator implements BundleActivator
{
private static final Logger log = Logger.getLogger(Activator.class);
public void start(BundleContext context)
{
log.info("Starting our bundle.");
}
public void stop(BundleContext context)
{
log.info("Stopping our bundle.");
}
}
代碼十分簡單,僅僅是爲了看看我們的log是否都工作,接着準備相應的manifest.mf文件:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.2 Created-By: 1.6.0_29-b11-402-11M3527 (Apple Inc.) Bundle-Name: LogTest Bundle-Description: A bundle that test logging Bundle-Vendor: OuYang Bundle-Version: 1.0.0 Bundle-Activator: tutorial.example1.Activator Bundle-ManifestVersion: 2 Import-Package: org.osgi.framework,org.apache.log4j Bundle-SymbolicName: exampleOne
因爲用到了log4j,所以Import-Package中加上了org.apache.log4j,接着把上面的Activator.class文件和manifest.mf文件打包成jar文件(命名爲example1.jar)。
要在OSGi中使用log4j,需要在一個單獨的bundle中提供log4j.properties文件,並且這個bundle要定義成fragment的,先看manifest.mf文件:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.2 Created-By: 1.6.0_29-b11-402-11M3527 (Apple Inc.) Bundle-ManifestVersion: 2 Bundle-SymbolicName: Log4JProperties Fragment-Host: org.springframework.osgi.log4j.osgi
上面的Fragment-Host: org.springframework.osgi.log4j.osgi表示這個bundle是fragment的,它的主bundle是org.springframework.osgi.log4j.osgi,也就是springDM中的log4j這個jar包了。
接着準備一個log4j.properties文件:
log4j.rootLogger=INFO, stdout, R log4j.logger.tutorial=INFO log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%d]-%-5p %t | %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=/Users/ouyang/logs/osgi-new-test.log log4j.appender.R.MaxFileSize=5000KB log4j.appender.R.MaxBackupIndex=5 log4j.appender.R.Threshold=DEBUG log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=[%d]-%-5p %t | %m%n
接着把manifest文件和log4j.properties文件打包成jar文件(命名爲log-bundle.jar)。
最終生成的兩個bundle的結構如下:
example1.jar --tutorial.example1 --Activator.class --MATE-INF --MANIFEST.MF log-bundle.jar --MATE-INF --MANIFEST.MF --log4j.properties
最後啓動felix,安裝並啓動相關的bundle,就可以看到log 信息被成功的輸出了。
g! install file:///Users/ouyang/Desktop/123/log4j.osgi-1.2.15-SNAPSHOT.jar Bundle ID: 5 g! install file:///Users/ouyang/Desktop/123/log-bundle.jar Bundle ID: 6 g! install file:///Users/ouyang/Desktop/123/example1.jar Bundle ID: 7 g! lb START LEVEL 1 ID|State |Level|Name 0|Active | 0|System Bundle (4.0.2) 1|Active | 1|Apache Felix Bundle Repository (1.6.6) 2|Active | 1|Apache Felix Gogo Command (0.12.0) 3|Active | 1|Apache Felix Gogo Runtime (0.10.0) 4|Active | 1|Apache Felix Gogo Shell (0.10.0) 5|Installed | 1|log4j.osgi (1.2.15.SNAPSHOT) 6|Installed | 1|Log4JProperties (0.0.0) 7|Installed | 1|Service listener example (1.0.0) g! start 7 [2012-01-04 17:09:43,974]-INFO Gogo shell | Starting our bundle. g! stop 7 [2012-01-04 17:09:49,151]-INFO Gogo shell | Stopping our bundle. g!