Writing a client to access OSGi service methods

In the first part of this howto, I explained how to build an OSGi declarative service with Maven using Netbeans. However, this post concludes the project with a client to access the OSGi service methods created in part 1.

 

Refreshing the memory

 

In the first part of this project, we performed the following task;

  1. We deployed the apache Felix SCR plugin
  2. In Netbeans we started a new Maven project by;
    • Writing a service interface
    • Writing the interface implementation
    • Creating an XML document
    • Creating a MANIFEST.MF
    • Modifying the pom.xml file created by Netbeans
    • and finally we clean and build the project

So, taking off from where we stopped

 

Get the jar file

 

After successful building of the project, we will be making use of the Maven generated jar file. In this case, MyComputeService-1.0-SNAPSHOT.jar. You will commonly find this file in dist directory of your project if using Ant. But since we are concerned with Maven in this particular project, you will find it in the target directory of the MyComputeService project.

 

In Netbeans

 

 

Start a new Maven Web Application

 

In Netbeans, start a new Maven web application and name it MyComputeServiceClient. Don't forget, this new application is meant to make use of the service methods.

New Maven Web application ScreenshotMaven Web application Project name screenshot

Note: Make sure the target server is Glassfish v3 on JavaEE 6

 

Write the Servlet

 

Create a servlet ComputeServlet.java and the copy the following code into it

package client;

import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mycompany.mycomputeservice.ComputeService;

/**
 *
 * @author Kayode Odeyemi
 */

@WebServlet(loadOnStartup=1, name="ComputeServlet", urlPatterns={"/ComputeServlet"})
public class ComputeServlet extends HttpServlet {

    @Resource(mappedName="decl-compservice")
    ComputeService cservice;
   
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here*/
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ComputeServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>The addition arithmetic is: " + cservice.add(10, 6) + "</h1>");
            out.println("<h1>The multiplication arithmetic is: " + cservice.multiply(10, 6) +

"</h1>");
            out.println("</body>");
            out.println("</html>");
           
        } finally {
            out.close();
        }
    }
}

 

Load the dependencies

 

To fix the errors in the ComputeServlet.java, you need to modify the pom.xml file. There are 2 ways to achieve this.

  1. Add the dependencies using Netbeans
    • right-click on the Libraries folder
    • click on "Add Dependency..."Netbeans Add dependency screenshot
  2. Modify the pom.xml file by adding the following code within the <dependencies></dependencies>

<dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>MyComputeService</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>jar</type>
        </dependency>

By doing either of the following, you have added the OSGi declarative service application into the Web application. Now, you have access to the service methods right inside this web application.

 

Note:

  • To confirm that the dependencies has been added successfully, Open the ComputeServlet once again in Netbeans and you will notice that all errors are gone. For benefit of doubt, you can right-click within the ComputeServlet.java file and click on "Fix Imports".
  • Also not the @Resource annotation which is used to inject the required resource identified by the mappedName into the servlet.
  • The @WebServlet annotation also gives us the opportunity of making the infamous web.xml optional

 

 

Clean and build, run

 

Clean and build the application. Start or restart Glassfish then run the application to deploy the application to the server. Access the application at http://<yourserver>:8080/MyComputeServiceClient/ComputeServlet.

 This concludes the Series "Building an OSGi declarative service with Maven using Netbeans". Have any questions, please use the comment form.

Writing a client to access OSGi service methods

Hi Kayode, Thanks very much for publishing the howto. Both parts are easy to follow and easy to write. How would you approach the problem that I have with running it? The deployment failed with a rather long stack trace: SEVERE: WebModule[/MyComputeServiceClient]PWC1396: Servlet /MyComputeServiceClient threw load() exception com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class client.ComputeServlet at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:312) ... Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: client.ComputeServlet/cservice@Field-Injectable Resource. Class name = client.ComputeServlet Field name=cservice@java.lang.String@@@ into class client.ComputeServlet ... Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/client.ComputeServlet/cservice' in SerialContext [Root exception is javax.naming.NamingException: Lookup failed for 'decl-compservice' in SerialContext [Root exception is javax.naming.NameNotFoundException: decl-compservice not found]] ... More of the same. Regards, Bernard

Post new comment

The content of this field is kept private and will not be shown publicly.