In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the error report of the observer and startup in springboot2.0.6 and the related analysis of Headless mode. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Parsing SpringApplication's run method watcher, startup error report, Headless mode, listener correlation analysis public ConfigurableApplicationContext run (String... Args) {/ / construct a task execution observer (Java class for checking time and performance) StopWatch stopWatch = new StopWatch (); / / start execution, record start time stopWatch.start (); / / customize the configuration context where the top-level parent class contains Bean Factory ConfigurableApplicationContext context = null; / / defines an error set (to support reporting startup errors) Collection exceptionReporters = new ArrayList () / / define a headless of awt (set headless mode, and the system property named java.awt.headless is set to true) configureHeadlessProperty ();. / / omit} 1. StopWatch (viewer) is mainly generated by spring to calculate the load time.
It contains a list (representing the number of tasks). When the start method is called, and then stop is called, both the current time and the start time are recorded, and then encapsulated into a task object and added to the list inside the StopWatch, where the start and stop methods must appear in pairs.
2. SpringBootExceptionReporter--- is used to support reporting errors about startup
Execute the getSpringFactoriesInstances method to get the specific implementation class from the configuration file. When an exception occurs in the springboot startup project, the handleRunFailure method is called, as shown below:
Private void handleRunFailure (ConfigurableApplicationContext context, Throwable exception, Collection exceptionReporters, SpringApplicationRunListeners listeners) {try {try {handleExitCode (context, exception); if (listeners! = null) {listeners.failed (context, exception);}} finally {reportFailure (exceptionReporters, exception); if (context! = null) {context.close () } catch (Exception ex) {logger.warn ("Unable to close ApplicationContext", ex);} ReflectionUtils.rethrowRuntimeException (exception);}
First, take a look at handleExitCode (context, exception).
Private void handleExitCode (ConfigurableApplicationContext context, Throwable exception) {int exitCode = getExitCodeFromException (context, exception); if (exitCode! = 0) {if (context! = null) {context.publishEvent (new ExitCodeEvent (context, exitCode));} SpringBootExceptionHandler handler = getSpringBootExceptionHandler (); if (handler! = null) {handler.registerExitCode (exitCode);}
The above code means to obtain the exit status code from the exception. If the exit status code is not equal to 0, the exit event is issued through ConfigurableApplicationContext and the status code is registered in SpringBootExceptionHandler.
Let's take a look at reportFailure (exceptionReporters, exception).
Private void reportFailure (Collection exceptionReporters, Throwable failure) {try {for (SpringBootExceptionReporter reporter: exceptionReporters) {if (reporter.reportException (failure)) {registerLoggedException (failure); return;}} catch (Throwable ex) {/ / Continue with normal handling of the original failure} if (logger.isErrorEnabled ()) {logger.error ("Application run failed", failure) RegisterLoggedException (failure);}}
The above code mainly calls the reportException method of SpringBootExceptionReporter in turn (this method mainly calls the collection of analyzer to analyze the exception, and prints the exception if any analyzer can analyze successfully) and registers the exception in SpringBootExceptionHandler.
3. Set headless mode
Headless mode is a system configuration that lacks a display screen, keyboard, or mouse. There are many methods in the java.awt.toolkit and java.awt.graphicsenvironment classes that call the methods of the monitor, keyboard, and mouse in addition to fonts, graphics, and printing. But there are some classes, such as Canvas and Panel, that can be executed in headless mode
Private void configureHeadlessProperty () {/ / what is called here is: if java.awt.headless / / does not provide external devices, it will operate on its own. / / this.headless defaults to true System.setProperty (SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty (SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString (this.headless));} public final class System {public static String setProperty (String key, String value) {/ / determine null operation checkKey (key); / / get security administrator default null SecurityManager sm = getSecurityManager () If (sm! = null) {sm.checkPermission (new PropertyPermission (key, SecurityConstants.PROPERTY_WRITE_ACTION));} / / returns return (String) props.setProperty (key, value);}}
System property configuration
To enable headless mode, you need to use the setProperty method to set the corresponding system properties.
System.setProperty ("java.awt.headless", "true")
If you want to use headless and traditional environments in the same program, you can use the following command line to do this: java-Djava.awt.headless=true
Set the value of this property in java.awt.GraphicsEnvironment to true by reflection
If the system property named java.awt.headless is set to true, the headless toolkit will be used. The application can do the following:
Create lightweight components.
Collect information about available fonts, font metrics, and font settings.
Set the color to render the ready picture.
Create and obtain images to prepare pictures for rendering.
Use printing in the java.awt.PrintJob,java.awt.print and javax.print classes
This is the end of the error report on the observer and startup in springboot2.0.6 and the related analysis of Headless mode. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.