Introduction
Yesterday I took a quit look to the binary communication protocol: Hessian.
With Hessian your admins will not have any troubles with port-activation for your (‘desktop’) application, if they have to access remote services (DB etc.).
The Java implemenation from Caucho is released under the Apache License 2.0. Hessian is well integrated into Spring and seems to perform well for version 3.2 (one year old!). An ORM tool which supports Hessian out of the box is Cayenne.
Example
Now to our hello world example. It is a maven project and you can get it from here (public domain of course …).
If you don’t want to use maven you should get the following jars:
But maybe you have the same problems with downloading a none-corrupted hessian.jar from Caucho (Why that?) – then you will be forced to install maven or NetBeans.
The usage is simple: In NetBeans you can directly open maven projects with the maven plugin. Then start the jetty-server: go into the MyServlet class and press SHIFT+F6. You should see in the output window:
… INFO: Logging to STDERR via org.mortbay.log.StdErrLog
… INFO: jetty-6.1.16
… INFO: Started SocketConnector@0.0.0.0:8080
Then go to the Client class and press SHIFT+F6. You should see a new window coming up:
Change the text and press ENTER will push the text to the server and get the response text at the bottom of the window updated:
Now let us understand what is necessary:
- An interface which has to be available on the server and on the client side:
public interface CommunicationService { String communicate(String str); }
- An implementation of this interface on the server side:
public class MyServlet extends HessianServlet implements CommunicationService { public String communicate(String str) { return "Hello World! " + str; } ... }
(Here only the ‘implements CommunicationService’ is important)
- Now the server code:
public static void main(String[] args) throws Exception { Server server = new Server(8080); Context context = new Context(server, "/", Context.SESSIONS); context.addServlet(MyServlet.class, "/communication-service"); server.start(); }
- And the client code:
String url = "http://localhost:8080/communication-service"; HessianProxyFactory factory = new HessianProxyFactory(); final CommunicationService basic = (CommunicationService) factory.create(CommunicationService.class, url); ... resultLabel.setText("Server said : " + basic.communicate(field.getText()));
- Thats it!
Looks really like RPC of Java but it is language independent – there are implementations for ruby, phyton … and it seems to be more performant than language independent solutions like XML-RPC. Are there other advantages or disadvantages?
출처 - http://karussell.wordpress.com/2009/04/10/hessian-web-service-protocol-hello-world-example/
위 포스트에서 3번 서버 설정 부분에서 문제 발생 시 아래와 같이 설정한다.
This is the Jetty 8 equivalent to your code. It's still just as simple as it was before, however the API has changed slightly.
If this isn't working for you, then you probably have a classpath issue - Jetty 8 is separated into a lot of independent jar files, and you will need a number of them. At the very least you need:
- jetty-continuation
- jetty-http
- jetty-io
- jetty-security
- jetty-server
- jetty-servlet
- jetty-util
- servlet-api
If you have those jars, then this code should work fine:
package test;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
public class Jetty8Server {
public static class MyServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.getWriter().write(getClass().getName() + " - OK");
}
}
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/"); // technically not required, as "/" is the default
handler.addServlet(MyServlet.class, "/communication-service");
server.setHandler(handler);
server.start();
}
}
출처 - http://stackoverflow.com/questions/9278912/migration-from-jetty-6-to-jetty-8
'Development > Java' 카테고리의 다른 글
java - 한글 인코딩 유니코드 (0) | 2013.08.30 |
---|---|
java - 객체 직렬화(Object Serialization) (0) | 2013.08.10 |
java - 이미지 사이즈 변경(image resize) (0) | 2013.06.22 |
java - listener(callback) 만들기 2 (0) | 2013.04.08 |
java - listener 만들기 (0) | 2013.04.08 |