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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:47:49+00:00 2026-05-19T04:47:49+00:00

I’m using Java and I want to keep a servlet continuously running in my

  • 0

I’m using Java and I want to keep a servlet continuously running in my application, but I’m not getting how to do it. My servlet has a method which gives counts of the user from a database on a daily basis as well as the total count of the users from the whole database. So I want to keep the servlet continuously running for that.

  • 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-19T04:47:50+00:00Added an answer on May 19, 2026 at 4:47 am

    Your problem is that you misunderstand the purpose of the servlet. It’s intented to act on HTTP requests, nothing more. You want just a background task which runs once on daily basis.

    EJB available? Use @Schedule

    If your environment happen to support EJB (i.e. a real Java EE server such as WildFly, JBoss, TomEE, Payara, GlassFish, etc), then use @Schedule instead. Here are some examples:

    @Singleton
    public class BackgroundJobManager {
    
        @Schedule(hour="0", minute="0", second="0", persistent=false)
        public void someDailyJob() {
            // Do your job here which should run every start of day.
        }
    
        @Schedule(hour="*/1", minute="0", second="0", persistent=false)
        public void someHourlyJob() {
            // Do your job here which should run every hour of day.
        }
    
        @Schedule(hour="*", minute="*/15", second="0", persistent=false)
        public void someQuarterlyJob() {
            // Do your job here which should run every 15 minute of hour.
        }
    
        @Schedule(hour="*", minute="*", second="*/5", persistent=false)
        public void someFiveSecondelyJob() {
            // Do your job here which should run every 5 seconds.
        }
    
    } 
    

    Yes, that’s really all. The container will automatically pickup and manage it.

    EJB unavailable? Use ScheduledExecutorService

    If your environment doesn’t support EJB (i.e. you’re not using not a real Java EE server, but a barebones servletcontainer such as Tomcat, Jetty, etc), then use ScheduledExecutorService. This can be initiated by a ServletContextListener. Here’s a kickoff example:

    @WebListener
    public class BackgroundJobManager implements ServletContextListener {
    
        private ScheduledExecutorService scheduler;
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.scheduleAtFixedRate(new SomeDailyJob(), 0, 1, TimeUnit.DAYS);
            scheduler.scheduleAtFixedRate(new SomeHourlyJob(), 0, 1, TimeUnit.HOURS);
            scheduler.scheduleAtFixedRate(new SomeQuarterlyJob(), 0, 15, TimeUnit.MINUTES);
            scheduler.scheduleAtFixedRate(new SomeFiveSecondelyJob(), 0, 5, TimeUnit.SECONDS);
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            scheduler.shutdownNow();
        }
    
    }
    

    Where the job classes look like this:

    public class SomeDailyJob implements Runnable {
    
        @Override
        public void run() {
            // Do your daily job here.
        }
    
    }
    
    public class SomeHourlyJob implements Runnable {
    
        @Override
        public void run() {
            // Do your hourly job here.
        }
    
    }
    
    public class SomeQuarterlyJob implements Runnable {
    
        @Override
        public void run() {
            // Do your quarterly job here.
        }
    
    }
    
    public class SomeFiveSecondelyJob implements Runnable {
    
        @Override
        public void run() {
            // Do your quarterly job here.
        }
    
    }
    

    Do not ever think about using java.util.Timer/java.lang.Thread in a Java EE / Servlet based environment

    Last but not least, never directly use java.util.Timer and/or java.lang.Thread in Java EE. This is recipe for trouble. An elaborate explanation can be found in this JSF-related answer on the same question: Spawning threads in a JSF managed bean for scheduled tasks using a timer.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.