服务osgi_OSGi –具有服务的简单Hello World

服务osgi

在本文中,我们将使用OSGi开发一个简单的Hello World应用程序。 我们将使用Felix作为OSGi容器 。 在下一篇文章中,我们将继续使用该应用程序,并使用Spring Dynamic Modules对其进行改进。

为了使开发有趣,我们将创建两个捆绑包:

  • 提供HelloWorld服务的捆绑包
  • 消耗该服务以定期间隔打个招呼的捆绑软件。

因此,让我们从第一个捆绑包开始。 我们首先需要的是一个非常简单的服务,在控制台中提供简单的打印:

package com.bw.osgi.provider.able;public interface HelloWorldService {void hello();
}
package com.bw.osgi.provider.impl;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldServiceImpl implements HelloWorldService {@Overridepublic void hello(){System.out.println("Hello World !");}
}

我们再简单不过了。 然后,我们需要使用激活器导出服务:

package com.bw.osgi.provider;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;public class ProviderActivator implements BundleActivator {private ServiceRegistration registration;@Overridepublic void start(BundleContext bundleContext) throws Exception {registration = bundleContext.registerService(HelloWorldService.class.getName(),new HelloWorldServiceImpl(),null);}@Overridepublic void stop(BundleContext bundleContext) throws Exception {registration.unregister();}
}

这里有很多代码。 对于那些对OSGi不确定的人,可以做一些解释。 当模块启动时将调用start方法,而在模块停止时将调用stop方法。 在start方法中,我们使用接口名称作为导出服务的名称在包上下文中注册我们的服务。 第三个参数null表示我们不为此服务提供任何配置。 在stop方法中,我们只是取消注册服务。

现在,我们的第一个捆绑包已准备就绪。 我们可以建立它。 为此,我们将使用Maven和maven-bundle-plugin直接构建OSGi Bundle。 这是项目的pom.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>OSGiDmHelloWorldProvider</groupId><artifactId>OSGiDmHelloWorldProvider</artifactId><version>1.0</version><dependencies><dependency><groupId>org.apache.felix</groupId><artifactId>org.osgi.core</artifactId><version>1.4.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-SymbolicName>OSGiDmHelloWorldProvider</Bundle-SymbolicName><Export-Package>com.bw.osgi.provider.able</Export-Package><Bundle-Activator>com.bw.osgi.provider.ProviderActivator</Bundle-Activator><Bundle-Vendor>Baptiste Wicht</Bundle-Vendor></instructions></configuration></plugin></plugins></build> 
</project>

然后,使用mvn install进行构建。

我们将在一个名为osgi的文件夹中工作,因此我们会将这个新捆绑包复制到osgi文件夹中。

我们已经可以在OSGi容器中对其进行测试。 如果您还没有Felix,请在这里下载。 您必须选择“ Felix分布”。

然后将其解压缩到我们之前创建的osgi文件夹中。 现在,您必须具有以下文件夹结构:

osgi- felix- OSGiDmHelloWorldProvider-1.0.jar

这样我们就可以进入felix文件夹并启动Felix:

wichtounet@Linux-Desktop:~/Desktop/osgi/felix$ java -jar bin/felix.jar
_______________
Welcome to Apache Felix Gogog!

并安装我们的捆绑包:

g! install file:../OSGiDmHelloWorldProvider-1.0.jar
Bundle ID: 5

该安装已正确安装,我们可以尝试启动它并查看其状态:

g! start 5
g! bundle 5
Location             file:../OSGiDmHelloWorldProvider-1.0.jar
State                32
Version              1.0.0
LastModified         1279124741320
Headers              [Tool=Bnd-0.0.357, Bundle-Activator=com.bw.osgi.provider.ProviderActivator, Export-Package=com.bw.osgi.provider.able, Build-Jdk=1.6.0_20, Bundle-Version=1.0.0, Created-By=Apache Maven Bundle Plugin, Bundle-ManifestVersion=2, Manifest-Version=1.0, Bundle-Vendor=Baptiste Wicht, Bnd-LastModified=1279124686551, Bundle-Name=Unnamed - OSGiDmHelloWorldProvider:OSGiDmHelloWorldProvider:bundle:1.0, Built-By=wichtounet, Bundle-SymbolicName=OSGiDmHelloWorldProvider, Import-Package=com.bw.osgi.provider.able,org.osgi.framework;version="1.5"]
BundleContext        org.apache.felix.framework.BundleContextImpl@2353f67e
BundleId             5
SymbolicName         OSGiDmHelloWorldProvider
RegisteredServices   [HelloWorldService]
ServicesInUse        null

一切都很好。 我们的服务注册良好

现在,我们将尝试在第二个捆绑包中使用此服务。 我们的消费者类别将非常简单:

package com.bw.osgi.consumer;import javax.swing.Timer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldConsumer implements ActionListener {private final HelloWorldService service;private final Timer timer;public HelloWorldConsumer(HelloWorldService service) {super();this.service = service;timer = new Timer(1000, this);}public void startTimer(){timer.start();}public void stopTimer() {timer.stop();}@Overridepublic void actionPerformed(ActionEvent e) {service.hello();}
}

现在,我们必须创建激活器以获取服务,然后将其提供给消费者。 这将使使用类似这样的东西:

package com.bw.osgi.consumer;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldActivator implements BundleActivator {private HelloWorldConsumer consumer;@Overridepublic void start(BundleContext bundleContext) throws Exception {ServiceReference reference = bundleContext.getServiceReference(HelloWorldService.class.getName());consumer = new HelloWorldConsumer((HelloWorldService) bundleContext.getService(reference));consumer.startTimer();}@Overridepublic void stop(BundleContext bundleContext) throws Exception {consumer.stopTimer();}
}

我们使用类的名称从捆绑软件上下文中获得服务引用。 之后,我们从包上下文获取服务实例。

我们还创建了一个小的pom.xml文件来构建捆绑包:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>OSGiDmHelloWorldConsumer</groupId><artifactId>OSGiDmHelloWorldConsumer</artifactId><version>1.0</version><packaging>bundle</packaging><dependencies><dependency><groupId>org.apache.felix</groupId><artifactId>org.osgi.core</artifactId><version>1.0.0</version></dependency><dependency><groupId>OSGiDmHelloWorldProvider</groupId><artifactId>OSGiDmHelloWorldProvider</artifactId><version>1.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-SymbolicName>OSGiDmHelloWorldConsumer</Bundle-SymbolicName><Bundle-Activator>com.bw.osgi.consumer.HelloWorldActivator</Bundle-Activator><Bundle-Vendor>Baptiste Wicht</Bundle-Vendor></instructions></configuration></plugin></plugins></build>
</project>

然后,我们使用mvn install创建捆绑包,并将其安装在容器中:

g! start 6
g! Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
g! stop 6

至此,我们已经使用OSGi创建了第一个应用程序。 使用该技术,您可以构建真正独立的模块。

在有关OSGi的下一篇文章中,我们将看到如何使用Spring来简化OSGi的开发,并将精力集中在应用程序而非OSGi上。

这两个捆绑包的来源可在此处获取:

  • OSGiDmHelloWorldProvider来源
  • OSGiDmHelloWorldConsumer

参考: OSGi –简单的Hello World,由JCG合作伙伴 Baptiste Wicht在@Blog(“ Baptiste Wicht”)提供服务 。

相关文章 :
  • OSGi将Maven与Equinox结合使用
  • 真正的模块化Web应用程序:为什么没有开发标准?
  • Java模块化方法–模块,模块,模块

翻译自: https://www.javacodegeeks.com/2011/11/osgi-simple-hello-world-with-services.html

服务osgi

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/358776.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java反射(Reflection)

基本概念 在Java运行时环境中&#xff0c;对于任意一个类&#xff0c;能否知道这个类有哪些属性和方法&#xff1f;对于任意一个对象&#xff0c;能否调用它的任意一个方法&#xff1f; 答案是肯定的。 这种动态获取类的信息以及动态调用对象的方法的功能来自于Java语言的反射&…

超详细的MySQL三万字总结

文章目录 MySQL基础数据库的介绍数据库概述数据的存储方式数据库的概念常见数据库排行榜 数据库的安装与卸载数据库的安装数据库的卸载 数据库服务的启动与登录Windows 服务方式启动DOS 命令方式启动控制台连接数据库SQLyog 图形化工具——客户端使用 SQLyog 登录数据库数据库…

map转字符串数组中 php_js将map转换成数组

linux中oops信息的调试及栈回溯【转】本文转载自:http://blog.csdn.net/kangear/article/details/8217329 ...linux 2&period;6 驱动笔记(一)本文作为linux 2.6 驱动笔记,记录环境搭建及linux基本内核模块编译加载. 环境搭建: 硬件:OK6410开发板 目标板操作系统:linux 2.6…

你要的能做出炫酷图表的网站来啦

不需要多高深的技术&#xff0c;也不需要多长时间&#xff0c;分分钟让小白都能做出超好看的图表~ 1.DataV DataV 是阿里云出品的在线可视化工具&#xff0c;可以将超多数据&#xff0c;放在一块大屏上 2.网易有数 网易有数的特点是&#xff0c;编辑页面自由度非常高&#x…

ecshop获取客户端操作系统

<?php /*** 获得客户端的操作系统** access private* return void*/ function get_os() {if (empty($_SERVER[HTTP_USER_AGENT])){return Unknown;}$agent strtolower($_SERVER[HTTP_USER_AGENT]);$os ;if (strpos($agent, win) ! false){if (strpos($agent, nt 5.1…

求有向图中两点最短距离java_算法题解:求有向图中的最短路径(JAVA+DFS算法实现)...

求有向图中的最短路径(JAVADFS算法实现)问题描述给定一个有向图&#xff0c;如下图所示&#xff0c;求从1号顶点到5号顶点的最短路径。输入数据格式为第一行输入顶点数和边数&#xff0c;从第二行开始每一行输入3个整数&#xff0c;分别代表连接顶点的边和权重。例如&#xff1…

B站学习资源汇总

一个B站就足够解决绝大多数学习问题。 AI入门 1、Crash Course AI https://space.bilibili.com/276373762/channel/detail?cid=101907 课程介绍:该课程出自Crash Course,首发YouTube,单个视频时长均在10分钟左右。视频节奏非常好,语速偏快,适合下饭时间入门了解。主讲…

30个Java入门技巧和最佳实践

Java是最流行的编程语言之一-无论是Win应用程序&#xff0c;Web应用程序&#xff0c;移动&#xff0c;网络&#xff0c;消费电子产品&#xff0c;机顶盒设备&#xff0c;Java随处可见。 在Java上运行的设备超过30亿。 据甲骨文称 &#xff0c;正在使用50亿张Java卡。 超过900…

Postman使用详解

一、Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的&#xff0c;用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具。今天给大家介绍的这款网页调试工具不仅可以调试简单的css、html、脚本等简单的网…

java double精确比较,Java float比double更精确?

Code:class Main {public static void main (String[] args) {System.out.print("float: ");System.out.println(1.35f-0.00026f);System.out.print("double: ");System.out.println(1.35-0.00026);}}Output:float: 1.34974double: 1.3497400000000002??…

《Bash 脚本教程》免费发布啦,开源!

一共写了 20 节&#xff0c;Bash 脚本编程的主要语法&#xff0c;都包括在内了&#xff0c;日常使用应该足够。也欢迎初学者使用这个教程&#xff0c;学习 Bash。 这个教程是开源的&#xff0c;你可以克隆它的代码仓库&#xff0c;放在本地&#xff0c;也可以提交 issue 和 pu…

linux脚本编程(shell)浅介 (转载)

linux脚本(shell)编程 啊&#xff0c;昨天上网看到一篇讲 linux/unix shell 的文章&#xff0c;想想自己最后写这东西也是一年前的事了&#xff0c;想想都快忘光了。 还是整理一下&#xff0c;做一次回顾&#xff0c;以后说不定还用得上&#xff1b;帖出来&#xff0c;方便第一…

postman插件下载安装教程(详细)

一、前言 postman是一款强大网页接口调试工具&#xff0c;我们在平时开发过程中经常会使用到&#xff0c;一般使用最多的是postman的客户端&#xff0c;实际上postman在谷歌浏览器上也提供了插件&#xff0c;可以不必要安装客户端进行接口测试工作。 建议更新到最新谷歌浏览器&…

java显示星期几_Java 使用日历显示星期几

import java.util.Calendar;/*nowjava.com 提 供*/public class Main {public static void main(String[] args) {//create Calendar instanceCalendar now Calendar.getInstance();System.out.println("Current date : " (now.get(Calendar.MONTH) 1) "-&qu…

SharePoint 2013 workflow cannot start automatically when you logged in site as a system account

I have created one simple workflow on custom list using SharePoint designer 2013.While designing workflow, I have unchecked the “Allow this workflow to be manually started” and select rest two options like “Start workflow automatically when an item is c…

10个 IDEA 插件来解放你的双手

不知道喜欢IDEA的你装了没有? 1、GenerateAllSetter 实际的开发中,可能会经常为某个对象中多个属性进行 set 赋值,尽管可以用BeanUtil.copyProperties()方式批量赋值,但这种方式有一些弊端,存在属性值覆盖的问题,所以不少场景还是需要手动 set。如果一个对象属性太多 s…

哥斯拉Godzilla shell管理工具

各大厂商的waf不断&#xff0c;在静态查杀、流量通信等方面对webshell进行拦截&#xff0c;众红队急需一款优秀的权限管理工具&#xff0c;冰蝎3.0的发布可能缓解了流量加密的困境&#xff0c;但是冰蝎3.0的bug众多&#xff0c;很多朋友甚至连不上冰蝎的shell&#xff0c; 于是…

使用Fabric8在Kubernetes中使用Camel和CDI

序幕 我最近在博客上发表了使用CDI注入Kubernetes服务的信息 。 在本文中&#xff0c;我将更进一步&#xff0c;将Apache Camel带入图片。 因此&#xff0c;我将使用Camel的CDI支持来连接我的组件和路由&#xff0c;以及Fabric8的CDI扩展来自动将Kubernetes服务注入到我的组件中…

sql双表查询java代码_原生sql 多表查询

Session session Session session super.getSession();session.flush();Connection con session.connection();String uid"";StringBuffer sql new StringBuffer();sql.append("select a.user_Id from user_info as a");//user用户表sql.append( left j…

写lua时需要注意的地方

条件语句判断时&#xff0c;只有false和nil会导致判断为假&#xff0c;其他的任何值都为真。 Lua 的字符串与编码无关&#xff1b; 它不关心字符串中具体内容。标准 Lua 使用 64 位整数和双精度&#xff08;64 位&#xff09;浮点数&#xff0c; 但你也可以把 Lua 编译成使用 3…