티스토리 뷰

Web Development

OSGI for Java Bundle Programing

§무명소졸§ 2020. 3. 16. 18:29

OSGI(Open Service Gatewary Initiative) 는 java 애플리케이션간 동적으로 동작, 배포를 가능하게 하는  서비스 번들, 모듈화를 위한 specification이다.. 예를 들면 이클립스IDE 의 플러그인 설치를 통해 기능을 확장해 가는 것과 같다.(이클립스도 OSGI Framework 인 Eclipse Equinox을 이용한다.) OSGI specification 구현하는 Framework는 몇 가지 종류가 있는데 오늘은 그중 하나인 apache felix와 felix를 이용한 OSGI 플랫폼인 apache karaf를 이용해서 간단한 예제를 만들어 보겠다. 

Pom.xml

bundle작업을 하기위해 felix maven plugin 과 osgi dependency 를 추가한다. 

<packaging>bundle</packaging>
<dependencies>
	<dependency>
		<groupId>org.osgi</groupId>
		<artifactId>org.osgi.core</artifactId>
		<version>6.0.0</version>
	</dependency>
</dependencies>
<build>
<plugins>
	<plugin>
		<groupId>org.apache.felix</groupId>
		<artifactId>maven-bundle-plugin</artifactId>
		<extensions>true</extensions>
		<configuration>
			<instructions>
				<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
				<Bundle-Name>${project.artifactId}</Bundle-Name>
				<Bundle-Version>${project.version}</Bundle-Version>
				<Bundle-Activator>info.m2sj.SimpleBundle</Bundle-Activator>
				<Private-Package>info.m2sj</Private-Package>
			</instructions>
		</configuration>
	</plugin>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<configuration>
			<source>6</source>
			<target>6</target>
		</configuration>
	</plugin>
</plugins>
</build>

 

SimpleBundle

클래스파일을 번들링하기 위해서는 BundleActivator라는 클래스를 구현하고 2개의 메서드를 override 해야한다. stdout 로 문자열을 출력하는 간단한 클래스이다. 

public class SimpleBundle implements BundleActivator {
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("start bundle..");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("stop bundle..");
    }
}

클래스 작성이 완료되면 mvn install 명령어로 maven repostiory에 추가해준다. 그래야지 karaf console 에서 인스톨을 할 수 있다.

➜  simpleosgi mvn clean install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for info.m2sj:simpleosgi:bundle:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 36, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ------------------------< info.m2sj:simpleosgi >------------------------
[INFO] Building simpleosgi 1.0-SNAPSHOT
[INFO] -------------------------------[ bundle ]-------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ simpleosgi ---
[INFO] Deleting /Users/mhkim/m2sj_project/simpleosgi/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ simpleosgi ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ simpleosgi ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/mhkim/m2sj_project/simpleosgi/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ simpleosgi ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/mhkim/m2sj_project/simpleosgi/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ simpleosgi ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ simpleosgi ---
[INFO] 
[INFO] --- maven-bundle-plugin:3.5.1:bundle (default-bundle) @ simpleosgi ---
[INFO] 
[INFO] --- maven-install-plugin:3.0.0-M1:install (default-install) @ simpleosgi ---
[INFO] Installing /Users/mhkim/m2sj_project/simpleosgi/target/simpleosgi-1.0-SNAPSHOT.jar to /Users/mhkim/.m2/repository/info/m2sj/simpleosgi/1.0-SNAPSHOT/simpleosgi-1.0-SNAPSHOT.jar
[INFO] Installing /Users/mhkim/m2sj_project/simpleosgi/pom.xml to /Users/mhkim/.m2/repository/info/m2sj/simpleosgi/1.0-SNAPSHOT/simpleosgi-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-bundle-plugin:3.5.1:install (default-install) @ simpleosgi ---
[INFO] Installing info/m2sj/simpleosgi/1.0-SNAPSHOT/simpleosgi-1.0-SNAPSHOT.jar
[INFO] Writing OBR metadata
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.704 s
[INFO] Finished at: 2020-03-18T12:43:59+09:00
[INFO] ------------------------------------------------------------------------

 

Apache Karaf

OSGI 플랫폼인 Apache Karaf를 설치하겠다. https://karaf.apache.org/download.html  이동해서 자신의 운영 체제에 맞는 Karaf Runtime 을 다운 받으면 된다. 

다운로드가 완료 됐으면 tar -xvf  tar xvf ./apache-karaf-4.2.8.tar.gz 명령어로 압축을 해제한다.

Apache Karaf 실행

Apache Karaf 의 설치 디렉토리 이동후에 bin/karaf start 명령어로 Karaf를 실행한다.

➜  bin ./karaf start
        __ __                  ____
       / //_/____ __________ _/ __/
      / ,<  / __ `/ ___/ __ `/ /_
     / /| |/ /_/ / /  / /_/ / __/
    /_/ |_|\__,_/_/   \__,_/_/

  Apache Karaf (4.2.8)

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown Karaf.

karaf@root()>

이제 CLI 환경에서 명령어를 입력해서 SimpleBundle 파일을 불러오겠다.

karaf@root()> bundle:install mvn:info.m2sj/simpleosgi/1.0-SNAPSHOT
Bundle ID: 44

start, stop 명령어를 실행해서 번들링된 클래스 파일을 실행할 수 있다.

karaf@root()> start 44
start bundle..
karaf@root()> stop 44
stop bundle..
karaf@root()>

 

Reference

아주 간단히 OSGI에 대해서 알아보았다. 전체 예제 코드와 OSGI에 더 많은 내용들은 아래 링크를 참고하면 된다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크