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

  • SEARCH
  • Home
  • 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 8960319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:38:12+00:00 2026-06-15T15:38:12+00:00

I have a problem which I do not know how to solve. I have

  • 0

I have a problem which I do not know how to solve. I have made a java polling app using executor service of 1 single thread. That service does some file manipulation and database interactions.

if we run with java -jar myapp.jar from command promt/ executable jar , it runs . We can stop it at any moment.

Now I need to make 2 scripts for the user (liek tomcat). One for start the app , another to stop.

But the think is when a user executes the script to stop, the process will not be killed until all the jobs/threads of executor service is finished. Because, if we kill the process while in running state, the system doing the job will stop can be in inconsistent state (during moving files for example ).

THe problem is, I can not access ExecutorService result from shell script.

How can I make two scripts to run this app and control and access the behaviors. ?

EDIT:

My myapp.jar runs a periodic executor service. I need to make a stop script which will allow to stop only after the executor service is not running the task, not kill the process. Because, if I kill the process during operation , I lost consistency of the system. My ssytem reads file and moves those into another directory. If I kill in the middle , some files will be processed,some will not. I need a solution where if user executes the stop script from the UNIX system, the script either does not kill the process or wait until the job finishes.

  • 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-06-15T15:38:13+00:00Added an answer on June 15, 2026 at 3:38 pm

    I guess Shutdown Hook is the solution here which you need. Shutdown Hooks gives the user complete control over the thread upon which a shutdown action is executed. The thread can be created in the proper thread group, given the correct priority, context, and privileges, and so forth.

    Sample program

    public class ShutDownHook {
    
        public static void main(String[] args) {
            
            final Thread mainThread = Thread.currentThread();
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    // Add your cleanup logic here
                    System.out.println("Shutdown hook is executing");
                    try {
                        mainThread.join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
            
            System.exit(0);
        }
    }
    

    Output

    Shutdown hook is executing
    
    • Runtime.getRuntime().addShutdownHook(Thread h) adds the hook and
    • Runtime.getRuntime().removeShutdownHook(Thread h) remove the specified hook given as parameter.

    You just need to add your proper code logic in the shutdown hook as specified above.

    Reference link javadoc, One blog post

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

Sidebar

Related Questions

I have a problem which I do not know how to solve. I have
I have another weird problem which I have not been able to solve in
I have a problem which I don't really know how to solve. I have
I have a problem which I don't know how to solve. I have created
Once again I have a problem, which I just don't know how to solve.
I have a problem, which is not easily described. I'm writing a web application
I have a problem with adding filter for attribute, which exist not in all
I have a problem which I don't know how to fix. It has to
I have the following problem which I could not find a solution for anywhere.
I have a unusual problem which I have no idea how to solve. I

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.