千家信息网

如何使用eclipse开发felix的OSGI插件

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要讲解了"如何使用eclipse开发felix的OSGI插件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何使用eclipse开发feli
千家信息网最后更新 2026年10月31日如何使用eclipse开发felix的OSGI插件

这篇文章主要讲解了"如何使用eclipse开发felix的OSGI插件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何使用eclipse开发felix的OSGI插件"吧!

1、官网下载http://felix.apache.org/downloads.cgi,
当前最新版本6.0.3
运行felix窗口有两种方式
(1) 可以直接下载发布版本
Felix Framework Distribution(org.apache.felix.main.distribution-6.0.3.zip)

解压后目录

进入项目目录,命令行下执行

java -jar ./bin/felix.jar

felix.jar 默认会在当前运行目录的 bundle 文件夹内加载启动所需的插件jar,config 目录下的 config.propeties 和 system.properties 里面加载环境变量,如果将其他目录作为启动根目录,该目录下不存在 felix 启动所需信息,启动就会有问题。项目第一次启动的时候会在启动目录下创建一个felix-cache的目录,用于存放框架运行过程产生的数据,当然该目录可以在启动的时候配置,使用 java -jar ./bin/felix.jar 即可。

(2) 源码导入eclipse运行
下载子项目Main(org.apache.felix.main-6.0.3-project.zip)


解压org.apache.felix.main-6.0.3-project.zip,进入目录,命令行下
运行mvn eclipse:eclipse工程文件,
再输入mvn package,打包编译
File->Import导入到eclipse

在eclipse的org.apache.felix.main.Main类右键运行Run As Java Application

常用命令

lb:查看已装插件包start:运行插件包stop:停止运行插件包install:安装插件包uninstall:删除插件包其它命令可以通过help查看

2、felix开发插件
参考是felix官方例子代码apache-felix-osgi-tutorial里的
apache-felix-tutorial-example-2
apache-felix-tutorial-example-2b
apache-felix-tutorial-example-3
这是一个检查输入单词(英文、中文....)是否正确的例子功能

先定义一个服务类:

package test.penngo.service; /** * 简单的定义了一个字典服务接口,该接口的功能只是简单的验证一个词是否正确。**/public interface DictionaryService{    /**            * 检查 单词是否存在    **/    public boolean checkWord(String word);}

英文词典插件包开发

package tutorial.example2;import java.util.Hashtable;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import test.penngo.service.DictionaryService;/** * 英文字典服务插件 **/public class Activator implements BundleActivator {        /**         * 服务启动         *          * @param context the framework context for the bundle.         **/        public void start(BundleContext context) {                Hashtable props = new Hashtable();                props.put("Language", "English");                context.registerService(DictionaryService.class.getName(), new DictionaryImpl(), props);        }        /**         * 服务停止         *          * @param context the framework context for the bundle.         **/        public void stop(BundleContext context) {                // NOTE: The service is automatically unregistered.        }        /**         * 英文词典服务实现         **/        private static class DictionaryImpl implements DictionaryService {                String[] m_dictionary = { "welcome", "to", "the", "osgi", "tutorial" };                /**                 * 英文单词检测                 **/                public boolean checkWord(String word) {                        word = word.toLowerCase();                        for (int i = 0; i < m_dictionary.length; i++) {                                if (m_dictionary[i].equals(word)) {                                        return true;                                }                        }                        return false;                }        }}

META-INF.MF

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: EnglishBundle-Name: English dictionaryBundle-Description: A bundle that registers an English dictionary serviceBundle-Vendor: penngoBundle-Version: 1.0.0Bundle-Activator: tutorial.example2.ActivatorImport-Package: org.osgi.framework, test.penngo.service

英文词典插件打包导出,eclipse自带的export打包会有问题,本文使用fatjar工具打包,

中文词典插件包开发

package tutorial.example2b; import java.util.Hashtable;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import test.penngo.service.DictionaryService;/** * 中文词典包**/public class Activator implements BundleActivator{    public void start(BundleContext context)    {        Hashtable props = new Hashtable();        props.put("Language", "Chinese");        context.registerService(            DictionaryService.class.getName(), new DictionaryImpl(), props);    }     public void stop(BundleContext context)    {        // NOTE: The service is automatically unregistered.    }     /**            *  中文词典服务实现    **/    private static class DictionaryImpl implements DictionaryService    {        // The set of words contained in the dictionary.        String[] m_dictionary =            { "欢迎", "你好", "教程", "模块" };         public boolean checkWord(String word)        {            word = word.toLowerCase();            for (int i = 0; i < m_dictionary.length; i++)            {                if (m_dictionary[i].equals(word))                {                    return true;                }            }            return false;        }    }}
Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: ChineseBundle-Name: Chinese dictionaryBundle-Description: A bundle that registers a Chinese dictionary serviceBundle-Vendor: penngoBundle-Version: 1.1.0Bundle-Activator: tutorial.example2b.ActivatorImport-Package: org.osgi.framework, test.penngo.service

插件打包

客户端调用词典开发,也是作为一个felix插件

package tutorial.example3;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import test.penngo.service.DictionaryService; /** * 此客户端在启动时,接收控制台输入,检查输入的单词是否正确,并且打印初相应的检测结果,  * 注意:当我们没有输入任何信息的时候,单词检测便会结束,如果需要重新进入单词检测程序,我们需要重新启动插件。**/public class Activator implements BundleActivator{     public void start(BundleContext context) throws Exception    {        // 获取所有词典插件        ServiceReference[] refs = context.getServiceReferences(            DictionaryService.class.getName(), "(Language=*)");                if (refs != null)        {                System.out.println("=======当前可用词典插件:" + refs.length);            try            {                System.out.println("Enter a blank line to exit.");                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));                String word = "";                while (true)                {                    System.out.print("Enter word: ");                    word = in.readLine();                    if (word.length() == 0)                    {                        break;                    }                     boolean isCorrect = false;                    for(ServiceReference service:refs) {                            DictionaryService dictionary =                                (DictionaryService) context.getService(service);                            if (dictionary.checkWord(word))                            {                                    isCorrect = true;                                System.out.println("Correct.");                            }                                              }                    if(isCorrect == false)                    {                        System.out.println("Incorrect.");                    }                }            } catch (IOException ex) {                     ex.printStackTrace();            }        }        else        {            System.out.println("Couldn't find any dictionary service...");        }    }     /**     * Implements BundleActivator.stop(). Does nothing since     * the framework will automatically unget any used services.     * @param context the framework context for the bundle.    **/    public void stop(BundleContext context)    {        // NOTE: The service is automatically released.    }}
Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: clientBundle-Name: Dictionary clientBundle-Description: A bundle that uses the dictionary service if it finds it at startupBundle-Vendor: penngoBundle-Version: 1.3.0Bundle-Activator: tutorial.example3.ActivatorExport-Package: test.penngo.serviceImport-Package: org.osgi.framework#//注意:test.penngo.service这个包与tutorial.example3打在同一个jar中,如果example3.jar更新,其它依赖test.penngo.service的也需要更新或重启felix.

安装插件
把上边生成的插件包example2.jar,example2b.jar,example3.jar入到org.apache.felix.main-6.0.3项目的plugins文件夹中
安装和运行效果:

通过插件方式开发,后期继续扩展增加其它语言词典(日语,俄语,韩语),不需要重启服务,也不影响已存在的语言词典功能(英文、中文)。

3、开发http服务插件

package test.penngo.http.example;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {        @Override        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {                resp.getWriter().write("hello penngo");        }}
package test.penngo.http.example;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import org.osgi.service.http.HttpService;import org.osgi.util.tracker.ServiceTracker;public class Activator implements BundleActivator {        private ServiceTracker httpTracker;        public void start(BundleContext context) throws Exception {                httpTracker = new ServiceTracker(context, HttpService.class.getName(), null) {                        public void removedService(ServiceReference reference, Object service) {                                // HTTP service is no longer available, unregister our servlet...                                try {                                        ((HttpService) service).unregister("/hello");                                } catch (IllegalArgumentException exception) {                                        // Ignore; servlet registration probably failed earlier on...                                }                        }                        public Object addingService(ServiceReference reference) {                                // HTTP service is available, register our servlet...                                HttpService httpService = (HttpService) this.context.getService(reference);                                try {                                        httpService.registerServlet("/hello", new TestServlet(), null, null);                                } catch (Exception exception) {                                        exception.printStackTrace();                                }                                return httpService;                        }                };                // start tracking all HTTP services...                httpTracker.open();        }        public void stop(BundleContext context) throws Exception {                // stop tracking all HTTP services...                httpTracker.close();        }}
#MANIFEST.MFManifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: helloBundle-Name: hello penngoBundle-Description: Http Hello WorldBundle-Vendor: penngoBundle-Version: 1.0.0Bundle-Activator: test.penngo.http.example.ActivatorImport-Package: org.osgi.framework, org.osgi.service.http, org.osgi.util.tracker, javax.servlet.http, javax.servlet

注意htpp插件包依赖:

org.apache.felix.http.base-4.0.6.jar,org.apache.felix.http.bridge-4.0.8.jar,org.apache.felix.http.jetty-4.0.10.jar,org.apache.felix.http.servlet-api-1.1.2.jar,org.apache.felix.http.whiteboard-4.0.0.jar

需要先安装上边的插件包,才能正常在felix中运行http功能。
安装运行效果

浏览器访问:

感谢各位的阅读,以上就是"如何使用eclipse开发felix的OSGI插件"的内容了,经过本文的学习后,相信大家对如何使用eclipse开发felix的OSGI插件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

插件 词典 运行 开发 目录 服务 英文 单词 中文 输入 功能 命令 检测 文件 时候 问题 项目 学习 检查 上边 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 为想互联网科技怎么样 高科技互联网 港股互联网科技股 金融科技 互联网金融 大白互联网科技 互联网养老 桔豆科技 济南和联互联网科技有限公司 剑昆互联网络科技有限公司临桂 崇明区互联网教育科技诚信经营 中科明德北京互联网科技 工业互联网山东科技大学 互联网时代的科技公司 山东星卡互联网科技有限公司官网 富国互联网金融科技混合基金 上海创道互联网络科技有限公司 广州梦谷互联网科技有限公司领导 珠海东科互联网科技有限公司 互联网科技收购价值 深圳互联网科技园地址 高科技和互联网的现代国家 互联网科技名片 互联网金融与金融科技 广西互联网科技公司 互联网科技网站 互联网实体化科技化 未来五年的互联网科技 互联网媒体平台算高科技产业 华夏恒生互联网科技013171 长沙茂晟互联网科技有限公司 黑云互联网科技有限公司
0