-
[ OSGi ] 5 - Spring DM 2JAVA 2014. 8. 26. 18:03728x90
[ OSGi ] 4 - Spring DM 1 에서 Spring 관련 bundle을 올리고 Activator 대신 spring.xml에 설정한 init-method, destory-method 사용을 알아봤다.
여기서는 Spring 을 이용해서 OSGi의 Service에 등록하는 것을 해보겠다.
( 앞에서도 말했지만 일반적인 Service 등록은 http://xguru.net/tag/osgi 에서 OSGi 시작하기 4, OSGi 사직하기 5 를 참고하자. )서비스에 등록할 인터페이스 생성
1. interface 생성
HelloWorldOSGiSpringDM 프로젝트에 HelloWorldOSGiSpringDMPrinter.java와 HelloWorldOSGiSpringDMPrinterImpl.java 파일을 생성한다.
2. code 작성
kr.co.jabsiri.helloworldosgi.springdm.HelloWorldOSGiSpringDMPrinter.java
kr.co.jabsiri.helloworldosgi.springdm.HelloWorldOSGiSpringDMPrinterImpl.java
3. spring.xml 내용 추가
4. MANIFEST.MF 수정
Export-Package: kr.co.jabsiri.helloworldosgi.springdm.service 를 추가하자
등록된 서비스 사용
[ OSGi ] 3 - OSGi plugin 의존 관계 에서 작성한 HelloWorldOSGiSub 프로젝트에서 위에서 작성한 SubActivator.java 에서 helloWorldOSGiSpringDMPrinter 서비스를 호출해 보도록 하자.
1. MANIFEST.MF
Import-Package 부분에 kr.co.jabsiri.helloworldosgi.springdm.service 를 추가한다.
2. 등록된 서비스 사용
SubActivator.java 에 빨간박스 부분의 코드를 추가하자
이 코드는 서비스로 등록된 내용을 가져와 사용하는 것이다.
배포 & 실행
1. HelloWorldOSGiSpringDM과 sample.HelloWorldOSGiSub 두 프로젝트를 Export 한다.
2. HelloWorldOSGiSpringDM_1.0.0.jar install
install file:HelloWorldOSGiSpringDM_1.0.0.jar 로 plugin을 install 시키고
b 22로 plugin의 상세 정보를 확인하면 밑에 빨간색 부분처럼 HelloWorldOSGiSpringDMPrinter가 서비스에 등록되어 있는 모습을 볼수 있다.
osgi> install file:HelloWorldOSGiSpringDM_1.0.0.jar
Bundle id is 22
Location file:HelloWorldOSGiSpringDM_1.0.0.jar
State 2
Bundle 22|Installed | 1|HelloWorldOSGiSpringDM (1.0.0)
Version 1.0.0
SymbolicName HelloWorldOSGiSpringDM
BundleContext null
Module osgi.identity; osgi.identity="HelloWorldOSGiSpringDM"; type="osgi.bundle"; version:Version="1.0.0" [id=22]
ServicesInUse null
RegisteredServices null
BundleId 22
LastModified 1409211674925
Headers Bundle-ManifestVersion = 2
Bundle-Name = HelloWorldOSGiSpringDM
Bundle-RequiredExecutionEnvironment = JavaSE-1.6
Bundle-SymbolicName = HelloWorldOSGiSpringDM
Bundle-Version = 1.0.0
Export-Package = kr.co.jabsiri.helloworldosgi.springdm.service
Manifest-Version = 1.0
osgi> start 22
osgi> spring dm start
osgi> b 22
HelloWorldOSGiSpringDM_1.0.0 [22]
Id=22, Status=ACTIVE Data Root=C:\develop\OSGi\sample\plugins\configuration\org.eclipse.osgi\22\data
"Registered Services"
{kr.co.jabsiri.helloworldosgi.springdm.service.HelloWorldOSGiSpringDMPrinter}={org.springframework.osgi.bean.name=helloWorldOSGiSpringDMPrinter, Bundle-SymbolicName=HelloWorldOS
GiSpringDM, Bundle-Version=1.0.0, service.id=49, service.bundleid=22, service.scope=bundle}
{org.springframework.osgi.context.DelegatedExecutionOsgiBundleApplicationContext, org.springframework.osgi.context.ConfigurableOsgiBundleApplicationContext, org.springframework.
context.ConfigurableApplicationContext, org.springframework.context.ApplicationContext, org.springframework.context.Lifecycle, java.io.Closeable, org.springframework.beans.factory.L
istableBeanFactory, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.context.MessageSource, org.springframework.context.ApplicationEventPublisher, org.
springframework.core.io.support.ResourcePatternResolver, org.springframework.beans.factory.BeanFactory, org.springframework.core.io.ResourceLoader, java.lang.AutoCloseable, org.spri
ngframework.beans.factory.DisposableBean}={org.springframework.context.service.name=HelloWorldOSGiSpringDM, Bundle-SymbolicName=HelloWorldOSGiSpringDM, Bundle-Version=1.0.0, service
.id=50, service.bundleid=22, service.scope=singleton}
Services in use:
{org.springframework.beans.factory.xml.NamespaceHandlerResolver}={spring.osgi.core.bundle.id=18, spring.osgi.core.bundle.timestamp=1409042843468, service.id=47, service.bundleid
=19, service.scope=singleton}
{org.xml.sax.EntityResolver}={spring.osgi.core.bundle.id=18, spring.osgi.core.bundle.timestamp=1409042843468, service.id=48, service.bundleid=19, service.scope=singleton}
{org.osgi.service.packageadmin.PackageAdmin}={service.ranking=2147483647, service.pid=0.org.eclipse.osgi.internal.framework.legacy.PackageAdminImpl, service.vendor=Eclipse.org -
Equinox, service.id=11, service.bundleid=0, service.scope=singleton}
Exported packages
kr.co.jabsiri.helloworldosgi.springdm.service; version="0.0.0"[exported]
No imported packages
No fragment bundles
No required bundles
3. sample.HelloWorldOSGiSub_1.0.0.jar install
그럼 서비스에 등록된 내용을 사용하는 sample.HelloWorldOSGiSub_1.0.0.jar를 install 해보자
install file:sample.HelloWorldOSGiSub_1.0.0.jar
osgi> install file:sample.HelloWorldOSGiSub_1.0.0.jar
Bundle id is 23
Location file:sample.HelloWorldOSGiSub_1.0.0.jar
State 2
Bundle 23|Installed | 1|sample.HelloWorldOSGiSub (1.0.0)
LastModified 1409212486067
Headers Bundle-Activator = kr.co.jabsiri.helloworldosgisub.SubActivator
Bundle-ManifestVersion = 2
Bundle-Name = HelloWorldOSGiSub
Bundle-RequiredExecutionEnvironment = JavaSE-1.6
Bundle-SymbolicName = sample.HelloWorldOSGiSub
Bundle-Version = 1.0.0
Import-Package = org.osgi.framework;version="1.3.0",kr.co.jabsiri.helloworldosgi,kr.co.jabsiri.helloworldosgi.springdm.service
Manifest-Version = 1.0
Version 1.0.0
Module osgi.identity; osgi.identity="sample.HelloWorldOSGiSub"; type="osgi.bundle"; version:Version="1.0.0" [id=23]
RegisteredServices null
ServicesInUse null
BundleContext null
SymbolicName sample.HelloWorldOSGiSub
BundleId 23
osgi> start 23
print string : I am sub activator !!!
spring dm service print string : I am sub activator !!!
sample.HelloWorldOSGiSub 에서는 sample.HelloWorldOSGi도 사용하고 있으니 이것도 install을 시킨다.
install file:sample.HelloWorldOSGi_1.0.0.jar
osgi> install file:sample.HelloWorldOSGi_1.0.0.jar
Bundle id is 24
Location file:sample.HelloWorldOSGi_1.0.0.jar
State 2
Bundle 24|Installed | 1|sample.HelloWorldOSGi (1.0.0)
LastModified 1409212573947
Headers Bundle-Activator = kr.co.jabsiri.helloworldosgi.Activator
Bundle-ManifestVersion = 2
Bundle-Name = HelloWorldOSGi
Bundle-RequiredExecutionEnvironment = JavaSE-1.6
Bundle-SymbolicName = sample.HelloWorldOSGi
Bundle-Version = 1.0.0
Export-Package = kr.co.jabsiri.helloworldosgi
Import-Package = org.osgi.framework;version="1.3.0"
Manifest-Version = 1.0
Version 1.0.0
Module osgi.identity; osgi.identity="sample.HelloWorldOSGi"; type="osgi.bundle"; version:Version="1.0.0" [id=24]
RegisteredServices null
ServicesInUse null
BundleContext null
SymbolicName sample.HelloWorldOSGi
BundleId 24
그리고 start 23 을 하자 ( 23은 sample.HelloWorldOSGiSub 의 bundle 번호 )
osgi> start 23
print string : I am sub activator !!!
spring dm service print string : I am sub activator !!!
그럼. 위와같이 HelloWorldOSGiSpringDM의 HelloWorldOSGiSpringDMPrinter.java에 정의한 printer method가 실행된 것을 확인한다.
첨부 1 : HelloWorldOSGi plugin Eclipse Project
첨부 2 : OSGi Framework
728x90'JAVA' 카테고리의 다른 글
[ OSGi ] 7 - OSGi Eclipse 실행 (0) 2014.08.29 [ OSGi ] 6 - Spring DM 3 (0) 2014.08.28 [ OSGi ] 4 - Spring DM 1 (0) 2014.08.25 [ OSGi ] 3 - OSGi plugin 의존 관계 (0) 2014.08.20 [ OSGi ] 2 - OSGi plugin project (2) 2014.08.19