How to implement _java of web container design

Source: Internet
Author: User
Tags exception handling session id response code sessions server memory

Design of web Container

Developing a Web container involves many different facets of technology, for example, communication layer knowledge, program language level of knowledge, etc., and an available Web container is a relatively large system, to say that it takes a long time, this article aims to introduce how to design a Web container, only to explore the idea of implementation, Does not involve too many concrete implementations. Divide it into modules and components, each component module is responsible for different functions, the following figure lists some of the basic components, and each component will be introduced.

Connection Receiver

The primary responsibility is to listen for a client socket connection and receive a socket, and then leave the socket to the task executor (thread pool) to perform. Continuously read the socket from the bottom of the system, do as little processing as possible, and then throw it into the thread pool. Why stress the need to do as little processing as possible? This is related to system performance problems, and too much processing can seriously affect throughput. Because there is generally only one receiver (one thread is responsible for socket reception), the length of time it takes to process each receive is likely to have an impact on overall performance. So the work of the receiver is very small and simple, only maintain a few state variables, flow control gate accumulation operation, ServerSocket receive operation, set the received socket some properties, will receive the socket into the thread pool and some exception handling. Other logic that requires longer processing is given to the thread pool, such as reading the underlying data of the socket, parsing the HTTP protocol message, and responding to the client's actions, and so on.

Connection Number Controller

For a single machine, the total flow of the access request is peak and the server has physical limit, in order to ensure that the Web server is not washed out we need to take some measures to protect the prevention, need to explain a little bit more of the traffic here refers to the number of sockets, by controlling the number of sockets connected to control traffic. One of the effective methods is to take the flow control, it is like in the flow of the entrance to add a gate, the size of the gate determines the size of the flow, once the maximum flow will close the gate stop to receive until there is a free channel. Counters can be implemented using JDK's AQS framework.

Socket Factory

Different use situations may require different levels of security, such as the payment of related transactions must be encrypted and then sent to the information, which also involves the process of key negotiation, and in other ordinary occasions, the need to encrypt the message. The response to the application layer is the issue of using HTTP and HTTPS.
Simply speaking, TLS\SSL protocol provides authentication service for each communication ①, and authenticates the legality of this session entity identity. ② provides cryptographic services, and strong encryption mechanisms ensure that messages in the communication process are not deciphered. ③ provides tamper-proof service, uses the hash algorithm to sign the message, and verifies that the communication content is not tampered with by verifying the signature.
The HTTP protocol corresponds to the socket, and HTTPS corresponds to the sslsocket. How to generate sockets and sslsocket is referred to the socket factory.

Task Definition--task

Define the tasks that need to be performed to tell the thread pool what tasks to perform. The task is divided into three points: processing the socket and responding to the client, the number of connections counter minus one, close the socket. The socket processing is the most important and the most complex, it includes the bottom socket byte stream reading, HTTP protocol Request message parsing (Request line, request header, request body, etc.), according to the request line resolution to find the appropriate host Web project resources, According to the processing result, the HTTP protocol response message is assembled to the client.

Task Executor

A thread pool with the maximum minimum number of threads is called the "task executor" because the thread pool can be seen as starting a number of threads to continuously detect a task queue, and when it is found that a task needs to be performed. Maximum minimum number of threads, redundant thread reclaim time limit, reject actions made by thread pool when the maximum number of threads is exceeded, and so on.

Message reading

Used to read messages from the client at the bottom of the operating system and provide buffering mechanisms. Messages are copied to desbuf.

Message output

Used to write messages processed by the Web container to the underlying operating system and provide buffering mechanisms. Writes the message outputbuf through a buffer to the operating system.

Input filter

In this reading process, you want to do some additional processing, and these additional processing may be based on different conditions to do different processing, considering the program decoupling and expansion, and then introduce filters. Through a layer of filters to complete the filtering operation to Desbuf, the process is like being joined a road to handle the checkpoint, through the checkpoints will be carried out to operate, and finally complete the source data to the destination data operation.

Output filter

Similar to the input filter function, used when the message is output.

Message parser

Provides the ability to parse parts of the HTTP protocol.

Request Builder

According to the object-oriented idea, the attributes and protocol fields related to request in each request process are abstracted into one request object. Includes the request line, the request header, the request body three parts information, what value is needed in the process to obtain directly from the request object. facilitates the implementation of the servlet standard.


Response Builder

Corresponds to the request, a Response object builder is required. This includes response lines, response headers, and response body information, which can be set directly into the response object in the processing result correlation value. facilitates the implementation of the servlet standard.

Address Mapper

The address mapper is the router that requests the individual Web projects and individual resources. A requested access is exported to the requesting client based on the resource that the path is mapped to find the response.

Life cycle

For further modularity, the entire container has many components that may need to do different things at different times and need a lifecycle to unify all components. For example, all components start, stop, shutdown and so on are separated from the lifecycle of unified management, you can easily manage the life cycle of these components. Want to do something before something happens? Adding a life cycle listener can be done gracefully.

JMX Manager

System running status monitoring and management, server performance, server-related parameter collection, JVM load, Web Connection number, thread pool, database connection pool, cache management, configuration file reload and so on. can provide some remote visualization management, high real-time. It also provides a solution for the management of distributed systems.

Web Loader

Webloader is used to load Web application projects, a Web container may contain several web applications. To achieve the isolation of Lib and servlet, use different ClassLoader ClassLoader for each Web application, and these classloader are not parent-child relationships to achieve the class isolation effect that a Web application's lib will not be used by other Web applications.

Session Manager

Session Manager mainly manages sessions, including: ① generation SessionID, general cookies or URLs without jsessionid values are considered nonexistent sessions and need to be regenerated SessionID used as session IDs. ② Many client sessions are saved in the server, and periodic cleanup is needed for timed sessions to ensure that server memory is not wasted. ③ for some important sessions can be persisted to disk and can be reloaded into memory when needed.

Run log

Records some warnings, exceptions, and errors at run time.

Access log

The access log generally records the client's access-related information, including client IP, request time, request protocol, request method, number of bytes requested, response code, session ID, processing time, and so on. Access logs can be used to count the number of access users, the distribution of access time, and so on, which can help companies make decisions on operational strategies.

Security Manager

Web projects run on the Web container platform, which is like embedding an application on a platform to run, so that the embedded program can run properly, first the platform to be safe and normal operation. and to maximize the platform is not affected by embedded applications, the two to a certain extent to achieve the effect of isolation. The policy file is specified by-djava.security.manager-djava.security.policy==web.policy at startup, and this file defines various permissions.

Operation Monitoring & Remote Management

Provides a platform for real-time monitoring of the running state of a web container and can be managed remotely.

Cluster

Clusters generally have two kinds: ① load-balanced cluster, generally through a certain distribution algorithm to distribute the access flow evenly into the cluster inside each machine for processing. ② high Availability cluster, trunking communications to connect a number of machines, this cluster is more emphasis on the cluster when a machine failure can be automated switching or traffic transfer measures to ensure the external availability of the entire cluster.
Web general requests are stateless, you can do the cluster directly, but the session is a stateful, need to use trunking communication technology for session copy. Related technologies include multicast and unicast.

Servlet engine

The servlet engine uses reflection to generate objects from the servlet and JSP in the Web application and into the Servlet object pool, and to invoke the appropriate method according to the actual call. Web applications Place business logic processing in Dopost or Doget methods, and the Web container processes the request with the processing logic defined here to handle the response client.

JSP compiler

In accordance with the specification JSP is ultimately compiled into a servlet execution, so to follow the specifications of the JSP file compiled. JSP compiler is actually the JSP syntax translation, according to the JSP syntax processing.

A Web container basically contains the functionality of the components described above and can be built into a Web container that allows your web to run, based on each component module.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.