Flash through the Xmlsocket Monitoring production system (3-1)

Source: Internet
Author: User
Tags flush thread thread class client
xml| Monitoring

Constantly sending different types of meaningful XML data to the client to simulate the different states of the production system. Given the simplicity of the function, the server only sends a random type of data to the client every 2 seconds, instead of including a timely state of the analog production system, such as when an error occurs at a certain moment, sending a data indicating the type of error, in actual design programming, based on the examples in this article, you can easily extend the functionality you need to implement.

public void Run () {

try {

while (true) {

System.out.println (This.getname () + "------" + i++);
D = new Date ();

if (new Random (). Nextint () *100 < 50) {

str = "< message type=\" error\ "text=\" Hello, the My name is morgan!\ "time=\" "+ d.tostring () +" "/>";
} else {

str = "";
}

Byte[] s= str.getbytes ();

Out.write (s);
Out.write (0);
Out.flush ();

This.sleep (2000);
}

catch (Exception ex) {

Ex.printstacktrace ();
}
}

The above statement is the custom thread class Serverthread the Run () method, first uses the random number condition if statement, randomly produces the different type XML data, then uses the OutputStream output stream object, outputs the XML data byte array data, in order to indicate that the data has been sent completes , and then sends a 0 byte, then the Flush () method that calls the output stream empties the cache so that the output stream sends the data immediately. Finally, the thread is paused for 2 seconds, and the above steps are recycled when the thread executes again.

This allows the client to receive a random type of XML data every 2 seconds. See the source code of the program included in this article.

On the server side, for simplicity, just randomly to two types of data (normal type and error type):

< message type= "error" text= "Data Content" time= "+ system time +/>"
< message type= "normal" text= "Data Content" time= "+ system time +/>"



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.