spring and hessian Integration - HessianServiceExporter & BurlapServiceExporter

Introduction:

Hessian recently started studying a bit of rmi program, because the client is a variety of mobile terminal platforms, originally wanted to use the legendary phprpc (hprose?), But read the following official documents, found no object-c version, Guer give up.

The official follow hessian example walking, running up and there is no problem, but I try to integrate with spring, when, out of a lot of problems. Record what, convenience, and passers-by, huh, huh.

Environmental Description:

  • spring version 3.0 RELEASE.
  • Hessian version 4.0.7 WHATEVER.
  • JEE version 2.5

Implementation steps:

  1. Configure web.xml, add spring-web in the definition of DispatcherServlet. As follows:
    <servlet>
            <servlet-name>test-remote</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/config/test-remote.xml</param-value>
            </init-param>
            <load-on-startup>4</load-on-startup>
    </servlet>
    
    <servlet-mapping>
            <servlet-name>test-remote</servlet-name>
            <url-pattern>/remote/*</url-pattern>
    </servlet-mapping>
    

    Tips:
    A. Many users have said must be placed in the WEB-INF with the following servlet-name corresponding to the test-remote-servlet.xml configuration file, in fact need not be so treated. If you specify contextConfigLocation, then it can solve the problem. Means that spring is not mandatory for your configuration file.
    B. DispatcherServlet is more than can be configured, and I have here a test-local, mapping of "/" path between them does not affect. Of course you can simply configure the number of servlet-mapping. I just want to test, the result indicates that it indeed is possible.

  2. Space Transport object class definition:
    package test.remote.dto;
    
    import java.io.Serializable;
    import java.util.Date;
    
    /**
     * Space data transfer object.
     * @author gogo
     *
     */
    public class Space implements Serializable{
    
      /**
         * auto generated serialVersionUid.
         */
        private static final long serialVersionUID = 5632812127919571530L;  
    
        /** public space name for people in the space.*/
            private String publicName;
            private String privateName;
            private boolean isActive;
            private Date lastUpdatedTime;
    
            /**
             *  setter & getter
             */
    
            @Override
            public String toString() {
                StringBuilder buffer = new StringBuilder("Space:");
                buffer.append("public name:").append(publicName).append(", ");
                buffer.append("private name:").append(privateName).append(", ");
                buffer.append("is actived:").append(isActive).append(", ");
                buffer.append("lastUpdatedTime:").append(lastUpdatedTime.getTime());
                return buffer.toString();
            }
    }
    
  3. Service interface is defined as follows:
    package test.remote.service;
    import java.util.List;
    
    import test.remote.dto.Space;
    
    /**
     *
     * @author gogo
     *
     */
    public interface SpaceService {
    
            List showSpaceNames(); // Returns the name of the collection  
    
            Space findSpace(String id);// Find the space name  
    
            boolean updateSpaceName(String name); // Update the space name  
    
    }
    
  4. Service interface class:
    package test.remote.service.impl;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.remoting.caucho.HessianServiceExporter;
    
    import test.remote.dto.Space;
    import test.remote.service.SpaceService;
    
    /**
     *  This is a remote interface testing  <br>
     *  This interface would be developed by the spring of   {@link HessianServiceExporter} HTTP services exposed  .<br>
     *  The client will rely on the Hessian implementation
     * @see HessianServiceExporter
     * @author gogo
     *
     */
    public class SpaceServiceImpl implements SpaceService{
    
            private static final Log log = LogFactory.getLog(SpaceServiceImpl.class);
    
            private Space createSpace() {
                    Space space = new Space();
                    space.setPublicName("roosher-space");
                    space.setPrivateName("my-private-room");
                    space.setActive(true);
                    space.setLastUpdatedTime(new Date());
                    return space;
            }
    
            public Space findSpace(String id) {
                    // TODO Auto-generated method stub
                    log.debug("find space id is :" + id);
                    return createSpace();
            }
    
            public List showSpaceNames() {
                    // TODO Auto-generated method stub
                    List names = new ArrayList<String>();
                    for (int i = 0; i < 10; i++) {
                            names.add("Hello space "+ i);
                    }
                    return names;
            }
    
            public boolean updateSpaceName(String name) {
                    // TODO Auto-generated method stub
                    log.debug("updated space name : " + name );
                    return true;
            }
    
    }
    
  5. test-remote.xml content:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:p="http://www.springframework.org/schema/p"
            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-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
            <bean 
    
                             p:order="1"></bean>
    
            <bean>
            </bean>
    
            <bean>
            </bean>
    
            <bean name="/SpaceHessian" 
    
                    p:serviceInterface="test.remote.service.SpaceService"
                    p:service-ref="spaceService">
            </bean>
    
            <bean name="/SpaceBurlap"
    
                 p:serviceInterface="test.remote.service.SpaceService"
                 p:service-ref="spaceService">
            </bean>
    
    </beans>
    

    Tips:
    A. BeanNameUrlHandlerMapping attribute p: order = "1", you can not configure. To avoid conflict with your own.
    B. HttpRequestHandlerAdapter which is critical in this, not that it may be this error:
    - No adapter for handler Does your handler implement a supported interface like Controller? HttpRequestHandlerAdapter its role is to make spring-mvc release privileges to the following Exporter their processes across the entire HTTP. After all, is to achieve HttpRequestHandler HessianServiceExporter

  6. Two clients achieve:
    A. is based on the realization of spring, with org.springframework.remoting.caucho.HessianProxyFactoryBean implementation, the configuration is as follows:
    <bean
                   >
            <property name="serviceUrl"
                      value="http://localhost:8080/test/remote/SpaceHessian" />
            <property name="serviceInterface" value="test.remote.service.SpaceService" />
    </bean>
    
    <bean
           >
            <property name="serviceUrl"
                      value="http://localhost:8080/test/remote/SpaceBurlap" />
            <property name="serviceInterface" value="test.remote.service.SpaceService" />
    </bean>
    

    note inside the URL yet? Which http://localhost:8080/test/remote/SpaceHessian of / SpaceHessian corresponds to the pre-configured in the remote-test.xml the <bean name="/SpaceHessian"> </ bean> of the URI.

    Then spring for the context, they can be injected directly into the SpaceService service = context.getBean ("spaceServiceBurlap");

    B. Is based on Hessian provide their own clients, here is the Java version of the show.

  7. package test.remote;
    
    import java.net.MalformedURLException;
    import java.util.List;
    
    import com.caucho.hessian.client.HessianProxyFactory;
    import test.remote.dto.Space;
    import test.remote.service.SpaceService;
    
    /**
     *  Connection to the client when it is necessary to pay attention to.   url IP address  .
     * @see Space
     * @author gogo
     *
     */
    public class SpaceClient {
    
        public static void main(String[] args) {
            String url = "http://192.168.0.9:8080/test/remote/SpaceHessian";
            HessianProxyFactory factory = new HessianProxyFactory();
            try {
                SpaceService spaceService = (SpaceService) factory.create(
                        SpaceService.class, url);
                System.out.println(spaceService.findSpace("testid"));
                System.out.println(spaceService.updateSpaceName("whatever"));
    
                List spaceNames = spaceService.showSpaceNames();
                System.out.println("\r\nGet space names:");
                for (int i = 0; i < spaceNames.size(); i++) {
                    System.out.print(spaceNames.get(i) + ", ");
                }
                System.out.println();
                System.out.println("space names list finished");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
    

    If the background does not have much problem running up and can be directly tested.

  8. Test results:
    Space:public name:roosher-space, private name:my-private-room, is actived:true,
    lastUpdatedTime:1282196778538
    true
    
    Get space names:
    Hello space 0, Hello space 1, Hello space 2, Hello space 3, Hello space 4, Hello space 5
    , Hello space 6, Hello space 7, Hello space 8, Hello space 9,
    space names list finished
    
  9. Cheers! First serious blogging. I hope to be helpful.
分类:Java 时间:2010-08-19 人气:266
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.599 (s). 12 q(s)