Home  >  Article  >  Java  >  How to add java startup command to tomcat service

How to add java startup command to tomcat service

WBOY
WBOYforward
2023-04-29 10:19:06881browse

Existing code:

1. Main function: mainGame.java (pickup function for game startup.)

2. Framework running class: gameEnterFrame.java (responsible for loop execution, I Set it to run every 2 seconds and write the number in the database.)

There are two key points about self-starting:

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

in WEB-INF under webRoot.

If you don't have the same path as my image, unfortunately your project type was created incorrectly.

Remember to create a new web server project.

This file simply adds three lines of code to tell tomcat that I want to run an auto-start class, and I will name it autoRun. As shown below, the blue part is the code I added.

listener-classgame.autoRun/listener-class

/When listener is running, you can execute the autoRun class under the game package (the game package is the game package I created myself, you can create your own favorite package name). This autoRun class is the self-starting code I wrote.

For details, see below:

2. How to write the self-starting code:

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

packagegame

import javax . servlet . SERVLETContextEvent; //This is the class used for self-starting, server background Event

import javax . servlet . servlet context listener; //This is a class used for self-starting, and the background is the server monitor

import game . main game;//We import the main function , easy to operate

//Declare an autoRun class and use the server background listening interface. Fixed usage, rote memorization

publicclass autorunelementsservletcontextlistener {

//When the background is initialized, the tomcat startup event occurs. This is the fixed usage

public void context initialized(servletcontexteventarg 0){

//What you want to do, write it here

system . out . println(' main functionis running,');

maingame . main(null );

}

//When the background is destroyed, a tomcat shutdown event occurs. This is a fixed usage

public void context destored(servletcontexteventarg 0){

//The execution content is written here

}

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

A beginning. What should I do

another close. What should I do

Of course it is closed. I don't need to take any action yet. I just need to perform the main functions of the game upon launch. So I left the main function in startup.

The above is the detailed content of How to add java startup command to tomcat service. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete