# OSGI DS 註解(annotation) 使用 ###### tags: `OSGI` `Declarative Services` `Annotation` 參考文獻: https://liferay.dev/blogs/-/blogs/liferay-osgi%25E6%25B3%25A8%25E8%25A7%25A3%25EF%25BC%2588annotation%25EF%25BC%2589-%25E4%25BD%25BF%25E7%2594%25A8%25E6%2589%258B%25E5%2586%258C-%25E8%25AF%2591%25E6%2596%2587- ## Declarative Services環境: felix 環境要有三個 ``` org.apache.felix.scr 2.1.16 org.osgi.util.promise 1.1.1.201810101357 org.osgi.util.function 1.1.0.201802012106 ``` eclipse 環境 ``` <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.component.annotations</artifactId> <version>1.4.0</version> <scope>provided</scope> </dependency> ``` ## Declarative Service(DS) http://blog.vogella.com/2016/06/21/getting-started-with-osgi-declarative-services/ http://blog.vogella.com/2016/09/26/configuring-osgi-declarative-services/ 使用DS,就不需要register 與 get Service的冗余動作,而且服務的相關步驟會一切都處理好 ### @Component 需要再osgi發佈任何組件時用 immediate:立即start properties:配置組件,或是過濾 service:定義組件的服務,可以是接口 也可以是實例 範例 ``` @Component(service = hello.class, immediate = true) public class hello { @Override public void sayhello() { System.out.println("hello"); } } @Component(service = Servlet.class, property = { "alias=/api" }) public class HttpApi extends HttpServlet { } ``` ### @Reference 需要把其他組件加入到自己組件的時候 target: 過濾 service: policy: cardinality: 組件啟動前 引入是否必須或是其他機制 1. MANDITORY: 1. OPTIONAL: 1. MULTIPLE: 1. AT_LEAST_ONE: 範例 ``` @Component() public class StaticConfiguredComponent { @Reference(service = hello.class) public void setHello(hello hoo){ hoo.sayhello(); } @Reference(service = DBtools.class, policy = ReferencePolicy.STATIC, cardinality = ReferenceCardinality.AT_LEAST_ONE) public void setHello(hello hoo){ hoo.sayhello(); } } ``` ### @Activate @Deactivate 啟動,停止調用的方法 ``` @Activate public void start() { System.out.println("test~"); } @Deactivate public void stop() { System.out.println("bye test~"); } ``` ### @Compose 註冊http api 需配合 felix whiteborad使用 ``` @Component(service = Servlet.class, property = { "alias=/api" }) public class HttpApi extends HttpServlet { } ``` ## Component 使用 inline properties ``` @Component( property = { "message=Welcome to the inline configured service", "iteration:Integer=3" } ) public class StaticConfiguredComponent { @Activate void activate(Map<String, Object> properties) { String msg = (String) properties.get("message"); Integer iter = (Integer) properties.get("iteration"); for (int i = 1; i <= iter; i++) { System.out.println(i + ": " + msg); } System.out.println(); } } ``` ## 使用 resource files 這邊是用的是bndtools的方式 ``` @Component( properties="OSGI-INF/config.properties" ) public class FileConfiguredComponent { @Activate void activate(Map<String, String> properties) { String msg = (String) properties.get("message"); String iter = (String) properties.get("iteration"); if (msg != null && iter != null) { Integer count = Integer.valueOf(iter); for (int i = 1; i <= count; i++) { System.out.println(i + ": " + msg); } System.out.println(); } } } ``` ## 不使用DS,註冊及使用服務的方式 ``` import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpService; public class Activator implements BundleActivator { // private HttpService service; public void start(BundleContext context) throws Exception { // System.out.println("123123"); // // ServiceReference ref2 = context.getServiceReference(ConnectionPool.class.getName()); // ConnectionPool pool = (ConnectionPool) context.getService(ref2); // // ServiceReference serviceReference = context.getServiceReference(HttpService.class.getName()); // service = (HttpService) context.getService(serviceReference); // // // service.registerServlet("/api", new HttpApi(pool), null, null); // System.out.println("register /api"); } public void stop(BundleContext context) throws Exception { // stop tracking all HTTP services... // System.out.println("345345"); // service.unregister("/api"); // System.out.println("byebye /api"); // context = null; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up