How to plan your large Java multi-concurrent server program

zhaozj2021-02-17  49

How to plan your large Java multi-concurrent server program

Author: Chen Linmao 2002-04-08

Since I have advent, more and more large server programs have been developed, mainly

Its stability and security, but for a newcomer, how do you develop your Java application server,

At the same time, how to plan your Java server program and control the progress of your application server development,

Finally, how do you post your Java application server? (I have a good work because of many seniors.

I can only draw a scoop here, I'm not pointed out, please let me know the correct, the old man will accept your heart! I contact us:

Linmaochen@sohu.com)

Less nonsense, turn below the topic:

This article will be divided into the following parts:

1. How to analyze the needs of the server? 2, how to plan the architecture of the server?

3, how to plan the server's directory and naming norm, development code?

4, the development of prototype (-): How to design a server's code skeleton?

5, the development of prototype (2): How to test your code skeleton?

6, detailed encoding?

7, how to release your Java server product?

First, how to analyze the needs of the server?

My point is: 1. The server is like a juque machine, and it is a root of sugar cane, which is a cup of sugar cane juice;

That is to say, before developing the server, first understand what the server is requested? What is the original data?

Next, what is the result of hope? What should the result data should be expressed?

In fact, there are many consideration, and it cannot be listed one by one (omitted).

Second, how to plan the architecture of the server?

First asked everyone a small question: In the large market in Shanghai, the bus buses on the road can be roughly divided into the following categories:

Air-conditioned passenger car, fare is generally two, do not need to be queued, can you sit on seats, you have to look at individual integration;

Unmanned ticket cars, the fare is generally 1 piece and a five-hairy, which requires a torque queue before getting on the bus. Of course, the seat is every person.

So, my question is, which kind of car is good? And the speed of getting up and down is fast? The answer is affirmative: no ticket cars.

Therefore, I generally design the server's architecture is:

First, there is a request queue, responsible for receiving the client's request, and it should also have a request processing mechanism. To actually, there should be an interface;

Second, there should be an output queue, which is responsible for collecting a processed request and ready for the corresponding answer; of course, it also has one

Answer mechanism, how to send the result information to the client;

Everyone knows that the server program does not have a log, then the server needs to have a log queue, responsible for the whole service.

The log information collection and processing of the instructor;

Finally, I said that there is a banknote, so the server also needs to have a verification mechanism.

... (What to say is too much, just slightly)

Third, how to plan the server's directory and naming standard, development code

For a general large server program, there should be the following directory: bin: Mainly stored the executable binary file; CommON: Store the Java program to perform the support class;

Conf: Store configuration file information for server programs;

Logs: Store the log information of the server;

Temp: Store some temporary file information generated by the server operation;

Cache: Store some buffer files generated in the server operation;

SRC: Of course, it is a Java source program that stores the server.

... (other settings, according to specific needs.)

Fourth, the development of prototype (-): How to design a server's code skeleton?

1. First of all the server programs need to have a startup class, we may wish to name the server: (ServerName) .class .class

2. The server needs to have a global thread, naming: (MAINTHREAD.CLASS);

3. Note whether there is a short connection and long connection, each client needs to have a thread to look at it, namelyclientthread.class

4. The request queue also needs to perform in a thread: (InputQuene.Class), the corresponding thread processing class is in inputprocessthread.class

name;

5. The output queue also requires a thread: (OutputQuene.class), the corresponding process mechanism is named at outputProcessThread.class;

6. The log queue is also a thread, we named logQuene.class, logqunenethread.class;

7. The cleaning of the buffer also requires timed work, and we are named in cachethread.class;

8. If your parameter information is expressed in XML, then I also recommend using a separate class to manage these parameters:

CONFIG.CLASS

9. Of course, if you want to do something more details, you may wish to make the communication section of the client's server side in the form of an interface: Comminterface.class

... (too much, I can only say anything!)

V. Development of prototype (2): How to test your code skeleton?

Below is the prototype of the skeleton code, I hope everyone will mention more comments! Thank you!

/ * Server Description: Server Master Thread 1. Read configuration file information 2. Establish a demand input queue 3. Establish a demand processing output queue 4. Establish a demand processing thread 5. Establish an output preprocessing thread to preprocessing the result of the demand processing 6. Create a buffer management thread and start managing the buffer 7. Establish a service connection set, accept the customer's connection request, and establish a customer connection processing thread * /

Import java.io. *; import java.ut. *; import java.util. *;

Public class mainthread extends thread {

Private Serversocket Serversocket = NULL;

/ * Port * / private int serverport, current server listening

Public MAINTHREAD (STRING Confurl) {Try {/ * establishes server listener sleeve * / this.serversocket = new Serversocket (Serverport);

} catch (exception e) {// system.out.println (E.getMessage ());}}

/ * Optick of thread * / public synchronized void Run () {

While (listening) {try {

Socket SerSocket = this.serversocket.accept (); ClientthRead_clientthread = New Clientthread ([paramlist]);

_clientthread.start ();} catch (Exception E) {

}

}

/ * Exit system * / system.exit (0);

/* 1. Complete the customer's connection request and verify the user password 2. Accept the user's request and press the request information into the stack; 3. Search for the corresponding result information from the result output queue and send the result information to the customer; Processing an exception in the demand processing and sends the log information to the log server. * /

Import java.io. *; import java.net. *;

Public class clientthread extends thread {

Public ClientthRead ([paramlist]) {

} Public void synchronized run () {

}

/ * Request queue: 1. Press the customer's needs into the queue 2. Popular the customer's needs * /

Import java.util. *;

Public class input queene {

PRIVATE Vector INPUTTEAM;

Public infutquene () {

/ * Initialization queue capacity * / inputteam = new vector (100);

}

/ * Demand into the team function * / public synchronized void enquene (Request request) {inputteam.add (request);}

/ * Put a request * / public synchronized void dequence (int index) {this.inputteam.remove (index);

}

/ * Request queue processing thread 1. Each request is taken from the demand queue according to the advanced first outline, and processes 2. Update the processed process status 3. Clean up the processed request * /

Import java.io. *; import java.util. *;

Public class infutprocessthread extends thread {

PRIVATE INPUENE _INQUENE;

Public InputProcessThread () {} public void run () {}

}

/ * Results Output Queue: 1. Enter the team 2 of the output result. Distinguished the output of the output * /

Import java.util. *; import java.io. *;

Public class outputquuene {

// Result output queue container private vector outputteam;

Public outputquene () {

// Initialization result output queue OutputTeam = New Vector (100);

// Enter the team function public synchronized void enquene (result) {outputteam.add (result);} / * Drawn function * / public synchronized void dequester (int index) {OutputTeam.Remove (index);}}

/ * Result processing thread: 1. Complete the confirmation of the output result 2. Complete the generation of the output result file stream 3. Complete the compression processing of the file stream * / Import java.io. *;

Public class outputprocessthread eXtends thread {

PRIVATE OUTPUTQUENE _OUTPUTQUENE

Public OutputProcessthread ([paramlist]) {// Todo}

/ * Optick of thread * / public void run () {while (doing) {TRY {

/ * Processing output queue * / processQuene ();

} catch (exception e) {E.PrintStackTrace ();

}

}

}

/ * Log Information Processing Thread: Function Description: 1. Complete the save 2 of the server log information. Clean up log information according to the set rules

The desired goal: The current log information is saved in a file, and the size of the file is automatically controlled later. * /

Import java.io. *; import java.util. *;

Public Class Logthread Extends Thread {Private Logquene LogQuene; Public LogThread ([paramlist]) {// Todo}

/ * Processing log information * / public void Run () {while (doing) {this.processLog (); try {this.sleep (100);} catch (exception e) {}}}}

/ * Function Description: Manage file information in the buffer, control all the size of the file within the range of the system * / import java.io. *; Import java.lang. *; Import java.util. *; Import java.text. *; import java.math. *;

Public class cachethread extends thread {private string cachepath; / * Class construction: Parameters: URL Buffer Directory Path Information * / Public CacheThread (String URL) {this.cachepath = URL;

/ * Create a file search class * / try {this.cachedir = new file (this.cachepath);} catch (exception e) {E.PrintStackTrace ();}

}

// Thread Optimum Wear Public Void Run () {// Timed Cleanup Buffer Document}

......

}

Write here today, I will write again!

转载请注明原文地址:https://www.9cbs.com/read-29021.html

New Post(0)