Index Changes

Deploy JAXRS-POJO in Fuji

What does the example do?

This example illustrates how a JAXRS-POJO deployed in the Fuji Framwork should work. The sequence flow is as follows:

  1. user uses a web browser to access a Http URL exposed by a JAXRS-POJO
  2. inside the JAXRS-POJO's GET method, it uses the Fuji Simple Service API to lookup a service provider registered on the NMR by endpoint name
  3. JAXRS-POJO invokes the service provider
  4. service provide returns response to JAXRS-POJO
  5. JAXRS-POJO receives the response, and then return response to web browser.

Running the example

  1. install and start Fuji Runtime milestone 4 or later
  2. install and start restbc-installer-2.5-SNAPSHOT.jar(info) bundle: this is the Rest Binding Component
  3. install and start jaxrs_app.jar(info) bundle: this is the Service Assembly containing the JAXRS-POJO
  4. install and start myprovider.jar(info) bundle: this is a Java class implementing the Fuji Simple Service Provider API
  5. point your browser to http://localhost:9696/jaxrsapp/hello: this is the URL exposed by the JAXRS-POJO
  6. if everything is successful, then you should see in the browser "response from test.MyProvider"

Service Unit for JAXRS-POJO

In addition to jbi.xml and Manifest.mf, the Service Unit for JAXRS-POJO contains:

  • jaxrs-pojo.properties
  • jaxrs/HelloResource.class

jaxrs-pojo.properties

Configuration for JAXRS-POJO

# optional property, if not exist, defaults to default-listener
listener-name=default-listener


# required property, all JAXRS-POJOs in this service-unit will be accessible under this context-root
context-root=/jaxrsapp

  • listener-name: this tell the RestBC which listener do you want to expose the JAXRS-POJOs in this ServiceUnit to. Option property, defaults to "default-listener", which binds to port "9696".
  • context-root: this specifies the context-root for all the JAXRS-POJOs in this Service Unit, this is equivalent to a servlet-context. JAXRS-POJOs URLs will be under this root. Required property.

jaxrs/HelloResource.class

This is the JAXRS-POJO class. It looks similar to this:

package jaxrs;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.glassfish.openesb.api.service.ServiceConsumer;
import org.glassfish.openesb.api.service.ServiceMessage;
import org.glassfish.openesb.api.service.ServiceProps;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * HelloResource.java
 *
 * @author Edward Chou
 */
@Path("/hello")
public class HelloResource {

    private static final Logger logger = Logger.getLogger(HelloResource.class.getName());
    
    @Context
    BundleContext bundleContext;
    
    @GET
    public String get() {
        
        String serviceFilter = "(" + ServiceProps.SERVICE_NAME + "=myService)";
        
        try {
            ServiceReference[] refs = bundleContext.getServiceReferences(ServiceConsumer.class.getName(), serviceFilter);
            
            if (refs.length > 0 && refs[0] != null) {
                ServiceReference serviceRef = refs[0];
                logger.log(Level.INFO, "looked up service reference: " + serviceRef);
                
                ServiceConsumer consumer = (ServiceConsumer) bundleContext.getService(serviceRef);
                
                ServiceMessage message = consumer.getServiceMessageFactory().createMessage();
                message.setPayload("request from consumer");
                
                ServiceMessage replyMessage = consumer.invoke(message);
                
                String reply = (String) replyMessage.getPayload();
                logger.log(Level.INFO, "got reply from provider: " + reply);
                
                return reply;
            }
            
        } catch (Exception e) {
            logger.log(Level.SEVERE, "exception when invoking ServiceProvider", e);
        }
        
        
        return "failed to invoke ServiceProvider";
    }

}


Pretty much everything is standard for JAXRS, except for the injected "@Context BundleContext bundleContext". This is the OSGI's BundleContext, and it is injected by RestBC upon deployment. This give the JAXRS-POJO access to the OSGI environment, which allows it to lookup the Fuji Simple Service API. The rest of the codes just sends a request message to the service provider "myService".

JSPWiki v2.4.100
[RSS]
« Home Index Changes Prefs
This page (revision-2) was last changed on 06-Apr-09 21:40 PM, -0700 by Edward Chou