In my current post, I will try to highlight using event admin with
spring DM. By an example, we will send an event inside the OSGi
container from a notifier bundle and intercept by second subscriber
bundle.
1 - Notifier Bundle
- It contains a single Activator class.
- Get an Event Admin service.
- Send an event every 30 seconds.
- An event is according to JTUNISIe queue and containing the current step and time stamp.
01.
package
com.jtunisie.osgi.event.notifier.impl;
02.
03.
import
java.util.Date;
04.
import
java.util.Properties;
05.
import
org.osgi.service.event.Event;
06.
import
org.osgi.service.event.EventAdmin;
07.
import
org.springframework.osgi.extensions.annotation.ServiceReference;
08.
09.
public
class
Activator {
10.
11.
private
String EVENT_QUEUE;
12.
13.
protected
void
init()
throws
InterruptedException {
14.
Properties props =
new
Properties();
15.
int
i=
0
;
16.
while
(
true
) {
17.
props.setProperty(
"property"
,
""
+i+++
" : "
+
new
Date());
18.
eventAdmin.sendEvent(
new
Event(EVENT_QUEUE, props));
19.
Thread.sleep(
30
*
1000
);
20.
}
21.
}
22.
private
EventAdmin eventAdmin;
23.
24.
@ServiceReference
25.
public
void
setEventAdmin(EventAdmin eventAdmin) {
26.
this
.eventAdmin = eventAdmin;
27.
}
28.
29.
public
void
setEVENT_QUEUE(String EVENT_QUEUE) {
30.
this
.EVENT_QUEUE = EVENT_QUEUE;
31.
}
32.
}
On the spring config file we just call the init method after bundle starting :
01.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
05.
xsi:schemaLocation="http:
06.
http:
07.
http:
08.
>
09.
10.
<bean id=
"activator"
class
=
"com.jtunisie.osgi.event.notifier.impl.Activator"
init-method=
"init"
>
11.
<property name=
"EVENT_QUEUE"
value=
"JTUNISE"
/>
12.
</bean>
13.
14.
<bean
class
=
"org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor"
/>
15.
</beans>
2 - Subscriber Bundle
- Contains a single Activator class
- Implements EventHandler Interface
- When an event is intercepted it prints the new property.
01.
package
com.jtunisie.osgi.event.subscriber.impl;
02.
03.
import
org.osgi.service.event.Event;
04.
import
org.osgi.service.event.EventHandler;
05.
06.
07.
08.
09.
10.
public
class
Activator
implements
EventHandler {
11.
12.
private
String property;
13.
14.
@Override
15.
public
void
handleEvent(Event event) {
16.
17.
String value = event.getProperty(property).toString();
18.
System.out.println(
"value : "
+ value);
19.
}
20.
21.
public
void
setProperty(String property) {
22.
this
.property = property;
23.
}
24.
}
Spring DM helps us to register our bundle as an event hander service and setting it to listen jtunisie queue.
01.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
07.
xsi:schemaLocation="http:
08.
http:
09.
http:
10.
http:
11.
>
12.
13.
14.
<bean id=
"activator"
class
=
"com.jtunisie.osgi.event.subscriber.impl.Activator"
>
15.
<property name=
"property"
value=
"property"
/>
16.
</bean>
17.
18.
<osgi:service id=
"activatorService"
ref=
"activator"
interface
=
"org.osgi.service.event.EventHandler"
>
19.
<osgi:service-properties>
20.
<entry key=
"event.topics"
value=
"JTUNISE"
/>
21.
</osgi:service-properties>
22.
</osgi:service>
23.
24.
25.
</beans>
3- Deployment
- Project is composed of one Maven project with two sub projects using Felix plug-in.
- At the deployment level we need :
1- An OSGi container.
2- Spring dependencies ( Extenders, beans ...) with spring-osgi-annotations
After installing the two bundles, the output should be the same as :
01.
osgi> value : 1 : Tue Nov 11 23:57:33 CET 2008
02.
value : 2 : Tue Nov 11 23:58:03 CET 2008
03.
value : 3 : Tue Nov 11 23:58:33 CET 2008
04.
value : 4 : Tue Nov 11 23:59:03 CET 2008
05.
value : 5 : Tue Nov 11 23:59:33 CET 2008
06.
value : 6 : Wed Nov 12 00:00:03 CET 2008
07.
value : 7 : Wed Nov 12 00:00:33 CET 2008
08.
value : 8 : Wed Nov 12 00:01:03 CET 2008
09.
value : 9 : Wed Nov 12 00:01:33 CET 2008
10.
value : 10 : Wed Nov 12 00:02:03 CET 2008
11.
value : 11 : Wed Nov 12 00:02:33 CET 2008
12.
value : 12 : Wed Nov 12 00:03:03 CET 2008
13.
value : 13 : Wed Nov 12 00:03:33 CET 2008
14.
value : 14 : Wed Nov 12 00:04:03 CET 2008
Note : source code is under svn : svn checkout http://osgievent.googlecode.com/svn/trunk/ osgievent-read-only
分享到:
相关推荐
### OSGi与Spring:Spring DM开发环境配置详解 #### 一、引言 随着软件架构的不断发展,模块化和微服务化的趋势日益明显。在Java领域,OSGi(Open Service Gateway Initiative)作为一套成熟的技术标准,为实现模块...
**OSGI MAVEN SPRINGDM 文档集** 这个文档集主要涵盖了三个关键的Java开发技术:OSGI(Open Service Gateway Initiative)、Maven以及Spring Dynamic Modules(Spring DM)。这些技术都是现代Java开发中的重要组成...
标题 "SpringDM笔记31-Testing with OSGi and SpringDM" 涉及到的是在OSGi环境中使用SpringDM进行测试的相关知识。OSGi(Open Service Gateway Initiative)是一种Java模块化系统,允许动态地发现、加载和卸载服务。...
Spring DM(Dependency Manager)则是Spring框架对OSGi环境的支持,它使得Spring应用可以在OSGi容器中无缝运行。 在OSGi环境中,每个Java类库或服务都被封装在一个称为bundle的独立单元中。这些bundles可以独立更新...
### SpringDM与OSGI概述 #### OSGI概念解析 OSGI(Open Service Gateway Initiative),直译为“开放的服务网关初始化”,它是一系列针对Java动态化模块化系统的规范。OSGI不仅指代一个官方联盟,还代表着由该联盟...
SpringDM(Spring Dynamic Modules)是Spring框架的一个扩展,专门用于OSGi(Open Service Gateway Initiative)环境中的应用程序开发。OSGi是一种Java模块化系统,它允许开发者将应用程序拆分成独立的、可热插拔的...
《SpringDM开发文档》是关于SpringDM框架的详细技术指南,该框架是在OSGi环境中运行Spring应用程序的关键组件。SpringDM,全称为Spring Dynamic Modules,是Spring框架针对OSGi(Open Service Gateway Initiative)...
Spring DM,全称为Spring Dynamic Modules,是Spring框架的一个扩展,主要设计用于开发在OSGi(Open Service Gateway Initiative)环境下的应用程序。OSGi是一种模块化系统,允许Java应用程序以组件的形式进行构建、...
标题“spring-dm-osgi整合jar包”和描述“spring-dm整合osgi所需所有jar包”提及的核心知识点是Spring Dynamic Modules(简称Spring DM)与OSGi(Open Service Gateway Initiative)框架的集成。这两个技术在Java...
标题中的"osgi_spring_dm_jr"可能是指OSGi(Open Service Gateway Initiative)框架下,Spring Dynamic Modules(Spring DM)的Java Runtime环境相关的知识。OSGi是一种模块化系统,用于构建可升级、可扩展和可配置...
【标题】:“基于Spring DM Server的OSGi实例” 在IT领域,OSGi(Open Services Gateway Initiative)是一种模块化系统和Java服务平台,它允许开发者创建可热插拔的组件,从而提高了软件的可维护性和可扩展性。...
Spring DM是Spring框架的一个扩展,专门用于服务导向架构(SOA)和OSGi(Open Services Gateway Initiative)环境。而Apache Felix是一个遵循OSGi规范的开源服务框架,它允许开发人员在模块化环境中创建、部署和管理...
Spring DM 1.1.x最大特性便...在多个Spring DM支持OSGi平台上运行)并且Spring DM Server并没有提供更多企业应用支持 不过对于刚 使用Spring DM进行WEB应用开发人来说成功地配置却不是件容易事,文档详细讲解了相关配置
四、spring-osgi-1.2.1-with-dependencies.zip 这个文件包含了Spring OSGi的核心库和其依赖,主要包含以下几个部分: 1. spring-osgi-core:提供OSGi环境下的Spring核心功能,如Bean管理、事件发布等。 2. spring-...
标题 "OSGI+SpringDM+Hessian" 涉及到三个重要的技术概念,它们分别是OSGI(Open Services Gateway Initiative)、SpringDM(Spring Dynamic Modules)和Hessian。这些技术在Java开发领域中都有其独特的应用和价值。...
Spring-DM指的是 Spring ...Spring-DM 的主要目的是能够方便地将 Spring 框架和OSGi框架结合在一起,使得使用Spring的应用程序可以方便简单地部署在OSGi环境中,利用OSGi框架提供的服务,将应用变得 更加模块化。
在选择OSGi实现时,通常会考虑Equinox、Apache Felix和Spring DM(现在称为Spring OSGi),这三种流行的实现各有特点和优势。 1. **Equinox**:由Eclipse基金会维护,是OSGi R4规范的核心框架实现。Equinox以其稳定...
SpringDM,全称为Spring Dynamic Modules,是Spring框架的一个扩展,专为基于OSGi(Open Services Gateway Initiative)的应用程序设计。OSGi是一种Java模块化系统,它允许开发人员创建可独立部署、热更新和依赖管理...