hessian实战1

Posted 甜菜波波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hessian实战1相关的知识,希望对你有一定的参考价值。

服务端:

1.新建MAVEN HessianServer 项目

2.新建接口 Basic

public interface Basic {
    String hello(String name);

    String say(String msg);

    int sum(int a,int b);
}

3.新建 BasicService 类,extends HessianServlet implements Basic

 

技术分享图片
import com.caucho.hessian.server.HessianServlet;
public class BasicService extends HessianServlet implements Basic {

    public String hello(String name) {
        return "Hello " + name;
    }


    public int sum(int a, int b) {
        return a + b;
    }


    public String say(String msg) {
        return "Say:" + msg;
    }
}
View Code

 

4.web.xml 文件

技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>BasicService</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>



</web-app>
View Code

5.POM.XML

技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>HessianWeb</groupId>
    <artifactId>HessianWeb</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
        <groupId>com.caucho</groupId>
        <artifactId>hessian</artifactId>
        <version>4.0.51</version>
    </dependency>

    </dependencies>

</project>
View Code

 

启动项目,在浏览器打开 http://localhost:8091/hello  显示 Hessian Requires POST ,说明服务已启动

客服端(不使用spring):

1.新建HessianClient maven 项目

2.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>HessianClient</groupId>
    <artifactId>HessianClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.51</version>
        </dependency>


    </dependencies>


</project>

3.新建接口Client

技术分享图片
public interface Client {
    String hello(String name);

    String say(String msg);

    int sum(int a,int b);
}
View Code

4.新建Test类

技术分享图片
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.server.HessianServlet;

import java.net.MalformedURLException;

public class Test {

    public static void main(String[] args) throws MalformedURLException {
        //TODO 根据实际地址修改
        String url = "http://localhost:8091/hello";
        HessianProxyFactory factory = new HessianProxyFactory();
        Client basic = (Client) factory.create(Client.class, url);
        System.out.println(basic.hello("aaa"));

    }
}
View Code

5.运行该项目,显示 aaa

客户端(使用spring)

1.pom.xml 改为

技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>HessianClient</groupId>
    <artifactId>HessianClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.51</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>


</project>
View Code

2.新建remote-client.xml

技术分享图片
<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">




    <!-- 客户端Hessian代理工厂Bean -->
    <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 请求代理Servlet路径 -->
        <property name="serviceUrl">
            <value>http://localhost:8091/hello</value>
        </property>
        <!-- 接口定义 -->
        <property name="serviceInterface">
            <value>Client</value>
        </property>
    </bean>
</beans>
View Code

3.新建Test2类

技术分享图片
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {

    public static void main(String[] args) {
        ApplicationContext contex = new ClassPathXmlApplicationContext(
                "remote-client.xml");

        // 获得客户端的Hessian代理工厂bean
        Client i = (Client) contex.getBean("clientSpring");
        System.out.println(i.hello("chen1231231231"));
    }
}
View Code

 

运行该项目显示 Hello chen1231231231

 

以上是关于hessian实战1的主要内容,如果未能解决你的问题,请参考以下文章

Hessian的使用

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

使用python求海森Hessian矩阵

Spring实战笔记:Spring集成

血管检测基于matlab mom方法结合Hessian和曲线拟合血管直径测量含Matlab源码 1970期