1核1g可以做几个网站,服务好质量好的app开发,朋友叫我去柬埔寨做彩票网站推广,长春网站设计长春网络推广OSGi联盟是这一搁浅的管理机构#xff0c;它始于1999年。其最初目标是为网络设备创建开放搁浅。 基于此思想#xff0c;此规范也针对Java引入。 Eclipse在Java中是第一个。 他们于2004年6月推出了基于OSGi的Eclipse IDE。 OSGi是在Java中定义动态模块的方法。 主要为Java实现… OSGi联盟是这一搁浅的管理机构它始于1999年。其最初目标是为网络设备创建开放搁浅。 基于此思想此规范也针对Java引入。 Eclipse在Java中是第一个。 他们于2004年6月推出了基于OSGi的Eclipse IDE。 OSGi是在Java中定义动态模块的方法。 主要为Java实现的三个OSGi容器是Apache Felix Eclipse Equinox和Knopflefish 。 为什么选择OSGi 因为OSGi提供了将应用程序划分为多个模块的能力并且那些模块易于与其他依赖项一起管理。 除此之外它非常容易安装更新停止和删除没有停止引擎的模块例如Tomcat Web应用程序容器。 我们可以使用多种实现方式来影响其他参考。 基于Web的Java框架主要有3个层表示层业务层和DAO层。 在那里我们可以将其分为三个基于OSGi的模块。 那么我们可以很容易地将错误修复到一层而不会影响其他错误并重新启动我们的Web容器。 只是我们需要更新模块。 在OSGi世界中输出是捆绑包可以是Jar或War文件。 捆绑软件由Java类和其他资源以及一些其他元数据组成向其他捆绑软件提供服务和软件包。 我将使用Eclipse IDE创建我的第一个捆绑软件。 因为Eclipse IDe内置了Equinox容器每个eclipse插件都是OSGi捆绑包。 创建Eclipse插件项目 转到新建–其他–插件项目 然后单击下一步将出现新项目创建对话框 提供项目名称和目标平台如下所示。 然后点击下一步 项目名称 com.chandana.Hello.HelloWorld 目标平台选择Stranded OSGi 在下一个屏幕中您可以更改捆绑包信息这些信息可在MANIFEST.MF中找到稍后我会提供详细信息然后单击“下一步”按钮。 之后将出现OSGi项目模板选择对话框。选择Hello OSGi Bundle然后单击Finish。 几秒钟后Eclipse将生成Hello World Plug-In-Project我几秒钟没有响应:) 在我的项目结构中是这样的 Activator.java package com.chandana.hello.helloworld;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;public class Activator implements BundleActivator {/** (non-Javadoc)* see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)*/public void start(BundleContext context) throws Exception {System.out.println(Hello World!!);}/** (non-Javadoc)* see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)*/public void stop(BundleContext context) throws Exception {System.out.println(Goodbye World!!);}} Activator是实现BundleActivator的类 接口。 它已经停了 然后开始 方法。 当捆绑包启动或停止时将调用这些方法 。 此束激活器类在MENIFEST.MF文件 束激活器条目中指定。 启动方法 捆绑包启动时OSGi容器将调用start方法。 我们可以使用此启动方法进行初始化的数据库连接注册服务以供其他捆绑使用。 停止方式 捆绑包停止时OSGi容器调用stop方法。 我们可以使用此方法从服务注册表中删除服务例如清理过程 清单文件 Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorld
Bundle-SymbolicName: com.chandana.Hello.HelloWorld
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.chandana.hello.helloworld.Activator
Bundle-Vendor: CHANDANA
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version1.3.0 Bundle-ManifestVersion Bundle-ManifestVersion标头显示OSGi容器该捆绑包遵循OSGi规范的规则。 值为2表示捆绑软件符合OSGi规范第4版。 值1表示它符合版本3或更早的版本。 捆绑名称 Bundle-Name标头定义捆绑的简短可读名称。 Bundle-SymbolicName Bundle-SymbolicName标头指定捆绑包的唯一名称。 这是您在从其他捆绑包中引用给定捆绑包时将使用的名称。 捆绑版本 Bundle-Version标头是捆绑软件的版本号。 捆绑销售商 Bundle-Vendor标头是供应商的描述例如这是我的名字。 进口包装 Import-Package指示此捆绑包还需要其他Java捆绑包OSGi。 我们称之为依赖性。 出口包装 导出包指示什么是捆绑包中的公共包那些导出包可以从其他捆绑包中导入。 运行捆绑包 对于“运行此项目”请单击“运行”-“运行配置”在OSGi Framework中右键单击并创建新的“运行配置”。 首先取消选中所有目标平台然后单击“ 添加必需的捆绑包” 。 之后单击“运行”按钮应用更改并运行项目。 运行后项目OSGi控制台显示如下。 OSGi终端命令 开始–启动指定的捆绑包 停止–停止指定的捆绑包 卸载–卸载指定的捆绑软件 更新–更新指定的捆绑包 刷新–刷新指定捆绑包的软件包 b –显示指定捆绑商品的详细信息 标头–打印包标头 服务–显示注册的服务详细信息 源代码 接下来我将描述如何创建基于依赖的OSGi捆绑软件。 OSGi服务是一个Java对象实例已向OSGi框架注册了一组属性。 可以通过服务注册表通过类BundleContext执行访问服务。 BundleActivator将在启动和停止时调用。 当BundleActivator调用start方法时我们将注册我们的服务。 之后任何捆绑包都可以访问该服务。 服务包 在服务包中您需要导出服务并需要通过服务注册表进行注册。 当我们导出服务时我们仅导出接口包。 与往常一样这是从其他捆绑包中隐藏实现。 我已经创建了一个名为HelloService的示例OSGi项目。 清单文件 Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService
Bundle-SymbolicName: com.chandana.hello.HelloService
Bundle-Version: 1.0.0
Bundle-Activator: com.chandana.hello.helloservice.Activator
Bundle-Vendor: CHANDANA
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version1.3.0
Export-Package: com.chandana.hello.service
Bundle-ActivationPolicy: lazy 服务接口 public interface HelloService { public String helloMethods();
} 服务实施 public class HelloServiceImpl implements HelloService {Overridepublic String helloMethods() {String retValue Inside Hello Service method;return retValue;}
} 边界激活剂 public class Activator implements BundleActivator {ServiceRegistration serviceRegistration;/** (non-Javadoc)* see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)*/public void start(BundleContext context) throws Exception {System.out.println(Bundle Started.....!!!!!);HelloService service new HelloServiceImpl();serviceRegistration context.registerService(HelloService.class.getName(), service,null);}/** (non-Javadoc)* see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)*/public void stop(BundleContext context) throws Exception {System.out.println(Bundle Stoped.....!!!!!);serviceRegistration.unregister();}
} 使用发布的服务时可以从另一个捆绑包中导入它。 因此需要为HelloClient创建另一个插件项目 捆绑内容 捆绑包上下文是OSGi运行时中单个捆绑包的上下文它是在捆绑包启动时创建的。 捆绑包上下文可用于安装新捆绑包通过其他捆绑包获取注册的服务以及在框架中注册服务。 清单文件 Import-Package: org.osgi.framework;version1.3.0,com.chandana.hello.service 导入捆绑软件后您可以访问该服务。 重要的是只能通过bundle上下文访问服务。 您可以通过BundleContext.getService方法获取实际的服务对象。 激活器类 public class Activator implements BundleActivator {ServiceReference serviceReference; /** (non-Javadoc)* see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)*/public void start(BundleContext context) throws Exception {serviceReference context.getServiceReference(HelloService.class.getName());HelloService helloService (HelloService)context.getService(serviceReference);System.out.println(helloService.helloMethods());}/** (non-Javadoc)* see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)*/public void stop(BundleContext context) throws Exception {context.ungetService(serviceReference);}
} context.getServiceReference方法返回HelloService OSGi服务引用使用该服务引用可以访问实际的服务对象。 对于“运行此项目”请单击“ 运行”-“运行配置” 在OSGi Framework中右键单击并创建新的“运行配置” 。 确保HelloService和HelloClient 。 问题 如果客户端访问服务时未启动服务会发生什么情况 如果您停止了该服务捆绑包会发生什么 代码仓库 http://code.google.com/p/osgi-world/source/browse/#svn/trunk/com.chandana.hello.HelloService http://code.google.com/p/osgi-world/source/browse/#svn/trunk/com.chandana.hello.HelloClient 参考来自JCG合作伙伴的 OSGiJava模块化 简介和OSGi – 2OSGi服务简介 Chandana Napagoda博客上的Chandana Napagoda 。 翻译自: https://www.javacodegeeks.com/2012/01/introduction-to-osgi-modular-java.html