프로그래밍/JAVA

[OSGi] 서비스 등록 및 해제

chance 2014. 3. 21. 17:13

OSGi


이클립스 4.x 에서 기본 OSGi Framework 실행 오류.

Run Configuration > OSGi Framework > Bundle 탭


TargetPlatform 을 모두 체크해제하고, 왼쪽의 Add Required Bundles , Validate Bundles 버튼 선택

아래 타켓만 추가

org.apache.felix.gogo.command

org.apache.felix.gogo.runtime

org.apache.felix.gogo.shell


org.eclipse.osgi

org.eclipse.equinox.console


필요한 경우 Settings 탭에서 Clear the configuration area before launching 체크 후 실행



번들 컨텍스트

Bundle bundle = FrameworkUtil.getBunjdle( 클래스);

BundleContext bundleContext = bundle.getBundleContext();


서비스 등록

ServiceRegistration regist = 

bundleContext.registerService( 클래스명, 오브젝트, 프로퍼티 );


ex)

public class MyClass implements IMyService {

.

.

}


IMyService myService;

IMyService object = new MyClass();

Dictionary dic = new Properties();

dic.put("key", "value");


bundleContext.registerService( IMyService.class.getName(), object, dic);



서비스 해제

regist.unregister();


서비스 가져오기

ServiceReference refer = bundleContext.getServiceReference(클래스);

IMyService service = (IMyService) bundleContext.getService( refer );


서비스 반환

bundleContext.ungetService(refer );