Jetty (embedded) use and test __jetty

Source: Internet
Author: User

Use 9.2.x. Because this version is JDK7, 9.3 corresponds to the JDK8.
Running jetty in embedded mode means putting an HTTP module into your application instead of applying it to an HTTP server. 1 steps to develop an embedded server

1 Create a server instance;
2) Add/configure connectors;
3 Add/configure handlers and/or contexts and/or servlets;
4) Start server;
5 wait on the server or use your thread to do something else.

Connector is responsible for receiving HTTP requests from clients, and the processing of requests is done by handler. 2 main code location: Handler

Processing logic is done in handler, which is where your main code is located.
To generate a response to a request, jetty requires you to set up a handler on the server. A handler can:
1 Test/modify HTTP request;
2) to produce a complete HTTP response;
3) Call another handler (see Handlerwrapper);
4 Select one or more handlers calls (see Handlercollection).

Its core code example:

public void handle (String target, request baserequest, HttpServletRequest request, httpservletresponse response) throws I Oexception, servletexception  
{  
        //response.setcontenttype ("Text/plain;charset=utf-8");
        Response.getwriter (). println (New Jsonobject (params));
        Response.setcontenttype ("Text/html;charset=utf-8");  
        Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);  
        Baserequest.sethandled (TRUE); This handler marks the request as handled.

        Response.getwriter (). println ("

When customizing the handler, you must implement this method, you can output the response directly, or you can do some packaging to the request and give it to the next handler.
Handler's powerful place is the ability to set several handler for jetty server, each handler complete its own function.
  
The parameters passed to the handle method are:
1 target: The destination of the request, a URI, or a name from the specified distributor;
2 Baserequest:jetty variable Request object, always be disassembled;
3 Requests: Unchanged request object, can be a filter or servlet packaging;
4) Response: response, can be packaged by a filter or servlet.

Handler sets the response state, Content-type, and sets the request to handled before using writer to produce a response (indicating that processing is complete and no longer passes to the next handler processing).

handler collections and wrappers
1) handlercollection
Contains a collection of other handler, calling each handler sequentially (even if a handler marks the request as handled, that is, sethandled (true));.
2) Handlerlist
A handler collection that calls each handler in turn until either an exception is generated, or the response is committed, or request.ishandled () returns True.
3) Handlerwrapper
A handler base class, you can use the linked handler in the aspect-oriented programming style. For example, a standard Web application is implemented through a chain of context, sessions, security, and servlet handlers.
4) Contexthandlercollection
A special handlercollection that uses the longest prefix of the request URI (ContextPath) to select a contained contexthandler to process the request.

Processing of multiple handler:

    public static void Main (string[] args) throws Exception  
    {  
        Server server = new server (8081);  
        Handlercollection HC =new handlercollection ();  
        Hc.sethandlers (New Handler[]{new Hellohandler (), New Hellohandlerscond ()});  
        Server.sethandler (hc);  
        Server.start ();  
        Server.join ();  
    }  
3 Connectors

The code above, a default connector instance is created inside the server to listen for requests for that port. However, we often want to be able to initialize and configure one or more connectors directly for the server instance.

public static void Main (string[] args) throws Exception  
    {  
        //the server server server  
        = new server ();  

        HTTP Connector  
        serverconnector http = new Serverconnector (server);  
        Http.sethost ("localhost");  
        Http.setport (8080);  
        Http.setidletimeout (30000);  

        Set the connector  
        server.addconnector (HTTP);  

        Set a handler  
        Server.sethandler (New Hellohandler ());  

        Start the server  
        server.start ();  
        Server.join ();  
    }  

You can also set up multiple connectors as needed, such as one to handle HTTP and one to process HTTPS. 4 Embedding Servlets

The servlet can be seen as similar to Jetty Handler, except that the request object is immutable.
The servlet is processed by a servlethandler in the jetty.
It uses a standard path-matching method to match a servlet to a request.

public static void Main (string[] args) throws Exception  
    {  
        Server server = new server (8080);  

        Servlethandler handler = new Servlethandler ();  
        Server.sethandler (handler);  

HelloServlet is the program you write in the servlet syntax.   handler.addservletwithmapping (Helloservlet.class, "/*");  

        Server.start ();  
        Server.join ();  
    }  
5 The ability to respond to validation programs

Write one of the simplest programs to do high concurrent access stress tests.
The procedure is as follows:

public class Restserver {public static void main (string[] args) throws Exception {Server server = new S

        Erver ();
        Serverconnector connector = new Serverconnector (server);
        Http.sethost ("localhost");
        Connector.setport (8080);
        Connector.setidletimeout (30000);

        Server.setconnectors (new connector[] {Connector});

        Server.sethandler (New Hellohandler ());
        Server.start ();
    Server.join (); }//-------Hellohandler---------public class Hellohandler extends Abstracthandler {@Override public void H Andle (String target, request baserequest, HttpServletRequest request, httpservletresponse response) throws IOE Xception, servletexception {System.out.print ("target=" + target + ", baserequest=" + Baserequest.getquerystri
        Ng ()); Response.setcontenttype ("Text/plain;
        Charset=utf-8 ");

        Response.setstatus (HTTPSERVLETRESPONSE.SC_OK); String xxx = Request.getparameter ("xxx");

        System.out.println (", xxx=" + xxx);
        PrintWriter out = Response.getwriter (); Out.println ("Hello, Jetty.") I'm coming.
        ");
    Baserequest.sethandled (TRUE); }

}
Use Apache AB to do stress testing:
Is the httpd with an executable file.
Ab-n 100000-c HTTP://LOCALHOST:8080/AB?XXX=A1

Common parameters for AB:
-N: Total number of request executions, default is 1;
-C: Concurrent number, default is 1;
-T: Total time of the test, in seconds, by default 50000s
Data files when-p:post
-W: Output results in HTML table format

If you want to submit data by post, add the following:
-P d:/tmp/postfile.txt-t "application/x-www-form-urlencoded"
Postfile.txt's content is the standard post data format, such as: name=xxx&age=123.

After the completion of the test, look at the results of the report, pay attention to several data:
1) Failed requests. How much has it failed?
2) Requests per second. Response capability
3 time per request. Response time for a request

Because Apache does not provide Windows version of the running program download, you need to go to Apachehaus or xampp and other third-party software to find. This can be found on the official website of Apache HTTP. On the httpd download page, "for Microsoft Windows" is on the link page.
To Apachehaus, download the corresponding version of the file, such as the corresponding WIN10 package: Apache 2.4 VC14. After the decompression, to the bin directory can find Ab.exe.
  
-Use Jstat monitoring performance (GC, etc.), jmap monitoring memory

JPS  //Find the jetty program pid
jstat-gcutil PID 5000//Start monitoring. Display Jmap-histo PID every 5 seconds (5000 milliseconds)
| less//Print the number of instances per class, memory footprint, class full name information

Jstat, generally use-gcutil to view GC conditions:
s0c, s1c, s0u, S1u:survivor 0/1 area Capacity (Capacity) and usage (Used)
EC, Eu:eden area capacity and usage
OC, OU: Older generation capacity and usage
PC, PU: Permanent generation capacity and usage
YGC, YGT: Young generation GC and GC time consuming
FGC, Fgct:full GC times and full GC time consuming
GCT:GC Total time consuming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.