|
| 1 | +--- |
| 2 | +author: "Phong Nguyen" |
| 3 | +title: "Java OSGi Service" |
| 4 | +date: "2025-02-10" |
| 5 | +description: "Java: OSGi Service." |
| 6 | +tags: ["java"] #tags search |
| 7 | +FAcategories: ["syntax"] #The category of the post, similar to tags but usually for broader classification. |
| 8 | +FAseries: ["Themes Guide"] #indicates that this post is part of a series of related posts |
| 9 | +aliases: ["migrate-from-jekyl"] #Alternative URLs or paths that can be used to access this post, useful for redirects from old posts or similar content. |
| 10 | +ShowToc: true # Determines whether to display the Table of Contents (TOC) for the post. |
| 11 | +TocOpen: true # Controls whether the TOC is expanded when the post is loaded. |
| 12 | +weight: 1 # The order in which the post appears in a list of posts. Lower numbers make the post appear earlier. |
| 13 | +--- |
| 14 | +Explain how to use the OSGi Service |
| 15 | +[Refer1](https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/different-between-osgi-services-and-osgi-component/m-p/621761)<br> |
| 16 | +[Refer2](https://www.vogella.com/tutorials/OSGi/article.html#osgi-services)<br> |
| 17 | +[Refer3](https://vogella.com/blog/getting-started-with-osgi-declarative-services-2024/)<br> |
| 18 | +## 1. Introduction |
| 19 | +- **OSGi Services** are essentially Java objects that provide a specific functionality or interface, and other components,plugins can dynamically discover and use these services. |
| 20 | +- Multiple plugins can provide a service implementation for the service interface. Plugins can access the service implementation via the service interface. |
| 21 | +- **E.g.** When you want to create modular that can be added or removed at runtime, you can use the OSGi service. |
| 22 | + |
| 23 | +## 2. Using OSGi |
| 24 | +- There are several ways of defining, providing and consuming service. |
| 25 | +- Let's start with create the API project to provide the external APIs for other components. |
| 26 | + |
| 27 | +## 2.1. Create API project |
| 28 | + |
| 29 | +## 2.2. Specific the API |
| 30 | +- Create an interface for the service definition. |
| 31 | + |
| 32 | +```java |
| 33 | +package com.test.internal; |
| 34 | + |
| 35 | +/** |
| 36 | + * Service for doing something |
| 37 | + * |
| 38 | + * @noimplement This interface is not intended to be implemented by clients. |
| 39 | + * @noextend This interface is not intended to be extended by clients. |
| 40 | + */ |
| 41 | +public interface IDoingSomethingService { |
| 42 | + public void doSomething(); |
| 43 | +} |
| 44 | + |
| 45 | +``` |
| 46 | +- Create the service implementation that implements service interface. |
| 47 | +```java |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +- Testing |
| 53 | + |
| 54 | +## 3 DS Annotations |
| 55 | +### 3.1. @Component |
| 56 | +- It i to identify the class as a Service Component and is used to generate the Component Description. |
| 57 | +- **Default**: |
| 58 | +> Its name is the full qualified class name |
| 59 | + It registers all of the class’s directly implemented interfaces as services |
| 60 | + The instance will be shared by all bundles |
| 61 | + It is enabled |
| 62 | + It is immediate if it has no services, otherwise it is delayed |
| 63 | + It has an optional configuration policy |
| 64 | + The configuration PID is the full qualified class name |
| 65 | + |
| 66 | +- The defaults can be changed via **annotation type elements**: |
| 67 | + - `service` : The name(s) of the interface or class this component is registered under as a service. Needs to be a full qualified class name. |
| 68 | + - `immediate`: Control whether a component configuration should be immediately activated after becoming satisfied or if the activation should be delayed. Needs to be false in case the factory attribute is set also, needs to be true if no service is provided. |
| 69 | + - ... |
| 70 | + |
| 71 | +### 3.2. @Activate, @Deactivate, @Modified |
| 72 | +- Used to specify methods that should be called when a life cycle event happens. |
| 73 | + - `@Activate`: The method that should be called on component activation. |
| 74 | + - `@Modified`: The method that should be called if a configuration is updated using the ConfigurationAdmin. |
| 75 | + - `@Deactivate`: The method that should be called on component deactivation. |
| 76 | + |
| 77 | +- **e.g.** |
| 78 | +```java |
| 79 | +@Activate |
| 80 | +private void activate() { |
| 81 | + |
| 82 | + //do some initialization stuff |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### 3.3. @Reference |
| 87 | +- Used to specify the dependency on other services. |
| 88 | +- When the annotation is applied to a **method**, the method is the bind method of the reference. |
| 89 | +- When the annotation is applied to a **field**, the field will contain the bound service(s) of the reference. |
| 90 | + |
| 91 | +- Field Injection |
| 92 | +```java |
| 93 | +@Component |
| 94 | +public class MyComponent { |
| 95 | + |
| 96 | + @Reference |
| 97 | + private MyService myService; |
| 98 | + |
| 99 | + public void doSomething() { |
| 100 | + myService.execute(); |
| 101 | + } |
| 102 | +} |
| 103 | +✅ Khi MyService có trong OSGi Service Registry, nó sẽ tự động được inject vào myService. |
| 104 | +❌ Nếu không có MyService, myService sẽ là null. |
| 105 | +``` |
| 106 | + |
| 107 | +- Method Injection |
| 108 | +```java |
| 109 | +@Component |
| 110 | +public class MyComponent { |
| 111 | + private MyService myService; |
| 112 | + |
| 113 | + @Reference |
| 114 | + protected void bindMyService(MyService myService) { |
| 115 | + this.myService = myService; |
| 116 | + } |
| 117 | + |
| 118 | + protected void unbindMyService(MyService myService) { |
| 119 | + if (this.myService == myService) { |
| 120 | + this.myService = null; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +✅ Phương thức bind (bindMyService): Được gọi khi MyService có sẵn trong Service Registry. |
| 125 | +✅ Phương thức unbind (unbindMyService): Được gọi khi service bị gỡ khỏi Registry. |
| 126 | +❗ Đây là cách tốt để xử lý service lifecycle (khi service bị remove). |
| 127 | +``` |
0 commit comments