Thursday Oct 01, 2009

Fuji simple service API and springDM

 What is Fuji simple service api?



The simple service provides the capability to invoke/interact with JAVA code outside of  JBI, Simple Service API provides a bridge between code that is hosted outside of a JBI component and service endpoints available on the NMR. The interface below shows what a user need to do in-order to use simple service api while provisioning an osgi service. for details see Fuji Simple Service API




/\*\*
 \* This interface represents a simple service provisioning contract with the
 \* JBI runtime environment.  The invoke method is called when a message
 \* exchange is addressed to this service instance.  Classes implementing this
 \* interface must be registered in the OSGi service registry in order to be
 \* available to consumers in the JBI runtime environment.  Further, the
 \* service registration must contain a property called "org.glassfish.openesb.serviceName" which
 \* contains the name of the service being offered.  Optionally, a property
 \* called "org.glassfish.openesb.endpointName" can also be specified.
 \* 
 \*/
public interface ServiceProvider { 
    ServiceMessage invoke(ServiceMessage message) throws Exception;
}


This blog shows how to integrate Fuji simple service api with spring/springDM  application, We will create a springDM application which instantiates the above provider Implementation , based on spring-config and spring-osgi-config files . In the diagram below the service provider is part of the spring application and the application context is created by the springDM module




Create a new springDM module, The Spring-OSGi project supplies a maven archetype that will create a SpringDM bundle project , example




 mvn archetype:create   -DarchetypeGroupId=org.springframework.osgi  
-DarchetypeArtifactId=spring-osgi-bundle-archetype 
-DarchetypeVersion=1.1.0  
-DgroupId=org.foo 
-DartifactId=org.foo.springdm.fuji
-Dversion=1.0

The structure of the project will look as below


 spring DM project



Change the pom , add dependency of the Fuji api



 go the src root of the application and add the following dependency to the springDM module pom




<dependency>
   <groupId>open-esb.fuji</groupId>
   <artifactId>api</artifactId>
   <version>1.0-M9-SNAPSHOT</version>
</dependency>



Write the POJO which implements ServiceProvider , note this POJO is part of the spring application, and can access any bean or the spring applicationContext, In the above diagram  org.foo.MyProvider.java , represents the POJO which implements the ServiceProvider Interface. here is a sample code




package org.foo;


import org.glassfish.openesb.api.service.ServiceMessage;
import org.glassfish.openesb.api.service.ServiceMessageFactory;
import org.glassfish.openesb.api.service.ServiceProvider;



public class MyProvider implements  ServiceProvider {
   
    ServiceMessageFactory msgFactory_;
   
    public ServiceMessage invoke(ServiceMessage message) throws Exception {
        // Print out the content of the message
        String payload = (String)message.getPayload();
        System.out.println(payload);
       
        // One way exchange, so there's no need to return anything
        return null;
    }


    public void setMessageFactory(ServiceMessageFactory factory) {
        msgFactory_ = factory;
    }
   
    public void setAnotherBean (AnotherBeanI bean){
     //make use the bean
    }


}  


Define the spring beans in the spring config file bundle-context.xml 




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
       <bean name="anotherBean" class="org.foo.AnotherBeanImpl" />
       <bean name="fujiService" class="org.foo.MyProvider">
         <property name="anotherBean" ref="anotherBean"/>
       </bean>
      
</beans> 


Let us define the beans to be exported as OSGI service, Once the bean MyProvider is exported as osgi service,  any JBI component will be able to access this bean by sending a normalized Message see simple service api for more details 




<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/osgi"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> 


 <service id="fujiServiceOsgi" ref="fujiService"
  interface="org.glassfish.openesb.api.service.ServiceProvider">


  <service-properties>
   <beans:entry key="org.glassfish.openesb.serviceName" value="abcService" />
   <beans:entry key="org.glassfish.openesb.endpointName" value="endpoint1" />
  </service-properties>
 </service>


</beans:beans>




Based on the osgi service with the ServiceProvider Interface, an jbi  internal interpoint is activated, The activation of the endpoint is based on the osgi service properties as specified in the bean service definition shown above



  • org.glassfish.openesb.serviceName is used for the service name of the activated endpoint

  • org.glassfish.openesb.endpointName is used for the endpoint name of the activated endpoint


Install the springDM bundles into Fuji-Felix



In order to use springDM application one has to install all the required spring DM osgi bundles, see the set of osgi bundles which needs to installed and started, more details



Build and deploy the application




Go to the project folder and execute
  mvn clean build
In the felix console type install and start the springDM bundle just created
  install file:/...path_to_springDM_bundle..........org.foo.springdm.fuji-1.0.jar 


Invoking JBI-Endpoint from a spring application 



This would require a springDM application to import an osgi service registered by the Fuji/jbi runtime. Note the current Fuji implementation while activating an jbi endpoint , register an osgi service based on the endpoint properties, The endpoint properties are once again based on service name and the endpoint name, Assuming and endpoint had been activated by a JBI component with service-name = xyzService and endpoint-name=xyzEndpoint , how such a endpoint can be accessed from an spring application. In the diagram below the service consumer is part of the spring application .



 


 1. Import the osgi services, using the spring-osgi-config file, note the filter




<reference id="jbiService"
  interface="org.glassfish.openesb.api.service.ServiceConsumer"
  filter="(org.glassfish.openesb.serviceName=xyzService)"  />


2. Use dependency injection to set the service reference into a bean




<bean id="someBean" class="org.foo.BeanWithJbiServiceReference" lazy-init="false">
  <property name="serviceReference" ref="jbiService" />
  <property name="service" ref="jbiService" />
</bean>


3. Define the bean and Invoke




package org.foo;
import org.glassfish.openesb.api.service.ServiceConsumer;
import org.glassfish.openesb.api.service.ServiceMessage;
import org.osgi.framework.ServiceReference;


public class BeanWithJbiServiceReference {
  private ServiceReference serviceReference;
  private ServiceConsumer service
  public void setServiceReference(ServiceReference serviceReference) {
   this.serviceReference = serviceReference;
  } 
  public void setService(ServiceConsumer service) {
   this.service = service;
  } 
  public void invokeJBIService(Object payload) throws Exception{
   ServiceMessage message = service.getServiceMessageFactory().createMessage();
   message.setPayload(payload);  
   service.invoke(message);
  } 
}
 



Future Direction Idea is to provide capabilty which will let any spring bean or osgi service to be accessed seamlessly, based on service properties or configuration, Also Fuji going forward will provide native support for spring beans . Read On   

About

sujit

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today