开发者

writing hessian service

开发者 https://www.devze.com 2023-02-06 09:29 出处:网络
I am new to Spring and Hessian and never used them before. I want to write a small Hello World Program which clearly shows how this service works.

I am new to Spring and Hessian and never used them before.

I want to write a small Hello World Program which clearly shows how this service works.

I am using Maven for list project details and dep开发者_StackOverflowendencies.

The resources for hessian available online are not complete step-by-step guide.

would appreciate if I get help form someone who has worked writing hessian services


The steps for implementing a Hessian-callable service are:

  • Create a Java interface defining methods to be called by clients.
  • Write a Java class implementing this interface.
  • Configure a servlet to handle HTTP Hessian service requests.
  • Configure a HessianServiceExporter to handle Hessian service requests from the servlet by delegating service calls to the Java class implementing this interface.

Let's go through an example. Create a Java interface:

public interface EchoService {
    String echoString(String value);
}

Write a Java class implementing this interface:

public class EchoServiceImpl implements EchoService {
    public String echoString(String value) {
        return value;
    }
}

In the web.xml file, configure a servlet:

<servlet>
  <servlet-name>/EchoService</servlet-name>
  <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>  
</servlet>

<servlet-mapping>
  <servlet-name>/EchoService</servlet-name>
  <url-pattern>/remoting/EchoService</url-pattern>
</servlet-mapping>

Configure an instance of the service class in the Spring application context:

<bean id="echoService" class="com.example.echo.EchoServiceImpl"/>

Configure the exporter in the Spring application context. The bean name must match the servlet name.

<bean
    name="/EchoService"
    class="org.springframework.remoting.caucho.HessianServiceExporter">
  <property name="service" ref="echoService"/>
  <property name="serviceInterface" value="com.example.echo.EchoService"/>
</bean>


The client has to create a proxy of the remote interface. You could simply write a JUnit-Test:

HessianProxyFactory proxyFactory = new HessianProxyFactory();
        proxyFactory.setHessian2Reply(false);
        proxyFactory.setHessian2Request(false);
        com.example.echo.EchoService service = proxyFactory.create(
                com.example.echo.EchoService, "http://localhost:8080/<optional-context/>remoting/EchoService");

Assert.equals(service.echoString("test"), "test");
0

精彩评论

暂无评论...
验证码 换一张
取 消