Over a million developers have joined DZone.

Hessian Service and Client on an OSGi Container : Part 2

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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() {

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"
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">
<entry key="url" value="/jtunisie"/>



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");

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


The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}