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 +/>"