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 9031847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:49:55+00:00 2026-06-16T07:49:55+00:00

EDIT : The current code is the working solution, the one which does not

  • 0

EDIT:
The current code is the working solution, the one which does not block the application, it
incorporates the suggestion made in the approved answer.


I want a background thread to download an MS Access database continuously, while my tomcat 7 web application is running, the thread does download the database, however it seems to block my application’s startup as I’m unable to access any page from the service, this is the code that I’m using:

public class DatabaseUpdater implements ServletContextListener {
    private Thread thread = null;

    private final Runnable updater = new Runnable() {

        private boolean hasExpired(File mdbFile) throws IOException {
            if (!mdbFile.exists())
                return true;

            Long ttl = Long.parseLong(Configuration.getValueForOS("db.http-expiration"));

            Date now = new Date();
            Date fileDate = new Date(mdbFile.lastModified());

            return (now.getTime() - fileDate.getTime()) > ttl;
        }

        @Override
        public void run() {
            while (true) {
                if (Thread.currentThread().isInterrupted())
                    throw new RuntimeException("Application Shutdown");

                try {
                    String databases[] = new String[]{"database1", "database2"};
                    for (String database : databases) {

                        String fileName = database + "." + StringUtil.randomString(8) + ".mdb";
                        String fileLocation = Configuration.getValueForOS("db.path");

                        File mdbFile = new File(fileLocation, fileName);
                        File currentDatabaseFile = new File(fileLocation, database + ".mdb");

                        if (hasExpired(currentDatabaseFile)) {
                            URL url = new URL(Configuration.getValueForOS("db.url." + database));
                            InputStream in = url.openConnection().getInputStream();
                            OutputStream out = new FileOutputStream(mdbFile);

                            FileUtil.streamBridge(in, out);
                            FileUtil.close(in, out);

                            while (currentDatabaseFile.exists() && !currentDatabaseFile.delete()) ;
                            while (!mdbFile.renameTo(currentDatabaseFile)) ;
                       }
                   }
                     // Put the thread to sleep so the other threads do not starve
                    Thread.sleep(Long.parseLong(
                        Configuration.getValueForOS("db.http-expiration"));
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }
    };

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        this.thread = new Thread(updater);
        thread.start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        if (this.thread.isAlive())
            this.thread.interrupt();
    }
}

What could be causing?

I based my implementation on this question: Background Thread for a Tomcat servlet app

  • 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-16T07:49:56+00:00Added an answer on June 16, 2026 at 7:49 am

    Given that your code loops forever, you’re probably starving all the other threads in the VM. Try sleeping the thread once in a while.

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

Sidebar

Related Questions

Edit: solution added. Hi, I currently have some working albeit slow code. It merges
EDIT: Android 2.2 MediaPlayer is working fine with one SHOUTcast URL but not with
My current solution will suck sometimes EDIT For those who don't understand,see this example:
Edit II: Current Code works great! Thanks everyone. I went ahead and included my
Currently i have a working PHP edit script, which allows users to edit ads
CURRENT PROGRESS TOWARD SOLUTION: This is the updated code: sql = INSERT INTO Strings
Edit (updated question) I have a simple C program: // it is not important
Working on an ASP.NET 4.0 project, which uses user controls to dynamically generate a
I'm trying to get this working, and I can't find a good solution. I
I'm working on a specialized HTML stripper. The current stripper replaces <td> tags with

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.