public interface HelloService {
String sayHello();
}
public class HelloServiceImpl implements HelloService {
public String sayHello() {
return "Hello, OSGi!";
}
}
public void activate(BundleContext context) {
HelloService helloService = new HelloServiceImpl();
context.registerService(HelloService.class.getName(), helloService, null);
}
private ServiceTracker<HelloService, HelloService> serviceTracker;
public void activate(BundleContext context) {
serviceTracker = new ServiceTracker<>(context, HelloService.class, null);
serviceTracker.open();
}
public void useHelloService() {
HelloService helloService = serviceTracker.getService();
if (helloService != null) {
String greeting = helloService.sayHello();
System.out.println(greeting);
} else {
System.out.println("HelloService is not available.");
}
}