My first server program
Recently, I have been learning to write online games, so I have to write server-side programs. I searched for PHP, JAVA, and C. Finally, in order to have good compatibility with Alibaba Cloud and Tencent Cloud, low cost and low learning difficulty, I chose Java.
Then start to learn how to write Java class. And how to connect to the database and how to run the code once every few seconds. After all, these two can become the simplest server together.
My first program is very simple. After Tomcat is started, it runs every 6 seconds, generates a set of random numbers, and gives it to the mysql database.
Key points: Self-start timed run Write database
Since writing databases and timing runs in previous articles have been introduced.
Therefore, this article mainly introduces how to start it up.
Already have code:
1. Main function: (The game starts the function.)
2. Frame running class: (Responsible for loop execution. I set it to run it once every 2 seconds and write a number into the database.)
There are two key points about self-start:
1. You need to modify a configuration file. The name is
In WEB-INF under webRoot.
If you don't have the same path as my image, it's a pity that your project type is created wrong.
Remember to new a web server project.
This file simply adds three lines of code and tells tomcat that I want to run an auto-start class, and I just name it autoRun, and that's fine. As shown in the figure below, the blue part is the code I added.
It is convenient for everyone to use and paste it.
<listener> <listener-class></listener-class> </listener>
With this listening sentence, you can execute the autoRun class under the game package (the game package is a game class package I created myself, and you can create the name of your favorite package) when running. This autoRun class is the auto-start code I wrote.
For details, see below:
2. How to write the self-start code:
We need to have the self-start code lead to the main function. So under the game package, create a new file with the name
package game; import ;//This is the class to be used for self-start, server background eventimport ;//This is the class to be used for self-start, and the server background monitorsimport ;//We import the main function for easy operation//Declare an autoRun class and use the server background listening interface. Fixed usage, rote memorizationpublic class autoRun implements ServletContextListener{ //When the background is initialized, the tomcat start event occurs, fixed usagepublic void contextInitialized(ServletContextEvent arg0){ //What you want to do is write here("MainFunction is running."); (null); } //When the background is destroyed, the tomcat shutdown event occurs, fixed usagepublic void contextDestroyed(ServletContextEvent arg0){ //The execution content is written here} }
You can see that there are two parts in the listening to tomcat startup and shutdown state.
- One is what I want to do after starting it
- Another thing is to close what I want to do
Of course, it's closed, I don't need to perform any actions at the moment. I just need to execute the main function of the game after startup. So I still put the main function in the startup.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.