OSGi bundle doesn't start when using declarative service annotations

41 views Asked by At

I'm working with OSGi and Equinox for the first time and I'm trying to build a simple project. It has two bundles:

  1. A service bundle with a simple MyService interface and an implementing MyServiceImpl class.
  2. A consumer bundle with consisting only a MyConsumer class.

The service interface looks like this:

package test.service.osgi.service;

public interface MyService {
    
    public String getMessage();
}

The implementing class like this:

package test.service.osgi.impl;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

import test.service.osgi.service.MyService;

@Component (immediate = true, service = MyService.class)
public class MyServiceImpl implements MyService {
    
    @Activate
    public void activate() {
        System.out.println("MyService is active!");
    }
    
    @Override
    public String getMessage() {
        
        return "Hello world!";
    }
    
    @Deactivate
    public void deactivate() {
        System.out.println("MyService is no longer active.");
    }
}

The consumer Class of the other bundle looks like this:

package org.osgi.service.consumer;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import test.service.osgi.service.MyService;

@Component(immediate = true, service = MyConsumer.class)
public class MyConsumer {

    @Reference
    MyService myService;
    
    @Activate
    public void activate() {
        System.out.println("Activate executed");
        printMessage();
    }
    
    public void printMessage() {
        if(myService != null ) {
            System.out.println(myService.getMessage());
        } else {
            System.out.println("MyService Reference is null!!!");
        }
    }
}

I configured both MANIFEST.MF and the Run configurations according to this tutorial: https://www.datainmotion.de/osgi-in-a-nutshell-launch-a-simple-service/

But when I run the project there is no output on the console. it only says "osgi>". I can use the commands like "ss" and get "Framework is launched" with the list of the bundles, which are all in ACTIVE state. However, none of the strings from the printing commands appear.

The same applies to when I stop and restart the consumer bundle via console command.

Does anyone know what the problem is?

I've been trying to get this running for hours but whatever I do, nothing changes.

0

There are 0 answers