How to add java startup command to tomcat service

How to add java startup command to tomcat service

My first server program

I'm currently learning to write online games, so I need to write server-side programs. I looked around for PHP, JAVA, and C. Finally, I chose Java for its compatibility with Alibaba Cloud and Tencent Cloud, low cost, and low learning difficulty.

Then start learning how to write java classes. And how to connect to the database and how to run the code every few seconds. After all, these two combined can become the simplest server.

My first program is very simple. After Tomcat is started, it runs every 6 seconds to generate a set of random numbers and send them to the MySQL database.
Key point: Self-starting timer to write database

Since writing to the database and timing operations have been introduced in previous articles.

Therefore, this article mainly introduces how to start automatically.

Existing code:

1. Main function: mainGame.java (the function that starts the game.)

2. Frame running class: gameEnterFrame.java (responsible for loop execution. I set it to run once every 2 seconds and write the number to the database.)

There are two key points about self-starting:

1. You need to modify a configuration file called web.xml

In WEB-INF under webRoot.

If you don't have the same path as mine, unfortunately, that means you created the wrong project type.

Remember to create a new web server project.

Simply add three lines of code to this file to tell Tomcat that I want to run a self-starting class, which I named autoRun. As shown in the figure below, the blue part is the code I added.

For your convenience, paste it here.

<listener> 
<listener-class>game.autoRun</listener-class>
</listener>

With this monitoring statement, you can execute the autoRun class under the game package (the game package is a game class package I created myself, you can create the name of your favorite package) at runtime. This autoRun class is the self-starting code I wrote.

How to write it specifically, see below:

2. How to write the self-starting code:

We need to have the self-starting code lead out to the main function. So under the game package, create a new file named autoRun.java

package game;
import javax.servlet.ServletContextEvent; //This is the class used for self-start, server background event import javax.servlet.ServletContextListener; //This is the class used for self-start, server background listening import game.mainGame; //We import the main function for easy running //Declare an autoRun class and use the server background listening interface. Fixed usage, rote memorization public class autoRun implements ServletContextListener {
//When the backend is initialized, that is, the tomcat startup event occurs, fixed usage public void contextInitialized(ServletContextEvent arg0){
//Write what you need to do hereSystem.out.println("MainFunction is running."); 
mainGame.main(null);
}
//When the backend is destroyed, that is, Tomcat is closed, fixed usage public void contextDestroyed(ServletContextEvent arg0){
//Write the execution content here}
}

As you can see, there are two parts in monitoring the startup and shutdown status of tomcat.

  • One is to start what I want to do
  • The other one is closed. What am I going to do?

Of course, it's closed, and I don't need to take any action right now. I just need to execute the main function of the game after startup. So I put the main function in the startup.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Tomcat startup error: solution to java.util.zip.ZipException
  • Tomcat startup error: java.lang.UnsatisfiedLinkError solution
  • Java Zero-Base Tutorial: How to Install and Start Tomcat Server on Windows (Installation-Free Version)
  • Error when starting tomcat: The proxy threw an exception: java.rmi.server.ExportException: Port already in use: 1099 Solution
  • Learn how to configure Tomcat hot start in javaweb

<<:  How to quickly clean up billions of data in MySQL database

>>:  The use and difference between JavaScript pseudo-array and array

Recommend

How to Run a Command at a Specific Time in Linux

The other day I was using rsync to transfer a lar...

A detailed introduction to Linux system configuration (service control)

Table of contents Preface 1. System Service Contr...

The big role of HTML meta

There are two meta attributes: name and http-equiv...

How to copy MySQL table

Table of contents 1.mysqldump Execution process: ...

MySQL loop inserts tens of millions of data

1. Create a test table CREATE TABLE `mysql_genara...

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

Summary of various methods of MySQL data recovery

Table of contents 1. Introduction 2. Direct recov...

How to display small icons in the browser title bar of HTML webpage

Just like this effect, the method is also very si...

Detailed explanation of the definition and function of delimiter in MySQL

When you first learn MySQL, you may not understan...

HTML Learning Notes--Detailed Explanation of HTML Syntax (Must Read)

1. What is HTML markup language? HTML is a markup...

Ubuntu terminal multi-window split screen Terminator

1. Installation The biggest feature of Terminator...

Teach you how to use webpack to package and compile TypeScript code

TypeScript Bundling webpack integration Usually, ...