Over a million developers have joined DZone.

Hessian Service and Client on an OSGi Container : Part 2

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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

 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}