ITeye博客

jetty与hessian结合

cjj_720 · 2010-08-06 16:41 · java · 编程语言 · Markdown

Hessian是由caucho提供的一个基于binary-RPC实现的远程通讯library,支持多种语言,包括c++,java,c#等,还支持flash/flex Jetty 是一个开源的servlet容器,它是作为一个可以嵌入到其他的Java代码中的servlet容器而设计的。 通过结合jetty和hessian结合,就可以通过一个普通的java工程来提供远程通信服务,而不需要建立一个web工程

首先编写接口和服务类

public interface Hello {
	String sayHello(String name);
}


import com.caucho.hessian.server.HessianServlet;

/**
 * @author cjj
 *
 */
public class HelloImpl extends HessianServlet implements Hello {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1464625224364842441L;

	@Override
	public String sayHello(String name) {
		return "hello:"+name;
	}

}

HelloImpl 继承 HessianServlet

服务启动类:


import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.prime.jerryhessian.service.HelloImpl;

/**
 * @author cjj
 * 
 */
public class OneServletContext {
	public static void main(String[] args) throws Exception {
		Server server = new Server(8080);

		ServletContextHandler context = new ServletContextHandler(
				ServletContextHandler.SESSIONS);
		context.setContextPath("/");
		server.setHandler(context);
		context.addServlet(new ServletHolder(new HelloImpl()), "/hello");
		
		server.start();
		server.join();
	}

}

客户端代码为:


import java.net.MalformedURLException;

import org.prime.jerryhessian.service.Hello;

import com.caucho.hessian.client.HessianProxyFactory;

/**
 * @author cjj
 *
 */
public class HelloTest {

	/**
	 * @param args
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws MalformedURLException {
		HessianProxyFactory factory = new HessianProxyFactory();
		Hello hello = (Hello) factory.create(Hello.class,
		"http://127.0.0.1:8080/hello");

		System.out.println(hello.sayHello("cjj")); 
	}

}

用到的jar包有

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。