spring and hessian Integration - HessianServiceExporter & BurlapServiceExporter

sponsored links

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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of spring and hessian Integration - HessianServiceExporter & BurlapServiceExporter

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries