6. Request log
The request log stores the request history.Log File)Is permanently stored in the computer. Many systems provide log files, such as Windows Log Files and Oracle log files. log files can record user operations on the system (such as data changes ). The request log file provides many functions. Common functions are as follows:
(1) If the system fails, the log file can provide a recovery mechanism for the system. In the request log file, you can record each step of the system's operations, so that the system can be smoothly restored to a specific State;
(2) request logs can also be used for batch processing. In a request log file, you can store a series of command objects, such as a command queue;
(3) All command objects in the command queue can be stored in one log file. Each Command is executed to delete a corresponding command object from the log file, to prevent request loss due to power failure or system restart, and to avoid repeated execution of some commands when all requests are ressent, you only need to read the request log file, continue to execute the remaining commands in the file.
When implementing the request log, we canSerialize a command object to a log file.In this case, the command class must implement the java. Io. serializable interface. The following uses a simple example to describe the usage of the log file and how to implement the request log:
Sunny software developed a website configuration file management tool that allows you to add, delete, modify, and perform other operations on the website configuration file through a visual interface. The tool is designed in command mode, as shown in structure 6: Figure 6 website configuration file management tool structure At present, sunny software developers want to record the operation requests to the configuration file in the log file. If the website is redeployed, they only need to execute the command object saved in the log file to modify the configuration file. |
The complete code of this instance is as follows:
Import Java. io. *; import Java. util. *; // abstract command class. Because the command object needs to be written to a file, it implements the serializable interface abstract class command implements serializable {protected string name; // command name protected string ARGs; // command parameter protected configoperator; // maintain the public command (string name) {This. name = Name;} Public String getname () {return this. name;} public void setname (string name) {This. name = Name;} public void setconfig Operator (configoperator) {This. configoperator = configoperator;} // declare two abstract execution methods: Execute () public abstract void execute (string ARGs); public abstract void execute ();} // Add command class: specific command class insertcommand extends command {public insertcommand (string name) {super (name);} public void execute (string ARGs) {This. ARGs = ARGs; configoperator. insert (ARGs);} public void execute () {configoperator. insert (Th Is. ARGs) ;}}// modify the command class: The specific command class modifycommand extends command {public modifycommand (string name) {super (name);} public void execute (string ARGs) {This. ARGs = ARGs; configoperator. modify (ARGs);} public void execute () {configoperator. modify (this. ARGs) ;}/// the DELETE command class deletecommand // configuration file operation class: Request recipient. Because the configoperator class object is a member object of the command, it will also write files along with the command object, so configoperator also needs to implement the serializable Interface Class configoperator implements serializable {public void insert (string ARGs) {system. out. println ("Add new node:" + ARGs);} public void modify (string ARGs) {system. out. println ("Modify node:" + ARGs);} public void Delete (string ARGs) {system. out. println ("delete node:" + ARGs) ;}// configuration file settings window class: Request sender class configsettingwindow {// defines a set to store each Command object private arraylist <command> commands = new arraylist <command> (); Private command; // inject a specific command object public void setcommand (command) {This. command = command;} // execute the configuration file modification command, and add the command object to the command set public void call (string ARGs) using command.exe cute (ARGs); commands. add (command) ;}// record the request log, generate the log file, and write the command set to the log file public void save () {fileutil. writecommands (commands);} // extract the command set from the log file and call the execute () method of each command object cyclically. To reset the public void recover () {arraylist; List = fileutil. readcommands (); For (Object OBJ: List) Export (command?obj=.exe cute () ;}}// tool class: file Operation class fileutil {// write the command set to the log file public static void writecommands (arraylist commands) {try {fileoutputstream file = new fileoutputstream ("config. log "); // create an object output stream to write the object to the object. objectoutputstream objout = new objectoutputstream (New bufferedoutputstream (File ); // Object Writing File objout. writeobject (commands); objout. Close ();} catch (exception e) {system. Out. println ("command saving failed! "); E. printstacktrace () ;}}// command set extraction from log files public static arraylist readcommands () {try {fileinputstream file = new fileinputstream ("config. log "); // create an object input stream to read the object objectinputstream objin = new objectinputstream (New bufferedinputstream (File) from the file )); // read the object in the file and convert it to arraylist-type arraylist commands = (arraylist) objin. readobject (); objin. close (); Return commands;} catch (exception e) {system. out. println ("command read Failed! "); E. printstacktrace (); return NULL ;}}}
Write the following client test code:
Class client {public static void main (string ARGs []) {configsettingwindow CSW = new configsettingwindow (); // defines the request sender's command; // define the command object configoperator CO = new configoperator (); // define the request receiver // change the configuration file four times command = new insertcommand ("add"); command. setconfigoperator (CO); CSW. setcommand (command); CSW. call ("Homepage"); command = new insertcommand ("add"); command. setconfigoperator (CO); CSW. setcommand (command); CSW. call ("port number"); command = new modifycommand ("modify"); command. setconfigoperator (CO); CSW. setcommand (command); CSW. call ("Homepage"); command = new modifycommand ("modify"); command. setconfigoperator (CO); CSW. setcommand (command); CSW. call ("port number"); system. out. println ("----------------------------"); system. out. println ("Save configuration"); CSW. save (); system. out. println ("----------------------------"); system. out. println ("Restore configuration"); system. out. println ("----------------------------"); CSW. recover ();}}
Compile and run the program. The output result is as follows:
Add a new node: Home Page Add new node: Port Number Modify node: Home Page Modify node: Port Number ---------------------------- Save Configuration ---------------------------- Restore Configuration ---------------------------- Add a new node: Home Page Add new node: Port Number Modify node: Home Page Modify node: Port Number |
[Author: Liu Wei http://blog.csdn.net/lovelion]