Platinum Partner
java,osgi,hessian

Hessian Service and Client on an OSGi Container : Part 2

In my last post, I introduced hessian extender. Now, I will show  how to create service that will be registered by the last extender, and a Java application that will communicate with it.

First, we create a simple service interface witch is inspired here :

package com.jtunisie.osgi.hessian;  
 
import com.jtunisie.osgi.hessian.exceptions.ServiceException;  
 
public interface IService {  
 public String execute() throws ServiceException;  
}  

Next, we create a service implementation that will extend HessianServlet and implement and old interface:

package com.jtunisie.osgi.hessian.impl;

import com.caucho.hessian.server.HessianServlet;
import com.jtunisie.osgi.hessian.IService;

import com.jtunisie.osgi.hessian.exceptions.ServiceException;


public class Service extends HessianServlet implements IService {

public void publish() {
this.setAPIClass(IService.class);
}

@Override
public String execute() throws ServiceException {
return "jTunisie";
}
}
 

We use Spring DM to register this service as com.caucho.hessian.server.HessianServlet name and we note that publish method will be called as default init method after all spring IoC
service reference injection.
 

 

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

<bean id="serviceDebug" class="com.jtunisie.osgi.hessian.impl.Service"/>

<osgi:service interface="com.caucho.hessian.server.HessianServlet" ref="serviceDebug">
<osgi:service-properties>
<entry key="url" value="/jtunisie"/>
</osgi:service-properties>
</osgi:service>
</beans>

 

 

Service will be registred under /jtunisie url.


At the client side, we need only the api bundle and hessian library :

 

package com.jtunisie.osgi.debug.test;

import com.caucho.hessian.client.HessianProxyFactory;
import com.jtunisie.osgi.hessian.IService;
import com.jtunisie.osgi.hessian.exceptions.ServiceException;
import java.net.MalformedURLException;

public class Main {

public static void main(String[] args) throws MalformedURLException, ServiceException {
HessianProxyFactory factory = new HessianProxyFactory();
IService service = (IService) factory.create(IService.class, "http://localhost:8080/jtunisie");
System.out.println(service.execute());
}

At the end, I want to thank Roman who let me share his work and my team that helped me to have and accomplish this work.
The full source code is available here as 2 maven projects:
svn checkout http://hessianextender.googlecode.com/svn/trunk/ hessianextender-read-only

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}