Actual combat jetty

Jetty is a Java-based, open source, standards-based Http server and Web container with rich functions, which can be used for commercial activities for free. The Jetty project was established in 1995, and now there are many successful products based on Jetty, such as Apache Geromino, JBoss, IBM Tivoli, Cisco SESM and so on. Jetty can be used as a traditional Web server or as a dynamic content server, and Jetty can be easily embedded in Java applications.

Introduction to Features

Ease of Use

Ease of use is Jetty design The basic principles, ease of use are mainly reflected in the following aspects:

  1. Configure Jetty through XML or API;
  2. The default configuration can meet most of the needs;
  3. Embedding Jetty into an application requires very little code;

Scalability

In Web 2.0 applications using Ajax In the program, each connection needs to be maintained for a longer time, so the consumption of threads and memory will increase sharply. This makes us worry that the entire program will affect the performance of the entire program because a single component falls into a bottleneck. But with Jetty:

  1. even In the case of a large number of service requests, the performance of the system can also be maintained in an acceptable state.
  2. Use the Continuation mechanism to handle a large number of user requests and time comparison Long connection.

In addition, Jetty has designed a very good interface, so a certain implementation in Jetty cannot meet the needs of users At the time, users can easily modify some implementations of Jetty to make Jetty suitable for special application requirements.

Ease of Embedding

Jetty was designed as an excellent component at the beginning, which means that Jetty can be easily embedded into the application without the need to modify the program in order to use Jetty. To some extent, you can also understand Jetty as an embedded Web server.

Deploy the application

Deploying your own application to Jetty is very simple Yes, first mark the developed application as a WAR package and place it under Jetty’s Webapps directory. Then use the following command to start the Jetty server: Java –jar start.jar, After starting the server. We can access our application. The default port of Jetty is 8080, and the name of the WAR is the Root Context of our application. For example, a typical URL is: http://127.0.0.1:8080/sample/index.jsp.

How to embed Jetty into the program

Embed Jetty into the program The middle is very simple, as shown in Code 1: First, we create a Server object and set the port to 8080, and then add a default Handler for this Server object. Then we use the configuration file jetty.xml to set this server, and finally we use the method server.start () Just start the Server. It can be seen from this code that Jetty is very suitable for being embedded into our application as a component, which is also a very important feature of Jetty.

Listing 1 . Code Snippets
public class JettyServer {public static void main(String[] args) {Server server = new Server(8080); server.setHandler(new DefaultHandler()); XmlConfiguration configuration = null; try {configuration = new XmlConfiguration( new FileInputStream("C: /development/Jetty/jetty-6.1. 6rc0/etc/jetty.xml"));} catch (FileNotFoundException e1) {e1.printStackTrace();} catch (SAXException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace(); } try {configuration.configure(server); server.start();} catch (Exception e) {e.printStackTrace();} }}

Next we analyze how Jetty Server is started. First of all, we noticed the Server class. This class actually inherits HttpServer. When the Jetty server is started, that is, if you enter java -jar start.jar etc/jetty.xml, note that there is a configuration file jetty.xml as a running parameter, this parameter can also be Other configuration files can be multiple XML configuration files. In fact, this configuration file is like the struts-config.xml file when we use Struts. The components needed to run the Server are written in it, such as the HttpServer configuration in the previous section. All required component classes can be written in this configuration file. When Jetty Server is started according to the above method, main Method This entry method first constructs an instance of the Server class (in fact, an HttpServer is constructed). During the creation of the instance, an object of the XmlConfiguration class is constructed to read the parameter configuration file, and then the XmlConfiguration object generated by this configuration file To configure this Server, the configuration process actually uses Java’s reflection mechanism, calling the Server method and passing in the parameters written in the configuration file to add HttpListener, HttpContext, HttpHandler, and Web Application to this Server (corresponding to our Web application).

Jetty’s Continuation mechanism

Discuss Jetty For the Continuation mechanism, Ajax technology needs to be mentioned first. Ajax technology is currently a very popular technology for developing Web applications, and it is also an important part of Web 2.0. A core object in Ajax technology is the XMLHttpRequest object, which supports asynchronous requests. The so-called asynchronous request means that when the client sends a request to the server, the client does not have to wait for the server’s response. In this way, the entire page will not be refreshed, and it will bring a better experience to the user. When the server-side response returns, the client uses a Javascript function to process the returned value to update the value of some elements on the page. But many times this kind of asynchronous event only occurs in a small part of the situation, so how to ensure that the client will know it immediately once the server has a response. We have two ways to solve this problem, one is to let the browse The device requests the server every few seconds to get changes, we call it round Inquiry. The second is that the server maintains a long-term connection with the browser to transmit data. The long-term connection technology is called Comet.

You can easily find that the main disadvantage of the polling method is a large amount of transmission waste. Because most of the requests to the server may be invalid, that is to say, the event that the client is waiting for has not occurred. If there are a large number of clients, the waste of network transmission is very severe. Especially for applications that have been updated on the server side for a long time, such as mail programs, this waste is even greater. And the ability to handle requests on the Server side has also increased requirements accordingly. If it takes a long time to send a request to the server, the client cannot get a timely response.

If Comet technology is used, the client and server must maintain a long connection. In general, each server Servlet will monopolize a thread, which will cause many threads on the server side to exist at the same time, which will also bring great challenges to the processing capacity of the server side in the case of a large number of clients.

Jetty uses Java language’s non-blocking I/O technology to handle a large number of concurrent connections. Jetty has a mechanism for handling long connections: one is called Continuations. Using the Continuation mechanism, Jetty can enable one thread to process multiple asynchronous requests sent from the client at the same time. Below we demonstrate the difference between not using the Continuation mechanism and using the Continuation through the server-side code of a simplified chat program.

Listing 2 . Continuation mechanism
public class ChatContinuation extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response){ postMessage(request, response);} private void postMessage(HttpServletRequest request, HttpServletResponse response) {HttpSession session = request.getSession(t rue); People people = (People)session.getAttribute(session.getId()); if (!people.hasEvent()) {Continuation continuation = ContinuationSupport.getContinuation(request, this); people.setContinuation(continuation); continuation .suspend(1000);} people.setContinuation(null); people.sendEvent(response); }}

Everyone noticed that, first get a Continuation object, and then hang it for 1 second until it times out or it is blocked in the middle. resume Function wake-up position, what needs to be explained here is that after calling suspend After the function, this thread can handle other requests, which greatly improves the concurrency of the program and makes long connections very good Extensibility.

If we don’t use the Continuation mechanism, then the program will look like Listing 3:

Listing 3. Do not use the Continuation mechanism
public class Chat extends HttpServlet{ public void doPost (HttpSer vletRequest request, HttpServletResponse response){ postMessage(request, response);} private void postMessage(HttpServletRequest request, HttpServletResponse response) {HttpSession session = request.getSession(true); People people = (People)session.getAttribute(session.getId( )); while (!people.hasEvent()) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}} people.setContinuation(null); people.sendEvent(response) ; })

We noticed that while waiting for the event to occur, the thread is suspended until all The waiting event occurs, but in the waiting process, this thread cannot handle other requests, which also causes the server’s processing capacity to keep up with a large number of clients. Below we explain how Jetty’s Continuation mechanism works.

In order to use Continuatins, Jetty must be configured to use its SelectChannelConnector Process the request. This connector is built on the java.nio API, allowing it to keep each connection open without consuming a thread. When using SelectChannelConnector When, ContinuationSupport.getContinuation() Provide a SelectChannelConnector.RetryContinuation Example (However, you must target Continuation interface programming). When in RetryContinuation suspend(), it throws a special runtime exception– RetryRequest, the exception is propagated outside the servlet and traced back to the filter chain, and finally is SelectChannelConnector capture. But instead of sending an exception response to the client, but keeping the request in the pending Continuations queue, the HTTP connection remains open. In this way, the thread used to service the request is returned to the ThreadPool, which can then be used to service other requests. The suspended request stays in the pending Continuations queue until the specified expiration time, or calls on its Continuation resume() method. When any one of the conditions is triggered, the request will be resubmitted to the servlet (through the filter chain). In this way, the entire request is “replayed” until the RetryRequest exception is no longer thrown, and then continues to execute as normal.

Jetty safety

In order to prevent anyone from having the right to close an already opened Jetty server, we can control by specifying parameters when starting the Jetty server, so that the user must provide a password to shut down the Jetty server. The command to start the Jetty server is as follows:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar 

In this way, the user must provide the password “mypassword” when stopping the Jetty server.

Summary

Jetty is a very convenient Web server with its characteristics It is very small, easy to embed in our application, and specially optimized for the Ajax technology of Web 2.0, which also makes our application using Ajax have better performance.

Original address: https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

Jetty 是一个用 Java 实现、开源、基于标准的,并且具有丰富功能的 Http 服务器和 Web 容器,可以免费的用于商业行为。 Jetty 这个项目成立于 1995 年,现在已经有非常多的成功产品基于 Jetty,比如 Apache Geromino, JBoss, IBM Tivoli, Cisco SESM 等。 Jetty 可以用来作为一个传统的 Web 服务器,也可以作为一个动态的内容服务器,并且 Jetty 可以非常容易的嵌入到 Java 应用程序当中。

Jetty 是一个用 Java 实现、开源、基于标准的,并且具有丰富功能的 Http 服务器和 Web 容器,可以免费的用于商业行为。 Jetty 这个项目成立于 1995 年,现在已经有非常多的成功产品基于 Jetty,比如 Apache Geromino, JBoss, IBM Tivoli, Cisco SESM 等。 Jetty 可以用来作为一个传统的 Web 服务器,也可以作为一个动态的内容服务器,并且 Jetty 可以非常容易的嵌入到 Java 应用程序当中。

Jetty 是一个用 Java 实现、开源、基于标准的,并且具有丰富功能的 Http 服务器和 Web 容器,可以免费的用于商业行为。 Jetty 这个项目成立于 1995 年,现在已经有非常多的成功产品基于 Jetty,比如 Apache Geromino, JBoss, IBM Tivoli, Cisco SESM 等。 Jetty 可以用来作为一个传统的 Web 服务器,也可以作为一个动态的内容服务器,并且 Jetty 可以非常容易的嵌入到 Java 应用程序当中。

特性简介

易用性

易用性是 Jetty 设计的基本原则,易用性主要体现在以下几个方面:

  1. 通过 XML 或者 API 来对 Jetty 进行配置;
  2. 默认配置可以满足大部分的需求;
  3. 将 Jetty 嵌入到应用程序当中只需要非常少的代码;

可扩展性

在使用了 Ajax 的 Web 2.0 的应用程序中,每个连接需要保持更长的时间,这样线程和内存的消耗量会急剧的增加。这就使得我们担心整个程序会因为单个组件陷入瓶颈而影响整个程序的性能。但是有了 Jetty:

  1. 即使在有大量服务请求的情况下,系统的性能也能保持在一个可以接受的状态。
  2. 利用 Continuation 机制来处理大量的用户请求以及时间比较长的连接。

另外 Jetty 设计了非常良好的接口,因此在 Jetty 的某种实现无法满足用户的需要时,用户可以非常方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。

易嵌入性

Jetty 设计之初就是作为一个优秀的组件来设计的,这也就意味着 Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。从某种程度上,你也可以把 Jetty 理解为一个嵌入式的Web服务器。

部署应用程序

将自己的应用程序部署到 Jetty 上面是非常简单的,首先将开发好的应用程序打成 WAR 包放到 Jetty 的 Webapps 目录下面。然后用如下的命令来启动 Jetty 服务器:Java –jar start.jar, 在启动服务器后。我们就可以访问我们的应用程序了,Jetty 的默认端口是 8080,WAR 的名字也就是我们的应用程序的 Root Context。例如一个典型的 URL 就是:http://127.0.0.1:8080/sample/index.jsp 。

如何将 Jetty 嵌入到程序当中

将 Jetty 嵌入到程序当中是非常简单的, 如 代码 1 所示:首先我们创建一个 Server 对象, 并设置端口为 8080,然后为这个 Server 对象添加一个默认的 Handler。接着我们用配置文件 jetty.xml 对这个 server 进行设置,最后我们使用方法 server.start() 将 Server 启动起来就可以了。从这段代码可以看出,Jetty 是非常适合用于作为一个组件来嵌入到我们的应用程序当中的,这也是 Jetty 的一个非常重要的特点。

清单 1. 代码片断
public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc 0/etc/jetty.xml"));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

接下来我们分析一下 Jetty Server 是如何启动的。首先我们注意到 Server 类,这个类实际上继承了 HttpServer, 当启动 Jetty 服务器的时候,就是说,在 Jetty 根目录下的命令行下如果输入 java -jar start.jar etc/jetty.xml,注意这里有一个配置文件 jetty.xml 做为运行参数,这个参数也可以是其它的配置文件,可以是多个 XML 配置文件,其实这个配置文件好比我们使用 Struts 时的 struts-config.xml 文件,将运行 Server 需要用到的组件写在里面,比如上一节中 HttpServer 的配置需要的组件类都可以写在这个配置文件中。按上述方法启动 Jetty Server 时,就会调用 Server 类里面的 main 方法,这个入口方法首先会构造一个 Server 类实例(其实也就构造了一个 HttpServer),创建实例的过程中就会构造 XmlConfiguration 类的对象来读取参数配置文件,之后再由这个配置文件产生的 XmlConfiguration 对象来配置这个 Server,配置过程其实是运用了 Java 的反射机制,调用 Server 的方法并传入配置文件中所写的参数来向这个 Server 添加 HttpListener,HttpContext,HttpHandler,以及 Web Application(对应于我们的 Web 应用)。

Jetty 的 Continuation 机制

讨论 Jetty 的 Co ntinuation 机制,首先需要提到 Ajax 技术,Ajax 技术是当前开发 Web 应用的非常热门的技术,也是 Web 2.0 的一个重要的组成部分。 Ajax 技术中的一个核心对象是 XMLHttpRequest 对象,这个对象支持异步请求,所谓异步请求即是指当客户端发送一个请求到服务器的时候,客户端不必一直等待服务器的响应。这样就不会造成整个页面的刷新,给用户带来更好的体验。而当服务器端响应返回时,客户端利用一个 Javascript 函数对返回值进行处理,以更新页面上的部分元素的值。但很多时候这种异步事件只是在很小一部分的情况下才会发生,那么怎么保证一旦服务器端有了响应之后客户端马上就知道呢,我们有两种方法来解决这个问题,一是让浏览器每隔几秒请求服务器来获得更改,我们称之为轮询。二是服务器维持与浏览器的长时间的连接来传递数据,长连接的技术称之为 Comet

大家很容易就能发现轮询方式的主要缺点是产生了大量的传输浪费。因为可能大部分向服务器的请求是无效的,也就是说客户端等待发生的事件没有发生,如果有大量的客户端的话,那么这种网络传输的浪费是非常厉害的。特别是对于服务器端很久才更新的应用程序来讲,比如邮件程序,这种浪费就更是巨大了。并且对 Server 端处理请求的能力也相应提高了要求。如果很长时间才向 Server 端发送一次请求的话,那么客户端就不能的得到及时的响应。

如果使用 Comet 技术的话,客户端和服务器端必须保持一个长连接,一般情况下,服务器端每一个 Servlet 都会独占一个线程,这样就会使得服务器端有很多线程同时存在,这在客户端非常多的情况下也会对服务器端的处理能力带来很大的挑战。

Jetty 利用 Java 语言的非堵塞 I/O 技术来处理并发的大量连接。 Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求,下面我们通过一个简化的聊天程序的服务器端的代码来演示不使用 Continuation 机制和使用 Continuation 的差别。

清单 2. Continuation 机制
public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true );        People people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

大家注意到,首先获取一个 Continuation 对象,然后把它挂起 1 秒钟,直到超时或者中间被 resume 函数唤醒位置,这里需要解释的是,在调用完 suspend 函数之后,这个线程就可处理其他的请求了,这也就大大提高了程序的并发性,使得长连接能够获得非常好的扩展性。

如果我们不使用 Continuation 机制,那么程序就如 清单 3 所示:

清单 3. 不使用 Continuation 机制
public class Chat extends HttpServlet{    public void doPost(HttpServlet Request request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

大家注意到在等待事件发生的时间里,线程被挂起,直到所等待的事件发生为止,但在等待过程中,这个线程不能处理其他请求,这也就造成了在客户端非常多的情况下服务器的处理能力跟不上的情况。下面我们解释一下 Jetty 的 Continuation 的机制是如何工作的。

为了使用 Continuatins,Jetty 必须配置为使用它的 SelectChannelConnector 处理请求。这个 connector 构建在 java.nio API 之上,允许它维持每个连接开放而不用消耗一个线程。当使用 SelectChannelConnector 时,ContinuationSupport.getContinuation() 提供一个SelectChannelConnector.RetryContinuation 实例(但是,您必须针对 Continuation 接口编程)。当在 RetryContinuation 上调用suspend() 时,它抛出一个特殊的运行时异常 — RetryRequest,该异常传播到 servlet 外并且回溯到 filter 链,最后被SelectChannelConnector 捕获。但是不会发送一个异常响应给客户端,而是将请求维持在未决 Continuations 队列里,则 HTTP 连接保持开放。这样,用来服务请求的线程返回给 ThreadPool,然后又可以用来服务其他请求。暂停的请求停留在未决 Continuations 队列里直到指定的过期时间,或者在它的 Continuation 上调用 resume() 方法。当任何一个条件触发时,请求会重新提交给 servlet(通过 filter 链)。这样,整个请求被”重播”直到 RetryRequest 异常不再抛出,然后继续按正常情况执行。

Jetty 的安全性

为了防止任何人都有权限去关闭一个已经开启的 Jett y 服务器, 我们可以通过在启动 Jetty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

总结

Jetty 是一个非常方便使用的 Web 服务器,它的特点在于非常小,很容易嵌入到我们的应用程序当中,而且针对 Web 2.0 的 Ajax 技术进行了特别的优化,这也使得我们的使用 Ajax 的应用程序可以拥有更好的性能。

原文地址:https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

特性简介

易用性

易用性是 Jetty 设计的基本原则,易用性主要体现在以下几个方面:

  1. 通过 XML 或者 API 来对 Jetty 进行配置;
  2. 默认配置可以满足大部分的需求;
  3. 将 Jetty 嵌入到应用程序当中只需要非常少的代码;

可扩展性

在使用了 Ajax 的 Web 2.0 的应用程序中,每个连接需要保持更长的时间,这样线程和内存的消耗量会急剧的增加。这就使得我们担心整个程序会因为单个组件陷入瓶颈而影响整个程序的性能。但是有了 Jetty:

  1. 即使在有大量服务请求的情况下,系统的性能也能保持在一个可以接受的状态。
  2. 利用 Continuation 机制来处理大量的用户请求以及时间比较长的连接。

另外 Jetty 设计了非常良好的接口,因此在 Jetty 的某种实现无法满足用户的需要时,用户可以非常方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。

易嵌入性

Jetty 设计之初就是作为一个优秀的组件来设计的,这也就意味着 Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。从某种程度上,你也可以把 Jetty 理解为一个嵌入式的Web服务器。

部署应用程序

将自己的应用程序部署到 Jetty 上面是非常简单的,首先将开发好的应用程序打成 WAR 包放到 Jetty 的 Webapps 目录下面。然后用如下的命令来启动 Jetty 服务器:Java –jar start.jar, 在启动服务器后。我们就可以访问我们的应用程序了,Jetty 的默认端口是 8080,WAR 的名字也就是我们的应用程序的 Root Context。例如一个典型的 URL 就是:http://127.0.0.1:8080/sample/index.jsp 。

如何将 Jetty 嵌入到程序当中

将 Jetty 嵌入到程序当中是非常简单的, 如 代码 1 所示:首先我们创建一个 Server 对象, 并设置端口为 8080,然后为这个 Server 对象添加一个默认的 Handler。接着我们用配置文件 jetty.xml 对这个 server 进行设置,最后我们使用方法 server.start() 将 Server 启动起来就可以了。从这段代码可以看出,Jetty 是非常适合用于作为一个组件来嵌入到我们的应用程序当中的,这也是 Jetty 的一个非常重要的特点。

清单 1. 代码片断
public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc0/e tc/jetty.xml"));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

接下来我们分析一下 Jetty Server 是如何启动的。首先我们注意到 Server 类,这个类实际上继承了 HttpServer, 当启动 Jetty 服务器的时候,就是说,在 Jetty 根目录下的命令行下如果输入 java -jar start.jar etc/jetty.xml,注意这里有一个配置文件 jetty.xml 做为运行参数,这个参数也可以是其它的配置文件,可以是多个 XML 配置文件,其实这个配置文件好比我们使用 Struts 时的 struts-config.xml 文件,将运行 Server 需要用到的组件写在里面,比如上一节中 HttpServer 的配置需要的组件类都可以写在这个配置文件中。按上述方法启动 Jetty Server 时,就会调用 Server 类里面的 main 方法,这个入口方法首先会构造一个 Server 类实例(其实也就构造了一个 HttpServer),创建实例的过程中就会构造 XmlConfiguration 类的对象来读取参数配置文件,之后再由这个配置文件产生的 XmlConfiguration 对象来配置这个 Server,配置过程其实是运用了 Java 的反射机制,调用 Server 的方法并传入配置文件中所写的参数来向这个 Server 添加 HttpListener,HttpContext,HttpHandler,以及 Web Application(对应于我们的 Web 应用)。

Jetty 的 Continuation 机制

讨论 Jetty 的 Continu ation 机制,首先需要提到 Ajax 技术,Ajax 技术是当前开发 Web 应用的非常热门的技术,也是 Web 2.0 的一个重要的组成部分。 Ajax 技术中的一个核心对象是 XMLHttpRequest 对象,这个对象支持异步请求,所谓异步请求即是指当客户端发送一个请求到服务器的时候,客户端不必一直等待服务器的响应。这样就不会造成整个页面的刷新,给用户带来更好的体验。而当服务器端响应返回时,客户端利用一个 Javascript 函数对返回值进行处理,以更新页面上的部分元素的值。但很多时候这种异步事件只是在很小一部分的情况下才会发生,那么怎么保证一旦服务器端有了响应之后客户端马上就知道呢,我们有两种方法来解决这个问题,一是让浏览器每隔几秒请求服务器来获得更改,我们称之为轮询。二是服务器维持与浏览器的长时间的连接来传递数据,长连接的技术称之为 Comet

大家很容易就能发现轮询方式的主要缺点是产生了大量的传输浪费。因为可能大部分向服务器的请求是无效的,也就是说客户端等待发生的事件没有发生,如果有大量的客户端的话,那么这种网络传输的浪费是非常厉害的。特别是对于服务器端很久才更新的应用程序来讲,比如邮件程序,这种浪费就更是巨大了。并且对 Server 端处理请求的能力也相应提高了要求。如果很长时间才向 Server 端发送一次请求的话,那么客户端就不能的得到及时的响应。

如果使用 Comet 技术的话,客户端和服务器端必须保持一个长连接,一般情况下,服务器端每一个 Servlet 都会独占一个线程,这样就会使得服务器端有很多线程同时存在,这在客户端非常多的情况下也会对服务器端的处理能力带来很大的挑战。

Jetty 利用 Java 语言的非堵塞 I/O 技术来处理并发的大量连接。 Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求,下面我们通过一个简化的聊天程序的服务器端的代码来演示不使用 Continuation 机制和使用 Continuation 的差别。

清单 2. Continuation 机制
public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

大家注意到,首先获取一个 Continuation 对象,然后把它挂起 1 秒钟,直到超时或者中间被 resume 函数唤醒位置,这里需要解释的是,在调用完 suspend 函数之后,这个线程就可处理其他的请求了,这也就大大提高了程序的并发性,使得长连接能够获得非常好的扩展性。

如果我们不使用 Continuation 机制,那么程序就如 清单 3 所示:

清单 3. 不使用 Continuation 机制
public class Chat extends HttpServlet{    public void doPost(HttpServletRequ est request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

大家注意到在等待事件发生的时间里,线程被挂起,直到所等待的事件发生为止,但在等待过程中,这个线程不能处理其他请求,这也就造成了在客户端非常多的情况下服务器的处理能力跟不上的情况。下面我们解释一下 Jetty 的 Continuation 的机制是如何工作的。

为了使用 Continuatins,Jetty 必须配置为使用它的 SelectChannelConnector 处理请求。这个 connector 构建在 java.nio API 之上,允许它维持每个连接开放而不用消耗一个线程。当使用 SelectChannelConnector 时,ContinuationSupport.getContinuation() 提供一个SelectChannelConnector.RetryContinuation 实例(但是,您必须针对 Continuation 接口编程)。当在 RetryContinuation 上调用suspend() 时,它抛出一个特殊的运行时异常 — RetryRequest,该异常传播到 servlet 外并且回溯到 filter 链,最后被SelectChannelConnector 捕获。但是不会发送一个异常响应给客户端,而是将请求维持在未决 Continuations 队列里,则 HTTP 连接保持开放。这样,用来服务请求的线程返回给 ThreadPool,然后又可以用来服务其他请求。暂停的请求停留在未决 Continuations 队列里直到指定的过期时间,或者在它的 Continuation 上调用 resume() 方法。当任何一个条件触发时,请求会重新提交给 servlet(通过 filter 链)。这样,整个请求被”重播”直到 RetryRequest 异常不再抛出,然后继续按正常情况执行。

Jetty 的安全性

为了防止任何人都有权限去关闭一个已经开启的 Jetty 服务器, 我们可以通过在启动 Jetty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

总结

Jetty 是一个非常方便使用的 Web 服务器,它的特点在于非常小,很容易嵌入到我们的应用程序当中,而且针对 Web 2.0 的 Ajax 技术进行了特别的优化,这也使得我们的使用 Ajax 的应用程序可以拥有更好的性能。

原文地址:https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

特性简介

易用性

易用性是 Jetty 设计的基本原则,易用性主要体现在以下几个方面:

  1. 通过 XML 或者 API 来对 Jetty 进行配置;
  2. 默认配置可以满足大部分的需求;
  3. 将 Jetty 嵌入到应用程序当中只需要非常少的代码;

可扩展性

在使用了 Ajax 的 Web 2.0 的应用程序中,每个连接需要保持更长的时间,这样线程和内存的消耗量会急剧的增加。这就使得我们担心整个程序会因为单个组件陷入瓶颈而影响整个程序的性能。但是有了 Jetty:

  1. 即使在有大量服务请求的情况下,系统的性能也能保持在一个可以接受的状态。
  2. 利用 Continuation 机制来处理大量的用户请求以及时间比较长的连接。

另外 Jetty 设计了非常良好的接口,因此在 Jetty 的某种实现无法满足用户的需要时,用户可以非常方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。

易嵌入性

Jetty 设计之初就是作为一个优秀的组件来设计的,这也就意味着 Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。从某种程度上,你也可以把 Jetty 理解为一个嵌入式的Web服务器。

部署应用程序

将自己的应用程序部署到 Jetty 上面是非常简单的,首先将开发好的应用程序打成 WAR 包放到 Jetty 的 Webapps 目录下面。然后用如下的命令来启动 Jetty 服务器:Java –jar start.jar, 在启动服务器后。我们就可以访问我们的应用程序了,Jetty 的默认端口是 8080,WAR 的名字也就是我们的应用程序的 Root Context。例如一个典型的 URL 就是:http://127.0.0.1:8080/sample/index.jsp 。

如何将 Jetty 嵌入到程序当中

将 Jetty 嵌入到程序当中是非常简单的, 如 代码 1 所示:首先我们创建一个 Server 对象, 并设置端口为 8080,然后为这个 Server 对象添加一个默认的 Handler。接着我们用配置文件 jetty.xml 对这个 server 进行设置,最后我们使用方法 server.start() 将 Server 启动起来就可以了。从这段代码可以看出,Jetty 是非常适合用于作为一个组件来嵌入到我们的应用程序当中的,这也是 Jetty 的一个非常重要的特点。

清单 1. 代码片断
public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc0/etc/ jetty.xml"));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

接下来我们分析一下 Jetty Server 是如何启动的。首先我们注意到 Server 类,这个类实际上继承了 HttpServer, 当启动 Jetty 服务器的时候,就是说,在 Jetty 根目录下的命令行下如果输入 java -jar start.jar etc/jetty.xml,注意这里有一个配置文件 jetty.xml 做为运行参数,这个参数也可以是其它的配置文件,可以是多个 XML 配置文件,其实这个配置文件好比我们使用 Struts 时的 struts-config.xml 文件,将运行 Server 需要用到的组件写在里面,比如上一节中 HttpServer 的配置需要的组件类都可以写在这个配置文件中。按上述方法启动 Jetty Server 时,就会调用 Server 类里面的 main 方法,这个入口方法首先会构造一个 Server 类实例(其实也就构造了一个 HttpServer),创建实例的过程中就会构造 XmlConfiguration 类的对象来读取参数配置文件,之后再由这个配置文件产生的 XmlConfiguration 对象来配置这个 Server,配置过程其实是运用了 Java 的反射机制,调用 Server 的方法并传入配置文件中所写的参数来向这个 Server 添加 HttpListener,HttpContext,HttpHandler,以及 Web Application(对应于我们的 Web 应用)。

Jetty 的 Continuation 机制

讨论 Jetty 的 Continuation 机制,首先需要提到 Ajax 技术,Ajax 技术是当前开发 Web 应用的非常热门的技术,也是 Web 2.0 的一个重要的组成部分。 Ajax 技术中的一个核心对象是 XMLHttpRequest 对象,这个对象支持异步请求,所谓异步请求即是指当客户端发送一个请求到服务器的时候,客户端不必一直等待服务器的响应。这样就不会造成整个页面的刷新,给用户带来更好的体验。而当服务器端响应返回时,客户端利用一个 Javascript 函数对返回值进行处理,以更新页面上的部分元素的值。但很多时候这种异步事件只是在很小一部分的情况下才会发生,那么怎么保证一旦服务器端有了响应之后客户端马上就知道呢,我们有两种方法来解决这个问题,一是让浏览器每隔几秒请求服务器来获得更改,我们称之为轮询。二是服务器维持与浏览器的长时间的连接来传递数据,长连接的技术称之为 Comet

大家很容易就能发现轮询方式的主要缺点是产生了大量的传输浪费。因为可能大部分向服务器的请求是无效的,也就是说客户端等待发生的事件没有发生,如果有大量的客户端的话,那么这种网络传输的浪费是非常厉害的。特别是对于服务器端很久才更新的应用程序来讲,比如邮件程序,这种浪费就更是巨大了。并且对 Server 端处理请求的能力也相应提高了要求。如果很长时间才向 Server 端发送一次请求的话,那么客户端就不能的得到及时的响应。

如果使用 Comet 技术的话,客户端和服务器端必须保持一个长连接,一般情况下,服务器端每一个 Servlet 都会独占一个线程,这样就会使得服务器端有很多线程同时存在,这在客户端非常多的情况下也会对服务器端的处理能力带来很大的挑战。

Jetty 利用 Java 语言的非堵塞 I/O 技术来处理并发的大量连接。 Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求,下面我们通过一个简化的聊天程序的服务器端的代码来演示不使用 Continuation 机制和使用 Continuation 的差别。

清单 2. Continuation 机制
public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

大家注意到,首先获取一个 Continuation 对象,然后把它挂起 1 秒钟,直到超时或者中间被 resume 函数唤醒位置,这里需要解释的是,在调用完 suspend 函数之后,这个线程就可处理其他的请求了,这也就大大提高了程序的并发性,使得长连接能够获得非常好的扩展性。

如果我们不使用 Continuation 机制,那么程序就如 清单 3 所示:

清单 3. 不使用 Continuation 机制
public class Chat extends HttpServlet{    public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

大家注意到在等待事件发生的时间里,线程被挂起,直到所等待的事件发生为止,但在等待过程中,这个线程不能处理其他请求,这也就造成了在客户端非常多的情况下服务器的处理能力跟不上的情况。下面我们解释一下 Jetty 的 Continuation 的机制是如何工作的。

为了使用 Continuatins,Jetty 必须配置为使用它的 SelectChannelConnector 处理请求。这个 connector 构建在 java.nio API 之上,允许它维持每个连接开放而不用消耗一个线程。当使用 SelectChannelConnector 时,ContinuationSupport.getContinuation() 提供一个SelectChannelConnector.RetryContinuation 实例(但是,您必须针对 Continuation 接口编程)。当在 RetryContinuation 上调用suspend() 时,它抛出一个特殊的运行时异常 — RetryRequest,该异常传播到 servlet 外并且回溯到 filter 链,最后被SelectChannelConnector 捕获。但是不会发送一个异常响应给客户端,而是将请求维持在未决 Continuations 队列里,则 HTTP 连接保持开放。这样,用来服务请求的线程返回给 ThreadPool,然后又可以用来服务其他请求。暂停的请求停留在未决 Continuations 队列里直到指定的过期时间,或者在它的 Continuation 上调用 resume() 方法。当任何一个条件触发时,请求会重新提交给 servlet(通过 filter 链)。这样,整个请求被”重播”直到 RetryRequest 异常不再抛出,然后继续按正常情况执行。

Jetty 的安全性

为了防止任何人都有权限去关闭一个已经开启的 Jetty 服务器, 我们可以通过在启动 Jetty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

总结

Jetty 是一个非常方便使用的 Web 服务器,它的特点在于非常小,很容易嵌入到我们的应用程序当中,而且针对 Web 2.0 的 Ajax 技术进行了特别的优化,这也使得我们的使用 Ajax 的应用程序可以拥有更好的性能。

原文地址:https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

特性简介

易用性

易用性是 Jetty 设计的基本原则,易用性主要体现在以下几个方面:

  1. 通过 XML 或者 API 来对 Jetty 进行配置;
  2. 默认配置可以满足大部分的需求;
  3. 将 Jetty 嵌入到应用程序当中只需要非常少的代码;

可扩展性

在使用了 Ajax 的 Web 2.0 的应用程序中,每个连接需要保持更长的时间,这样线程和内存的消耗量会急剧的增加。这就使得我们担心整个程序会因为单个组件陷入瓶颈而影响整个程序的性能。但是有了 Jetty:

  1. 即使在有大量服务请求的情况下,系统的性能也能保持在一个可以接受的状态。
  2. 利用 Continuation 机制来处理大量的用户请求以及时间比较长的连接。

另外 Jetty 设计了非常良好的接口,因此在 Jetty 的某种实现无法满足用户的需要时,用户可以非常方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。

易嵌入性

Jetty 设计之初就是作为一个优秀的组件来设计的,这也就意味着 Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。从某种程度上,你也可以把 Jetty 理解为一个嵌入式的Web服务器。

部署应用程序

将自己的应用程序部署到 Jetty 上面是非常简单的,首先将开发好的应用程序打成 WAR 包放到 Jetty 的 Webapps 目录下面。然后用如下的命令来启动 Jetty 服务器:Java –jar start.jar, 在启动服务器后。我们就可以访问我们的应用程序了,Jetty 的默认端口是 8080,WAR 的名字也就是我们的应用程序的 Root Context。例如一个典型的 URL 就是:http://127.0.0.1:8080/sample/index.jsp 。

如何将 Jetty 嵌入到程序当中

将 Jetty 嵌入到程序当中是非常简单的, 如 代码 1 所示:首先我们创建一个 Server 对象, 并设置端口为 8080,然后为这个 Server 对象添加一个默认的 Handler。接着我们用配置文件 jetty.xml 对这个 server 进行设置,最后我们使用方法 server.start() 将 Server 启动起来就可以了。从这段代码可以看出,Jetty 是非常适合用于作为一个组件来嵌入到我们的应用程序当中的,这也是 Jetty 的一个非常重要的特点。

清单 1. 代码片断
public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc0/etc/jet ty.xml"));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

接下来我们分析一下 Jetty Server 是如何启动的。首先我们注意到 Server 类,这个类实际上继承了 HttpServer, 当启动 Jetty 服务器的时候,就是说,在 Jetty 根目录下的命令行下如果输入 java -jar start.jar etc/jetty.xml,注意这里有一个配置文件 jetty.xml 做为运行参数,这个参数也可以是其它的配置文件,可以是多个 XML 配置文件,其实这个配置文件好比我们使用 Struts 时的 struts-config.xml 文件,将运行 Server 需要用到的组件写在里面,比如上一节中 HttpServer 的配置需要的组件类都可以写在这个配置文件中。按上述方法启动 Jetty Server 时,就会调用 Server 类里面的 main 方法,这个入口方法首先会构造一个 Server 类实例(其实也就构造了一个 HttpServer),创建实例的过程中就会构造 XmlConfiguration 类的对象来读取参数配置文件,之后再由这个配置文件产生的 XmlConfiguration 对象来配置这个 Server,配置过程其实是运用了 Java 的反射机制,调用 Server 的方法并传入配置文件中所写的参数来向这个 Server 添加 HttpListener,HttpContext,HttpHandler,以及 Web Application(对应于我们的 Web 应用)。

Jetty 的 Continuation 机制

讨论 Jetty 的 Continuation 机制,首先需要提到 Ajax 技术,Ajax 技术是当前开发 Web 应用的非常热门的技术,也是 Web 2.0 的一个重要的组成部分。 Ajax 技术中的一个核心对象是 XMLHttpRequest 对象,这个对象支持异步请求,所谓异步请求即是指当客户端发送一个请求到服务器的时候,客户端不必一直等待服务器的响应。这样就不会造成整个页面的刷新,给用户带来更好的体验。而当服务器端响应返回时,客户端利用一个 Javascript 函数对返回值进行处理,以更新页面上的部分元素的值。但很多时候这种异步事件只是在很小一部分的情况下才会发生,那么怎么保证一旦服务器端有了响应之后客户端马上就知道呢,我们有两种方法来解决这个问题,一是让浏览器每隔几秒请求服务器来获得更改,我们称之为轮询。二是服务器维持与浏览器的长时间的连接来传递数据,长连接的技术称之为 Comet

大家很容易就能发现轮询方式的主要缺点是产生了大量的传输浪费。因为可能大部分向服务器的请求是无效的,也就是说客户端等待发生的事件没有发生,如果有大量的客户端的话,那么这种网络传输的浪费是非常厉害的。特别是对于服务器端很久才更新的应用程序来讲,比如邮件程序,这种浪费就更是巨大了。并且对 Server 端处理请求的能力也相应提高了要求。如果很长时间才向 Server 端发送一次请求的话,那么客户端就不能的得到及时的响应。

如果使用 Comet 技术的话,客户端和服务器端必须保持一个长连接,一般情况下,服务器端每一个 Servlet 都会独占一个线程,这样就会使得服务器端有很多线程同时存在,这在客户端非常多的情况下也会对服务器端的处理能力带来很大的挑战。

Jetty 利用 Java 语言的非堵塞 I/O 技术来处理并发的大量连接。 Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求,下面我们通过一个简化的聊天程序的服务器端的代码来演示不使用 Continuation 机制和使用 Continuation 的差别。

清单 2. Continuation 机制
public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

大家注意到,首先获取一个 Continuation 对象,然后把它挂起 1 秒钟,直到超时或者中间被 resume 函数唤醒位置,这里需要解释的是,在调用完 suspend 函数之后,这个线程就可处理其他的请求了,这也就大大提高了程序的并发性,使得长连接能够获得非常好的扩展性。

如果我们不使用 Continuation 机制,那么程序就如 清单 3 所示:

清单 3. 不使用 Continuation 机制
public class Chat extends HttpServlet{    public void doPost(HttpServletRequest requ est, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

大家注意到在等待事件发生的时间里,线程被挂起,直到所等待的事件发生为止,但在等待过程中,这个线程不能处理其他请求,这也就造成了在客户端非常多的情况下服务器的处理能力跟不上的情况。下面我们解释一下 Jetty 的 Continuation 的机制是如何工作的。

为了使用 Continuatins,Jetty 必须配置为使用它的 SelectChannelConnector 处理请求。这个 connector 构建在 java.nio API 之上,允许它维持每个连接开放而不用消耗一个线程。当使用 SelectChannelConnector 时,ContinuationSupport.getContinuation() 提供一个SelectChannelConnector.RetryContinuation 实例(但是,您必须针对 Continuation 接口编程)。当在 RetryContinuation 上调用suspend() 时,它抛出一个特殊的运行时异常 — RetryRequest,该异常传播到 servlet 外并且回溯到 filter 链,最后被SelectChannelConnector 捕获。但是不会发送一个异常响应给客户端,而是将请求维持在未决 Continuations 队列里,则 HTTP 连接保持开放。这样,用来服务请求的线程返回给 ThreadPool,然后又可以用来服务其他请求。暂停的请求停留在未决 Continuations 队列里直到指定的过期时间,或者在它的 Continuation 上调用 resume() 方法。当任何一个条件触发时,请求会重新提交给 servlet(通过 filter 链)。这样,整个请求被”重播”直到 RetryRequest 异常不再抛出,然后继续按正常情况执行。

Jetty 的安全性

为了防止任何人都有权限去关闭一个已经开启的 Jetty 服务器, 我们可以通过在启动 Jetty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

总结

Jetty 是一个非常方便使用的 Web 服务器,它的特点在于非常小,很容易嵌入到我们的应用程序当中,而且针对 Web 2.0 的 Ajax 技术进行了特别的优化,这也使得我们的使用 Ajax 的应用程序可以拥有更好的性能。

原文地址:https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

特性简介

易用性

易用性是 Jetty 设计的基本原则,易用性主要体现在以下几个方面:

  1. 通过 XML 或者 API 来对 Jetty 进行配置;
  2. 默认配置可以满足大部分的需求;
  3. 将 Jetty 嵌入到应用程序当中只需要非常少的代码;< /li>

可扩展性

在使用了 Ajax 的 Web 2.0 的应用程序中,每个连接需要保持更长的时间,这样线程和内存的消耗量会急剧的增加。这就使得我们担心整个程序会因为单个组件陷入瓶颈而影响整个程序的性能。但是有了 Jetty:

  1. 即使在有大量服务请求的情况下,系统的性能也能保持在一个可以接受的状态。
  2. 利用 Continuation 机制来处理大量的用户请求以及时间比较长的连接。

另外 Jetty 设计了非常良好的接口,因此在 Jetty 的某种实现无法满足用户的需要时,用户可以非常方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。

易嵌入性

Jetty 设计之初就是作为一个优秀的组件来设计的,这也就意味着 Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。从某种程度上,你也可以把 Jetty 理解为一个嵌入式的Web服务器。

部署应用程序

将自己的应用程序部署到 Jetty 上面是非常简单的,首先将开发好的应用程序打成 WAR 包放到 J etty 的 Webapps 目录下面。然后用如下的命令来启动 Jetty 服务器:Java –jar start.jar, 在启动服务器后。我们就可以访问我们的应用程序了,Jetty 的默认端口是 8080,WAR 的名字也就是我们的应用程序的 Root Context。例如一个典型的 URL 就是:http://127.0.0.1:8080/sample/index.jsp 。

如何将 Jetty 嵌入到程序当中

将 Jetty 嵌入到程序当中是非常简单的, 如 代码 1 所示:首先我们创建一个 Server 对象, 并设置端口为 8080,然后为这个 Server 对象添加一个默认的 Handler。接着我们用配置文件 jetty.xml 对这个 server 进行设置,最后我们使用方法 server.start() 将 Server 启动起来就可以了。从这段代码可以看出,Jetty 是非常适合用于作为一个组件来嵌入到我们的应用程序当中的,这也是 Jetty 的一个非常重要的特点。

清单 1. 代码片断
public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc0/etc/jetty. xml"));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

接下来我们分析一下 Jetty Server 是如何启动的。首先我们注意到 Server 类,这个类实际上继承了 HttpServer, 当启动 Jetty 服务器的时候,就是说,在 Jetty 根目录下的命令行下如果输入 java -jar start.jar etc/jetty.xml,注意这里有一个配置文件 jetty.xml 做为运行参数,这个参数也可以是其它的配置文件,可以是多个 XML 配置文件,其实这个配置文件好比我们使用 Struts 时的 struts-config.xml 文件,将运行 Server 需要用到的组件写在里面,比如上一节中 HttpServer 的配置需要的组件类都可以写在这个配置文件中。按上述方法启动 Jetty Server 时,就会调用 Server 类里面的 main 方法,这个入口方法首先会构造一个 Server 类实例(其实也就构造了一个 HttpServer),创建实例的过程中就会构造 XmlConfiguration 类的对象来读取参数配置文件,之后再由这个配置文件产生的 XmlConfiguration 对象来配置这个 Server,配置过程其实是运用了 Java 的反射机制,调用 Server 的方法并传入配置文件中所写的参数来向这个 Server 添加 HttpListener,HttpContext,HttpHandler,以及 Web Application(对应于我们的 Web 应用)。

Jetty 的 Continuation 机制

讨论 Jetty 的 Continuation 机制,首先需要提到 Ajax 技术,Ajax 技术是当前开发 Web 应用的非常热门的技术,也是 Web 2.0 的一个重要的组成部分。 Ajax 技术中的一个核心对象是 XMLHttpRequest 对象,这个对象支持异步请求,所谓异步请求即是指当客户端发送一个请求到服务器的时候,客户端不必一直等待服务器的响应。这样就不会造成整个页面的刷新,给用户带来更好的体验。而当服务器端响应返回时,客户端利用一个 Javascript 函数对返回值进行处理,以更新页面上的部分元素的值。但很多时候这种异步事件只是在很小一部分的情况下才会发生,那么怎么保证一旦服务器端有了响应之后客户端马上就知道呢,我们有两种方法来解决这个问题,一是让浏览器每隔几秒请求服务器来获得更改,我们称之为轮询。二是服务器维持与浏览器的长时间的连接来传递数据,长连接的技术称之为 Comet

大家很容易就能发现轮询方式的主要缺点是产生了大量的传输浪费。因为可能大部分向服务器的请求是无效的,也就是说客户端等待发生的事件没有发生,如果有大量的客户端的话,那么这种网络传输的浪费是非常厉害的。特别是对于服务器端很久才更新的应用程序来讲,比如邮件程序,这种浪费就更是巨大了。并且对 Server 端处理请求的能力也相应提高了要求。如果很长时间才向 Server 端发送一次请求的话,那么客户端就不能的得到及时的响应。

如果使用 Comet 技术的话,客户端和服务器端必须保持一个长连接,一般情况下,服务器端每一个 Servlet 都会独占一个线程,这样就会使得服务器端有很多线程同时存在,这在客户端非常多的情况下也会对服务器端的处理能力带来很大的挑战。

Jetty 利用 Java 语言的非堵塞 I/O 技术来处理并发的大量连接。 Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求,下面我们通过一个简化的聊天程序的服务器端的代码来演示不使用 Continuation 机制和使用 Continuation 的差别。

清单 2. Continuation 机制
public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        Pe ople people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

大家注意到,首先获取一个 Continuation 对象,然后把它挂起 1 秒钟,直到超时或者中间被 resume 函数唤醒位置,这里需要解释的是,在调用完 suspend 函数之后,这个线程就可处理其他的请求了,这也就大大提高了程序的并发性,使得长连接能够获得非常好的扩展性。

如果我们不使用 Continuation 机制,那么程序就如 清单 3 所示:

清单 3. 不使用 Continuation 机制
public class Chat extends HttpServlet{    public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

大家注意到在等待事件发生的时间里,线程被挂起,直到所等待的事件发生为止,但在等待过程中,这个线程不能处理其他请求,这也就造成了在客户端非常多的情况下服务器的处理能力跟不上的情况。下面我们解释一下 Jetty 的 Continuation 的机制是如何工作的。

为了使用 Continuatins,Jetty 必须配置为使用它的 SelectChannelConnector 处理请求。这个 connector 构建在 java.nio API 之上,允许它维持每个连接开放而不用消耗一个线程。当使用 SelectChannelConnector 时,ContinuationSupport.getContinuation() 提供一个SelectChannelConnector.RetryContinuation 实例(但是,您必须针对 Continuation 接口编程)。当在 RetryContinuation 上调用suspend() 时,它抛出一个特殊的运行时异常 — RetryRequest,该异常传播到 servlet 外并且回溯到 filter 链,最后被SelectChannelConnector 捕获。但是不会发送一个异常响应给客户端,而是将请求维持在未决 Continuations 队列里,则 HTTP 连接保持开放。这样,用来服务请求的线程返回给 ThreadPool,然后又可以用来服务其他请求。暂停的请求停留在未决 Continuations 队列里直到指定的过期时间,或者在它的 Continuation 上调用 resume() 方法。当任何一个条件触发时,请求会重新提交给 servlet(通过 filter 链)。这样,整个请求被”重播”直到 RetryRequest 异常不再抛出,然后继续按正常情况执行。

Jetty 的安全性

为了防止任何人都有权限去关闭一个已经开启的 Jetty 服务器, 我们可以通过在启动 Jet ty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

总结

Jetty 是一个非常方便使用的 Web 服务器,它的特点在于非常小,很容易嵌入到我们的应用程序当中,而且针对 Web 2.0 的 Ajax 技术进行了特别的优化,这也使得我们的使用 Ajax 的应用程序可以拥有更好的性能。

原文地址:https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

特性简介

易用性

易用性是 Jetty 设计的基本原则,易用性主要体现在以下几个方面:

  1. 通过 XML 或者 API 来对 Jetty 进行配置;
  2. 默认配置可以满足大部分的需求;
  3. 将 Jetty 嵌入到应用程序当中只需要非常少的代码;

可扩展性

在使用了 Ajax 的 Web 2.0 的应用程序中,每个连接需要保持更长的时间,这样线程和内存的消耗量会急剧的增加。这就使得我们担心整个程序会因为单个组件陷入瓶颈而影响整个程序的性能。但是有了 Jetty:

  1. 即使在有大量服务请求的情况下,系统的性能也能保持在一个可以接受的状态。
  2. 利用 Continuation 机制来处理大量的用户请求以及时间比较长的连接。

另外 Jetty 设计了非常良好的接口,因此在 Jetty 的某种实现无法满足用户的需要时,用户可以非常方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。

易嵌入性

Jetty 设计之初就是作为一个优秀的组件来设计的,这也就意味着 Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。从某种程度上,你也可以把 Jetty 理解为一个嵌入式的Web服务器。

部署应用程序

将自己的应用程序部署到 Jetty 上面是非常简单的,首先将开发好的应用程序打成 WAR 包放到 Jetty 的 Webapps 目录下面。然后用如下的命令来启动 Jetty 服务器:Java –jar start.jar, 在启动服务器后。我们就可以访问我们的应用程序了,Jetty 的默认端口是 8080,WAR 的名字也就是我们的应用程序的 Root Context。例如一个典型的 URL 就是:http://127.0.0.1:8080/sample/index.jsp 。

如何将 Jetty 嵌入到程序当中

将 Jetty 嵌入到程序当中是非常简单的, 如 代码 1 所示:首先我们创建一个 Se rver 对象, 并设置端口为 8080,然后为这个 Server 对象添加一个默认的 Handler。接着我们用配置文件 jetty.xml 对这个 server 进行设置,最后我们使用方法 server.start() 将 Server 启动起来就可以了。从这段代码可以看出,Jetty 是非常适合用于作为一个组件来嵌入到我们的应用程序当中的,这也是 Jetty 的一个非常重要的特点。

清单 1. 代码片断
public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc0/etc/jetty.xml "));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

接下来我们分析一下 Jetty Server 是如何启动的。首先我们注意到 Server 类,这个类实际上继承了 HttpServer, 当启动 Jetty 服务器的时候,就是说,在 Jetty 根目录下的命令行下如果输入 java -jar start.jar etc/jetty.xml,注意这里有一个配置文件 jetty.xml 做为运行参数,这个参数也可以是其它的配置文件,可以是多个 XML 配置文件,其实这个配置文件好比我们使用 Struts 时的 struts-config.xml 文件,将运行 Server 需要用到的组件写在里面,比如上一节中 HttpServer 的配置需要的组件类都可以写在这个配置文件中。按上述方法启动 Jetty Server 时,就会调用 Server 类里面的 main 方法,这个入口方法首先会构造一个 Server 类实例(其实也就构造了一个 HttpServer),创建实例的过程中就会构造 XmlConfiguration 类的对象来读取参数配置文件,之后再由这个配置文件产生的 XmlConfiguration 对象来配置这个 Server,配置过程其实是运用了 Java 的反射机制,调用 Server 的方法并传入配置文件中所写的参数来向这个 Server 添加 HttpListener,HttpContext,HttpHandler,以及 Web Application(对应于我们的 Web 应用)。

Jetty 的 Continuation 机制

讨论 Jetty 的 Continuation 机制,首先需要提到 Ajax 技术,Ajax 技术是当前开发 Web 应用的非常热门的技术,也是 Web 2.0 的一个重要的组成部分。 Ajax 技术中的一个核心对象是 XMLHttpRequest 对象,这个对象支持异步请求,所谓异步请求即是指当客户端发送一个请求到服务器的时候,客户端不必一直等待服务器的响应。这样就不会造成整个页面的刷新,给用户带来更好的体验。而当服务器端响应返回时,客户端利用一个 Javascript 函数对返回值进行处理,以更新页面上的部分元素的值。但很多时候这种异步事件只是在很小一部分的情况下才会发生,那么怎么保证一旦服务器端有了响应之后客户端马上就知道呢,我们有两种方法来解决这个问题,一是让浏览器每隔几秒请求服务器来获得更改,我们称之为轮询。二是服务器维持与浏览器的长时间的连接来传递数据,长连接的技术称之为 Comet

大家很容易就能发现轮询方式的主要缺点是产生了大量的传输浪费。因为可能大部分向服务器的请求是无效的,也就是说客户端等待发生的事件没有发生,如果有大量的客户端的话,那么这种网络传输的浪费是非常厉害的。特别是对于服务器端很久才更新的应用程序来讲,比如邮件程序,这种浪费就更是巨大了。并且对 Server 端处理请求的能力也相应提高了要求。如果很长时间才向 Server 端发送一次请求的话,那么客户端就不能的得到及时的响应。

如果使用 Comet 技术的话,客户端和服务器端必须保持一个长连接,一般情况下,服务器端每一个 Servlet 都会独占一个线程,这样就会使得服务器端有很多线程同时存在,这在客户端非常多的情况下也会对服务器端的处理能力带来很大的挑战。

Jetty 利用 Java 语言的非堵塞 I/O 技术来处理并发的大量连接。 Jetty 有一个处理长连接的机制:一个被称为 Continuations 的特性。利用 Continuation 机制,Jetty 可以使得一个线程能够用来同时处理多个从客户端发送过来的异步请求,下面我们通过一个简化的聊天程序的服务器端的代码来演示不使用 Continuation 机制和使用 Continuation 的差别。

清单 2. Continuation 机制
public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        Peopl e people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

大家注意到,首先获取一个 Continuation 对象,然后把它挂起 1 秒钟,直到超时或者中间被 resume 函数唤醒位置,这里需要解释的是,在调用完 suspend 函数之后,这个线程就可处理其他的请求了,这也就大大提高了程序的并发性,使得长连接能够获得非常好的扩展性。

如果我们不使用 Continuation 机制,那么程序就如 清单 3 所示:

清单 3. 不使用 Continuation 机制
public class Chat extends HttpServlet{    public void doPost(HttpServletRequest request, Htt pServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

大家注意到在等待事件发生的时间里,线程被挂起,直到所等待的事件发生为止,但在等待过程中,这个线程不能处理其他请求,这也就造成了在客户端非常多的情况下服务器的处理能力跟不上的情况。下面我们解释一下 Jetty 的 Continuation 的机制是如何工作的。

为了使用 Continuatins,Jetty 必须配置为使用它的 SelectChannelConnector 处理请求。这个 connector 构建在 java.nio API 之上,允许它维持每个连接开放而不用消耗一个线程。当使用 SelectChannelConnector 时,ContinuationSupport.getContinuation() 提供一个SelectChannelConnector.RetryContinuation 实例(但是,您必须针对 Continuation 接口编程)。当在 RetryContinuation 上调用suspend() 时,它抛出一个特殊的运行时异常 — RetryRequest,该异常传播到 servlet 外并且回溯到 filter 链,最后被SelectChannelConnector 捕获。但是不会发送一个异常响应给客户端,而是将请求维持在未决 Continuations 队列里,则 HTTP 连接保持开放。这样,用来服务请求的线程返回给 ThreadPool,然后又可以用来服务其他请求。暂停的请求停留在未决 Continuations 队列里直到指定的过期时间,或者在它的 Continuation 上调用 resume() 方法。当任何一个条件触发时,请求会重新提交给 servlet(通过 filter 链)。这样,整个请求被”重播”直到 RetryRequest 异常不再抛出,然后继续按正常情况执行。

Jetty 的安全性

为了防止任何人都有权限去关闭一个已经开启的 Jetty 服务器, 我们可以通过在启动 Jetty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

总结

Jetty 是一个非常方便使用的 Web 服务器,它的特点在于非常小,很容易嵌入到我们的应用程序当中,而且针对 Web 2.0 的 Ajax 技术进行了特别的优化,这也使得我们的使用 Ajax 的应用程序可以拥有更好的性能。

原文地址:https://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

public class JettyServer {    public static void main(String[] args) {        Server server = new Server(8080);        server.setHandler(new DefaultHandler());        XmlConfiguration configuration = null;        try {            configuration = new XmlConfiguration(                new FileInputStream("C:/development/Jetty/jetty-6.1.6rc0/etc/jetty.xml"));        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (SAXException e1) {            e1.printStackTrace();        } catch (IOException e1) {            e1.printStackTrace();        }                try {                configuration.configure(server);            server.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

public class ChatContinuation extends HttpServlet{        public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        if (!people.hasEvent())        {            Continuation continuation =                 ContinuationSupport.getContinuation(request, this);            people.setContinuation(continuation);            continuation.suspend(1000);        }        people.setContinuation(null);        people.sendEvent(response);    }}

public class Chat extends HttpServlet{    public void doPost(HttpServletRequest request, HttpServletResponse response){        postMessage(request, response);    }        private void postMessage(HttpServletRequest request, HttpServletResponse response)    {        HttpSession session = request.getSession(true);        People people = (People)session.getAttribute(session.getId());        while (!people.hasEvent())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        people.setContinuation(null);        people.sendEvent(response);        }}

java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar

Leave a Comment

Your email address will not be published.