Whether you do the Web backend or app backend, or SOA service, long connection is a good choice, on the one hand to save the resource consumption of establishing a connection each time, on the other hand, can make the message timely response, improve the experience.
Here is a way to implement a long connection via Nginx module, which is a long connection on the HTTP protocol, strictly speaking the HTTP protocol itself is request-response, and there is no strict meaning of long connection, so-called long-term connection is when there is no corresponding time, can hold, Until there is a corresponding, then reestablish the connection immediately.
Here's how to implement it.
1, first download nginx_http_push_module and Nginx, on these two tar files;
2. Copy the two tar files to the Linux system and execute them under the Nginx directory:
./configure--add-module=path/to/nginx_http_push_module ... makemake install
3, the middle may appear can not find the Pcre module, etc., if you are a CentOS system, use yum-y install Pcre-devel OpenSSL openssl-devel installed
After installation, continue to perform Nginx installation
4, such as Nginx are installed after the configuration of long connection:
nginx.conf file in/use/local/nginx/conf
Add to:
location/publish { set $push _channel_id $arg _id; Push_publisher; Push_store_messages on; Push_message_timeout 2h; Push_max_message_buffer_length 10; } location/activity { push_subscriber; Set $push _channel_id $arg _id; Push_subscriber_concurrency broadcast; Default_type Text/plain; }
5, restart Ngnix, Access http://Your IP port/activity?id= your channel, if the browser has been waiting,
Then, you write a piece of code to publish a message, if the browser can accept that the installation is successful!
public void Testnginx () { String http = "Http://172.16.4.108/publish?id=my"; Postmethod Postmethod = new Postmethod (HTTP); Requestentity requestentity = new Stringrequestentity ("444"); Postmethod.setrequestentity (requestentity); try{ int status =this.client.executemethod (Postmethod); if (status = = Httpstatus.sc_ok) { String text = postmethod.getresponsebodyasstring (); System.out.println (text); } } catch (Exception e) { e.printstacktrace (); } }
6, the above are installed after the start of our own logic
Here is the listener, here is a simple implementation, we need to always record a lastmodified on the listener, this time represents the last time he received a new message
private static String ETag = ""; private static String lastmodified = ""; public static void Main (string[] args) {while (true) {HttpClient HttpClient = new HttpClient (); String http = "Http://172.16.4.108/activity?id=my"; GetMethod GetMethod = new GetMethod (HTTP); Getmethod.setrequestheader ("Connection", "keep-alive"); Getmethod.setrequestheader ("If-none-match", ETag); Getmethod.setrequestheader ("If-modified-since", lastmodified); try {int status = Httpclient.executemethod (GetMethod); if (Getmethod.getresponseheader ("etag") = null) {ETag = Getmethod.getresponseheader ("ETag"). GetValue () ; } if (Getmethod.getresponseheader ("last-modified")! = null) {lastmodified = getmethod.ge Tresponseheader ("Last-modified"). GetValue (); } System.out.println ("etag=" + ETag + "; lastmodif=" + LastModified + "; status=" + status); if (status = = HTTPSTATUS.SC_OK) {String text = getmethod.getresponsebodyasstring (); System.out.println (text); }} catch (Exception e) {e.printstacktrace (); } } }
Here is the Send message end: The same code we used when we tested it.
public void Testnginx () { String http = "Http://172.16.4.108/publish?id=my"; Postmethod Postmethod = new Postmethod (HTTP); Requestentity requestentity = new Stringrequestentity ("444"); Postmethod.setrequestentity (requestentity); try{ int status =this.client.executemethod (Postmethod); if (status = = Httpstatus.sc_ok) { String text = postmethod.getresponsebodyasstring (); System.out.println (text); } } catch (Exception e) { e.printstacktrace (); } }
Here, our plan is finished.
Nginx for long-connected applications