The bio, NIO, Apr mode settings for Tomcat on Linux servers

Source: Internet
Author: User
Tags tomcat server java se

I. BIO, NIO, AIO

Learn Four Concepts first:

Sync: Personally take the bank card to the bank to withdraw money (when using sync io, Java handles IO read and write itself).

Asynchronous: Entrust a little brother to take the bank card to the bank to withdraw money, and then give you (using asynchronous IO, Java will read and write IO to the OS processing, the data buffer address and size need to pass to the OS (bank card and password), the OS needs to support the asynchronous IO Operation API).

Blocking: ATM queues up and withdrawals, you can only wait (when using blocking IO, the Java call is blocked until read-write is completed before returning).

Non-blocking: counter withdrawals, take a number, and then sit in the chair to do other things, the equal sign broadcast will notify you to handle, not to the number you can not go, you may keep asking the lobby manager row to no, lobby manager if it is not yet you can not go (using non-blocking IO, if not read and write Java call will return When the IO event dispatcher notifies the reader to read and write, continue to read and write, continuously looping until read-write is completed.

Java support for bio, NIO, AIO:

Java BIO: Synchronization and blocking, the server implementation mode for a connection to a thread, that is, the client has a connection request when the server needs to start a thread to process, if the connection does not do anything will cause unnecessary thread overhead, of course, can be improved through the thread pool mechanism.

Java NIO: Synchronous non-blocking, the server implementation mode is a request for a thread, that is, the connection request sent by the client is registered on the multiplexer, and the multiplexer polls to the connection with an I/O request to start a thread for processing.

Java AIO (nio.2): Asynchronous non-blocking, server implementation mode for a valid request for a thread, the client I/O requests are completed by the OS before notifying the server application to start the thread for processing

BIO, NIO, AIO application Scenario analysis:

The bio method is suitable for a small and fixed number of connections, which requires a high level of server resources, and is limited to applications, JDK1.4 the only choice before, but the program is intuitive and easy to understand.

The NIO approach is suitable for architectures with a large number of connections and short (light-operated) connections, such as chat servers, which are limited to applications, and are more complex to program, and JDK1.4 begin to support.

AIO mode allows for a number of connections and long-connected (re-operation) of the architecture, such as the album server, full call to the OS to participate in concurrent operations, programming more complex, JDK7 began to support.

Ii. Introduction to Tomcat three models

BIO
BIO (blocking I/O), as the name implies, is a blocking I/O operation that indicates that Tomcat is using traditional Java I/O operations (that is, the Java.io package and its sub-packages). Tomcat, by default, is run in bio mode. Unfortunately, in general, the Bio mode is the lowest performance in three operating modes. We can view the current state of the server through Tomcat Manager.

NIO
is a new I/O operation (i.e. the Java.nio package and its child packages) provided by Java SE 1.4 and subsequent releases. Java NiO is a buffer-based, Java API that provides non-blocking I/O operations, so NiO is also considered an abbreviation for non-blocking I/O. It has better concurrency performance than traditional I/O operations (bio).

APR
(Apache portable Runtime/apache Portable runtime), which is the support library for Apache HTTP servers. You can simply understand that Tomcat will invoke the core dynamic link Library of the Apache HTTP server in the form of JNI to handle file reads or network transfer operations, thus greatly improving the performance of Tomcat's handling of static files. Tomcat APR is also the preferred mode for running high concurrent applications on Tomcat.

Three, tomcat three mode performance comparison

Here I quoted the test results given by netizens

Four, Tomcat mode settings

What I'm demonstrating here is TOMCAT7, which is the bio mode by default. And TOMCAT8 is the default NiO mode.

Prior to this, we configured the Tomcat management interface so that it would be easier to observe our bio, NIO, Apr mode (if you don't want to set this up, you can also view Tomcat's log files)

Pull to the bottom, copy the contents of the red box, paste outside the comment, add the manager/status user (please do not change the user name here, change it as if it is not)

After configuring the reboot Tomcat, the Tomcat status can be seen through ip:port/manager/status, with server information and Tomcat information.

Down is the configuration steps of three modes: Bio Mode

Tomcat7 default is. If you are tomcat8 or 9 want to set into bio mode, then change here

Find the port=8080 port setting, as long as the red box is the same, restart Tomcat will use the Bio mode. TOMCAT7 The default is this way

    <port= "8080"  protocol= "http/1.1"               connectiontimeout  = "20000"               redirectport= "8443"/>
NiO mode

In the same position as setting the bio mode, the contents of the red box are changed so that rebooting Tomcat will use NiO mode. Tomcat8 The default is this way

    <port= "8080"  protocol= " Org.apache.coyote.http11.Http11NioProtocol "               connectiontimeout=" 20000 "                Redirectport= "8443"/>
Apr Mode

Enabling this mode is a bit cumbersome and requires some dependent libraries to be installed, and the following are the required conditions for installation:

1, the latest APR

2, the newest Apr-util

3,tomcat-native.tar.gz (with the appropriate installation of the TAR package under tomcat/bin/)

But also need to change the configuration file, first to change the configuration file, or in the same location, the contents of the red box into this (note and the NIO pattern very much like), save exit, and then go to install the dependent library

    <port= "8080"  protocol= " Org.apache.coyote.http11.Http11AprProtocol "               connectiontimeout=" 20000 "                Redirectport= "8443"/>

Before installing those three dependencies, see if the server has an APR

I've been faking it (as if Tomcat was faking it, I don't remember it), so uninstall it, ignore the dependency uninstall, reload the latest

1.apr(http://apr.apache.org/download.cgi), upload unzip

Install to/USR/LOCAL/APR

./configure--prefix=/usr/local/apr && make && make install

2.apr-util(http://apr.apache.org/download.cgi), upload unzip

Install to/usr/local/apr-util, specify the APR directory

./configure--with-apr=/usr/local/apr/--prefix=/usr/local/apr-util && make && make install

3.tomcat-native(with the appropriate installation of the TAR package under tomcat/bin/), unzip

Install, specify the APR directory and the Java_home directory

./tomcat-native-1.2.14-src/native/configure--WITH-APR=/USR/LOCAL/APR--with-java-home=/usr/share/jdk1.8 & & Make && make install

After installation, remember to add an APR environment variable after the JAVA_HOME environment variable in the/etc/profile file (note the APR directory you installed yourself)

Export Ld_library_path= $LD _library_path:/usr/local/apr/lib

Use the Source/etc/profile command to make the environment variable configuration effective immediately

Start Tomcat, Access (IP of your tomcat server) 192.168.25.128:8080/manager/status, so it's OK.

If you do not want to set up the Tomcat management interface above, you can also view it through the log

The bio, NIO, Apr mode settings for Tomcat on Linux servers

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.