Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:35:27+00:00 2026-05-27T22:35:27+00:00

Suppose a shutdownHook as : class ShutdownHolder extends Thread { public void run() {

  • 0

Suppose a shutdownHook as :

 class ShutdownHolder extends Thread {  

    public void run() {
      Logger logger = LoggerFactory.getCoreLogger(ShutdownHolder.class);
      try {
        logger.info("Shutdown hook is running...");
        doSomething();  
        logger.info("Shutdown hook end."); 
      } catch(Exception e) {
        logger.severe("Unexpected ERROR during shutdown", e);
      }
    }
  }

How to avoid that logger doesn’t be closed before ShutdownHook ends ?

For example, doSomething() method could call several methods on other accessible classes also writening logs. I don’t want to skip all these logs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:35:28+00:00Added an answer on May 27, 2026 at 10:35 pm

    So aside from some reflection hacks, I don’t see an easy way to do this unfortunately. Under 1.6 JDK the shutdown hooks are stored in an IdentityHashmap which provides no guaranteed order. In effect the shutdown hooks are called in random order depending on the internals of that map.

    In terms of reflection, I tried to do the following but hookMap comes out as null for me. If you can get it to work then you may be able to remove the log4j hook and call it manually at the end of your hook or something.

    Class<?> clazz = Class.forName("java.lang.ApplicationShutdownHooks");
    Field field = clazz.getDeclaredField("hooks");
    field.setAccessible(true);
    @SuppressWarnings("unchecked")
    Map<Thread, Thread> hookMap = (Map<Thread, Thread>) field.get(null);
    

    However, I would strongly recommend doing something other than a shutdown hook. How about calling your own hooks before main finishes? For us, we use Spring and the DisposableBean pattern heavily. Those beans are called in reverse order from when the classes were instantiated so anything that depends on the loggers still has the logger when it shuts down.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the following constructor: class Needed { public: Needed () {} Needed (const char
Suppose I had the following method in an object: public class foo { public
Suppose I define an export in AssemblyA: [Export(typeof(Foo))] public class Foo { ... }
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose you have the following Generic class heirarchy: public abstract class GenericBase<T> { T
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Suppose I have a grid of squared defined like so in a class: Square
Suppose I have a innerclass extends asynctask called A and the outerclass extends Activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.