In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The solution to prevent the repeated startup of JAVA programs, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
There is a background task handler in our project, which is java development application, which is used to deal with some batch data files submitted by the website, because the data amount of these data files is generally large, so we wrote this batch processing program to deal with these batch data files asynchronously. This program is designed as a plug-in, dealing with a variety of different data files as a separate plug-in, and then using Spring to glue each component, so that the program can be easily extended.
Today, the customer made a request: need to control that this program can only start one instance on the same host.
In order to meet the customer requirements, my first thought is to create a table in the database, write a flag to the table when the program starts, and then delete the flag when the program ends. But one problem with this approach is that if the program stops or is killed abnormally, the flag cannot be cleared, and the next startup will be misjudged as a repeat startup; in addition, if the startup flag is recorded in a database, it feels awkward to closely couple the program with the database.
After excluding the first option, I thought of using files to save startup flags (as if some large programs, such as weblogic, seem to control repeated starts by recording startup flags in a file). Passenger flow this way does not need to be coupled with the database, but there is also a problem that the program aborts abnormally and cannot clear the startup flag, so this scheme is also shot.
The third solution I can think of is to call the operating system to view the system process in JAVA to get the system process, and then detect that the system process has a special process flag to determine whether it is started repeatedly. But this way looks awkward, and the commands for viewing system processes in Window and * nix systems are different and are dealt with in several cases, which increases the complexity of the program for no reason and is not advisable.
Can you record a boot flag in memory? In theory, this should not be feasible, because it is impossible to manipulate memory data with each other across JVM. I searched the Internet, but I couldn't find any relevant examples.
Can we use a little bit of system shared resources in exchange for our goals? The system resource that is easy to think of and cannot be reused is the port. I try to use the following scheme: specify a less commonly used port in the program (for example: 12345), when the program starts, start a ServerSocket on the specified port, this Socket is just to occupy this port and does not accept any network connections. If you try to start a second instance, the program will throw an exception when it starts ServerSocket on that designated port, then we can assume that the system has been started, then print a prompt and exit the program directly. This approach should be possible in theory, and I began to modify the program. The program is modified as follows:
Java code
Package cn.com.pansky.xmdswz.application.scheduler
Import org.apache.commons.logging.Log
Import org.apache.commons.logging.LogFactory
Import org.quartz.SchedulerException
Import org.quartz.impl.StdScheduler
Import org.springframework.beans.factory.BeanFactory
Import org.springframework.context.support.ClassPathXmlApplicationContext
Import cn.com.pansky.xmdswz.system.cache.CachedTableMgr
Import cn.com.pansky.xmdswz.system.config.SystemConfig
Import cn.com.pansky.xmdswz.utility.DateUtil
Import org.quartz.JobDetail
Import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
Import java.net.ServerSocket
Import java.io.*
/ * *
* Title: XXXXXXX
* Description: XXXXXXXXXXXX
* Copyright: Copyright (c) 2006
* Company: www.pansky.com.cn
*
* @ author Sheng Youfu
* @ version 1.0
, /
Publicclass Scheduler {
Privatestatic Log log = LogFactory.getLog (Scheduler.class)
Privatestatic ServerSocket srvSocket = null; / / service thread, which is used to control the server to start only one instance
Privatestaticfinalint srvPort = 12345; / / controls the port number to launch the unique instance, which will be more flexible if saved in the configuration file
/ * *
* scheduled task profile
, /
Privatestatic String CONFIG_FILE = "cn/com/pansky/xmdswz/application/scheduler/Scheduling-bean.xml"
Public Scheduler () {
/ / check whether the system starts only one instance
CheckSingleInstance ()
/ / read the configuration file of Spring below
SystemConfig cfg = new SystemConfig ()
String config = cfg.parseParam ("SCHEDULER.CONFIG_FILE", false)
If (configcolors null & &! ".equals (config.trim ()
CONFIG_FILE = config
Log.debug ("CONFIG_FILE:" + CONFIG_FILE)
}
/ * *
* main function
* @ param args String []
* @ throws Exception
, /
Publicstaticvoid main (String [] args) throws Exception {
Scheduler sch = new Scheduler ()
Sch.execute ()
}
/ * *
* run scheduled tasks
, /
Publicvoid execute () {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext (new String [] {CONFIG_FILE})
BeanFactory factory = (BeanFactory) appContext
/ * *
* load task scheduling
, /
StdScheduler scheduler = (StdScheduler) factory.getBean ("schedulerFactoryBean")
/ / pause all tasks and wait for the cache code table to be loaded
Try {
Scheduler.pauseAll ()
} catch (SchedulerException ex) {
Log.error ("", ex)
}
/ * *
* load the cache code table
, /
CachedTableMgr cachedtableMgr = (CachedTableMgr) factory.getBean ("cachedTableMgr")
Try {
CachedtableMgr.loadCodeTable ()
} catch (Exception ex) {
Log.fatal ("Load cached table failed. System will exit.", ex)
System.exit (0)
}
/ / resume all tasks
Try {
Scheduler.resumeAll ()
} catch (SchedulerException ex) {
Log.error ("", ex)
}
}
/ * *
* check whether only one instance has been started by the system
, /
Protectedvoid checkSingleInstance () {
Try {
SrvSocket = new ServerSocket (srvPort); / / start a ServerSocket to control that only one instance is started
} catch (IOException ex) {
If (ex.getMessage () .indexOf ("Address already in use: JVM_Bind") > = 0)
System.out.println ("only one process can be started at a time on a host (Only one instance allowed).")
Log.fatal ("", ex)
System.exit (0)
}
}
}
This is the answer to the solution to prevent the repeated startup of the JAVA program. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.