1.准备工作
自己去官方下载,或者直接下载我第一篇文章中配置好的felix项目,自己下载的话比较麻烦,可能需要很多次进行尝试,包的版本不对,无法启动,懒得尝试或者想开箱即用的兄弟请移步到:
2.下载配置idea osgi插件
3.新建两个maven项目
需要源码的去我上面的连接去下载一下,在上面
felix-clinet bundle
package com.nxhz.felix.client; import com.nxhz.felix.server.Hello; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; /** * @author jierui * @package PACKAGE_NAME * @date 2022/7/13-9:33 * @description: */ public class ClientBundleActivator implements BundleActivator { @Override public void start(BundleContext ctx) throws Exception { System.out.println("----------------hello client start---------------------"); ServiceReference ref = ctx.getServiceReference(Hello.class.getName()); if (ref != null) { Hello hello = null; try { hello = (Hello) ctx.getService(ref); if (hello != null){ hello.sayHello(); }else{ System.out.println("Service:Hello---object null"); } } catch (RuntimeException e) { e.printStackTrace(); } finally { ctx.ungetService(ref); hello = null; } } else { System.out.println("Service:Hello---not exists"); } System.out.println("----------------hello client end---------------------"); } @Override public void stop(BundleContext bundleContext) throws Exception { System.out.println("stop Service:Hello"); } }
felix-server bundle
package com.nxhz.felix; import com.nxhz.felix.server.Hello; import com.nxhz.felix.server.impl.IHelloImpl; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import java.util.ArrayList; import java.util.List; /** * @author jierui * @package com.nxhz.felix * @date 2022/7/13-9:51 * @description: */ public class ServerBundleActivator implements BundleActivator { private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>(); private static BundleContext context; static BundleContext getContext() { return context; } @Override public void start(BundleContext bundleContext) throws Exception { ServerBundleActivator.context = bundleContext; System.out.println("----------------server start---------------------"); //注册hello接口中的服务 registrations.add(bundleContext .registerService(Hello.class.getName(), new IHelloImpl(), null)); System.out.println("----------------server end---------------------"); } @Override public void stop(BundleContext bundleContext) throws Exception { ServerBundleActivator.context = null; for (ServiceRegistration registration : registrations) { System.out.println("unregistering: " + registration); registration.unregister(); } } }
4.配置idea osgi debug
4.1新建一个Run/Debug 启动选择OSGI Bundles
4.2 配置OSGI Project Structure
client manifest
Export-Packag: com.nxhz.felix Import-Package: org.osgi.framework,com.nxhz.felix.server;version="1.0.4"
server manifest
Export-Package: com.nxhz.felix.server;version="1.0.4" Import-Package: org.osgi.framework
4.3 配置启动debug
5.idea配置felix web console
访问地址:http://localhost:8080/system/console/bundles/
用户名密码:admin/admin
我其实没有配置密码,需要的可以自己修改