1 创建项目
利用Maven创建多模块项目。项目名称为hessian-easy,项目有两个模块,为hessian-easy-api和hessian-easy- server。hessian-easy-api模块为jar工程,存放对外发布的接口;hessan-easy-server模块为war工程,存放业务逻辑的实现。接下来将用 eclipse辅助创建Maven多模块项目。从创建主项目开始。
1.1 创建主项目
1.1.1 创建过程
选择Maven Project。packaging选择pom。
1.2 创建模块hessian-easy-api
1.2.1 创建过程
选择Maven Module。
选择Create a simple project。自己定义项目信息,包括项目坐标。
packaging选择jar。
点击Finish,创建模块完成。
1.2.2 pom.xml
<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> <parent> <groupId>org.mahz</groupId> <artifactId>hessian-easy</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>hessian-easy-api</artifactId> </project>
1.3 创建模块hessian-easy-server
1.3.1 创建过程
此模块为war工程,为了免去创建项目骨架的麻烦,我们选择让eclipse自动生成war工程。因此不选择Create a simple project。
选择maven-archetype-webapp。
点击Finish,创建模块完成。
1.3.2 pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.mahz</groupId> <artifactId>hessian-easy</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>hessian-easy-server</artifactId> <packaging>war</packaging> <name>hessian-easy-server Maven Webapp</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.mahz</groupId> <artifactId>hessian-easy-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>hessian-easy-server</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.12.v20130726</version> </plugin> </plugins> </build> </project>
1.4 创建hessian客户端
此工程为hession客户端,并且是普通的Java工程。我大费周章用Maven构建的目的,是为了方便依赖管理,不需要去官网下载各类开源jar。
1.4.1 创建过程
跟之前一样,我们还是自己手动填写项目基本信息。
填写项目坐标信息。
点击Finish,创建项目完成。
1.4.2 pom.xml
<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>org.mahz</groupId> <artifactId>hessian-easy-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hessian-easy-client</name> <dependencies> <dependency> <groupId>org.mahz</groupId> <artifactId>hessian-easy-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>3.1.3</version> </dependency> </dependencies> </project>
2 创建hessian服务端
2.1 创建服务接口
在hessian-easy-api模块下创建接口,代码如下:
package org.mahz.easyhessian.api; public interface Hello { public String sayHello(String name); public void printHello(String name); }
2.2 实现服务接口
在hession-easy-server模块下实现服务接口,代码如下:
package org.mahz.easyheassin.server; import org.mahz.easyhessian.api.Hello; public class HelloImpl implements Hello { public String sayHello(String name) { return name != null ? "hello " + name : "hello hessian"; } public void printHello(String name) { System.out.println("hello " + name); } }
2.3 配置Servlet
在hession-easy-server模块下的web.xml中配置Servlet。
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>hessian</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>service-class</param-name> <param-value>org.mahz.easyheassin.server.HelloImpl</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/hessian</url-pattern> </servlet-mapping> </web-app>
3 创建hessian客户端
3.1 使用HessianProxyFactory创建客户端
在hassian-easy-client项目下创建客户端,代码如下:
package org.mahz.easyhessian.client; import java.net.MalformedURLException; import org.mahz.easyhessian.api.Hello; import com.caucho.hessian.client.HessianProxyFactory; public class Client { public static void main(String[] args) throws MalformedURLException { String url = "http://localhost:8080/hessian"; HessianProxyFactory factory = new HessianProxyFactory(); Hello d = (Hello) factory.create(Hello.class, url); System.out.println(d.sayHello("刘德华"));//打印从服务器端获取的字符串 d.printHello("Hessian"); //在服务器端控制台打印 "Hello Hessian" } }
4 运行
在主项目hessian-easy下,运行mvn clean,mvn install。构建项目。
在模块hessian-easy-server下,运行jetty:run。启动Hessian服务端。
在项目hessian-easy-client下,运行Client主程序。
在控制台中,出现hello 刘德华。即说明创建成功。