OSGi Microservice using wso2 msf4j

Following is a sample OSGi component.

@Component(name = “coffeeShopService”,
 immediate = true)
public class CoffeeShopService {
@Activate
 protected void activate(BundleContext bundleContext) {
 }
@Deactivate
 protected void deactivate() {
 }
}

You can define your java class as an OSGi component by adding @Component on top of the class. And, you have to implement @Activate and @Deactivate methods, which will get triggered at the begging and end of available time period of the OSGi service.

To make your OSGi component a microservice, you can implement org.wso2.msf4j.Microservice, and define your reference path and services.

@Component(name = “coffeeShopService”, service = {Microservice.class},
 immediate = true)
@Path(“/coffeeshop”)
public class CoffeeShopService implements Microservice {
@GET
 @Path(“/order”)
 public void makeOrder() {
 log.info(“Make an order”);
 }
}

Finally, you have to define your OSGi microservice in your maven pom.xml file.

<Carbon-Component>
 osgi.service;objectClass=”<package-name>.CoffeeShopService”,
 osgi.service;objectClass=”org.wso2.msf4j.Microservice”
 </Carbon-Component>

You can find a sample code from here.
 ( https://github.com/prabushi/carbon-uuf/tree/session-from-client/samples/microservices/org.wso2.carbon.uuf.sample.hello )

Like what you read? Give prabushi samarakoon a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.