OSGi ile tam denk olmayan ancak biraz benzer özellikler veren ServiceLoader yazısına bakabilirsiniz.
OSGi Nedir?
Açıklaması şöyle. OSGi bir container üzerinde çalışır.
The OSGi framework is a standardized module system and service platform for the Java programming language. The OSGi standards has both open-source and commercial implementations. Below are some popular open source implementations of OSGi.- Apache Felix- Eclipse EquinoxAny OSGi application runs on any of above OSGi container and application will be developed as a Module/Plugins.
Maven
Şu satırı dahil ederiz
<dependency><groupId>org.osgi</groupId><artifactId>org.osgi.core</artifactId><version>${osgi.version}</version><scope>provided</scope></dependency>
Container için org.apache.felix plugin dahil edilir.
BundleActivator Sınıfı
Plugin'leri register etmek içindir.
Örnek
Şöyle yaparız
BundleContext Sınıfıimport org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator {public static BundleContext bundleContext;@Overridepublic void start(BundleContext context) throws Exception {bundleContext = context;registerServices();}@Overridepublic void stop(BundleContext context) throws Exception {bundleContext = null;}private void registerServices() {CalculatorService service = new SubService();bundleContext.registerService(CalculatorService.class.getName(), service,
new Hashtable<String, Object>());}}
Eğer SpringBoot ile kullanıyorsak ConfigurableApplicationContext ile aynı şeydir.
Örnek
Şöyle yaparız
import org.osgi.framework.BundleContext;import com.finalhints.osgi.Activator;import com.finalhints.osgi.api.CalculatorService;@RestController@RequestMapping("/calculator")public class CalculatorController {@GetMappingpublic List<String> getAllOperations() {BundleContext bundleContext = ...;try {Collection<ServiceReference<CalculatorService>> references = bundleContext.getServiceReferences(CalculatorService.class, null);for (ServiceReference<CalculatorService> reference : references) {CalculatorService calcService = bundleContext.getService(reference);}} catch (Exception e) {...}...}}
Hiç yorum yok:
Yorum Gönder