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

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

Tutorial on how to install and use Ceph distributed software under Linux

Table of contents Preface 1. Basic Environment 1....

Implementation of CSS dynamic height transition animation effect

This question originated from a message on Nugget...

MySQL learning database operation DML detailed explanation for beginners

Table of contents 1. Insert statement 1.1 Insert ...

Write a React-like framework from scratch

Recently I saw the article Build your own React o...

How to check if the firewall is turned off in Linux

1. Service method Check the firewall status: [roo...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

How to use Vue3 asynchronous data loading component suspense

Table of contents Preface Creating Components Sum...

Detailed analysis and testing of SSD performance issues in MySQL servers

【question】 We have an HP server. When the SSD wri...

Linux common basic commands and usage

This article uses examples to illustrate common b...

Detailed explanation of the use of Refs in React's three major attributes

Table of contents Class Component Functional Comp...

Implementing Priority Queue in JavaScript

Table of contents 1. Introduction to priority que...