Comet4j Java Server pushes messages to web page instances

Source: Internet
Author: User

For page monitoring, Ajax requests are used in the past, but small concurrency is not a big problem, but it is not suitable for large concurrency, if you don't want to write the thread to manipulate it, you can find some plug-ins, such as comet4j.

Next I will demonstrate how to use this plug-in

Prepare the required tools first:

Comet4j-tomcat6.jar (tomcat6 just import this)

Comet4j-tomcat7.jar (import this for Tomcat 7)

Comet4j. js (introduce this JS page)

For more information, see the following.

Then write a class.

Package COM. shadow. extras. comet4j; import javax. servlet. servletcontextevent; import javax. servlet. servletcontextlistener; import Org. comet4j. core. cometcontext; import Org. comet4j. core. cometengine; public class testcomet implements servletcontextlistener {Private Static final string channel = "test"; Private Static final string channel2 = "Test2"; Public void contextinitialized (servletcontextevent arg0) {cometcontext cc = cometcontext. getinstance (); CC. registchannel (Channel); // register the application's channelcc. registchannel (channel2); thread helloappmodule = new thread (New helloappmodule (), "sender app module"); // whether to start helloappmodule. setdaemon (true); // the startup thread helloappmodule. start (); thread helloappmodule2 = new thread (New helloappmodule2 (), "sender app module"); // whether to start helloappmodule2.setdaemon (true); // start thread helloappmodule2.start ();} class helloappmodule2 implements runnable {public void run () {While (true) {try {// sleeping time thread. sleep (5000);} catch (exception ex) {ex. printstacktrace ();} cometengine engine = cometcontext. getinstance (). getengine (); // get the message content long l = getfreememory (); // start sending the engine. sendtoall (channel2, L) ;}} class helloappmodule implements runnable {public void run () {While (true) {try {// sleep time thread. sleep (2000);} catch (exception ex) {ex. printstacktrace ();} cometengine engine = cometcontext. getinstance (). getengine (); // get the message content long l = getfreememory (); // start sending the engine. sendtoall (Channel, L) ;}} public void contextdestroyed (servletcontextevent arg0) {} public long getfreememory () {return runtime. getruntime (). freememory ()/1024 ;}}

Then write a page.

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Then configure web. xml.

<! -- Comet4j --> <listener> <description> comet4j container listening </description> <listener-class> Org. comet4j. core. cometapplistener </listener-class> </listener> <servlet> <description> comet connection [Default: Org. comet4j. core. cometservlet] </description> <display-Name> cometservlet </display-Name> <servlet-Name> cometservlet </servlet-Name> <servlet-class> Org. comet4j. core. cometservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> cometservlet </servlet-Name> <URL-pattern>/comet </url-Pattern> </servlet-mapping> <listener> <description> testcomet </description> <listener-class> COM. shadow. extras. comet4j. testcomet </listener-class> </listener>

Finally, modify the Tomcat server. xml file.

Change the Protocol parameter value to the following because it is a plug-in developed based on NiO.

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>

Test, it is easy to refer to the test.html we just created, and then we can see that the memory value has been automatically refreshed and fluctuating.

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.