OSGi 서비스

다음을 포함한 OSGi 서비스 개발의 기본 사항에 대해 알아봅니다.

  • Java POJO를 OSGi 서비스로 변환하는 방법
  • OSGi 서비스를 Java 인터페이스에 바인딩하는 방법

리소스

코드

Activities.java

/core/src/main/java/com/adobe/aem/wknd/examples/core/adventures/Activities.java

package com.adobe.aem.wknd.examples.core.adventures;

import org.osgi.annotation.versioning.ProviderType;

@ProviderType
public interface Activities {
    String getRandomActivity();
}

ActivitiesImpl.java

/core/src/main/java/com/adobe/aem/wknd/examples/core/adventures/impl/ActivitiesImpl.java

package com.adobe.aem.wknd.examples.core.adventures.impl;

import java.util.Random;

import com.adobe.aem.wknd.examples.core.adventures.Activities;

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

@Component(
    service = { Activities.class }
)
public class ActivitiesImpl implements Activities {

    private static final String[] ACTIVITIES = new String[] {
        "Camping", "Skiing",  "Skateboarding"
    };

    //private final int randomIndex = new Random().nextInt(ACTIVITIES.length);
    private final Random random = new Random();

    /**
     * @return the name of a random WKND adventure activity
     */
    public String getRandomActivity() {
        int randomIndex = random.nextInt(ACTIVITIES.length);
        return ACTIVITIES[randomIndex];
    }
}

package-info.java

/core/src/main/java/com/adobe/aem/wknd/examples/core/adventures/package-info.java

@Version("1.0")
package com.adobe.aem.wknd.examples.core.adventures;

import org.osgi.annotation.versioning.Version;
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69