How to build service and how to active that in CQ5

By : devsda
Source: Stackoverflow.com
Question!

I am learning OSGI, CQ5 these days. I am trying to build a bundle that have service (My first code). I successfully build a bundle and upload that bundle on CQ5, and install that also.

But component shows registered only, not active. Why ?

I also want to activate this service. How can I do this ? Someone on net said to make jsp. I also do that, but didn't get any response. Help me from this problem. I spent lot of time on this, lot of searching, but I didn't get any solution.

How can I use my service in CQ5, CRXDE(Adobe).

JSP code are :-

<% var service =   sling.getService(Packages.mh.osgitest.SayHello); %> 
<%= service.sayHelloTest() %>

Above code is not working.

The snapshots of these are

Bundle enter image description here

component enter image description here

service enter image description here

My codes are as :-

SayHello

package service.expose;

import org.apache.felix.scr.annotations.Service;

public interface SayHello {
    public void sayHelloTest();
}

SayHelloTestServlet // Servlet have no sense here.

package service.expose;

import java.io.IOException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;


@Component
@Service(value = SayHello.class)
public class SayHelloTestServlet implements SayHello {

    public void sayHelloTest() {
        System.out.println("Testing Say Hello");
    }

    @Activate
    protected void activate() {
        System.out.println("service started");
    }


    @Deactivate
    protected void deactivate() {
        System.out.println("service stopped");
    }
}
By : devsda


Answers
Use @Component(immediate=true) to instantiate your service as soon as the bundle is activated as opposed to on-demand.



This video can help you solving your question :)
By: admin