Skip to main content

⏰ Spring Scheduler Calling Multiple Times? Here's What Went Wrong!

 Hey devs πŸ‘‹,

A few years back (around 3–4 years ago), I got a chance to work on a project involving Spring Cron Scheduler — using both XML configuration and Java annotations.

The requirement was pretty standard:

✅ Trigger a job every day at a particular time
✅ Fetch data from a table maintained by another team
✅ Process that data further

Everything worked well at first — in lower environments and even after initial production deployment.

But then... something strange started happening 😬


🚨 The Weird Behavior

After 4-5 days in production, the scheduled method started behaving oddly:

❌ It triggered twice at the same time
❌ Then later, three times, then four...
😡 Restarting the application fixed it — but only temporarily.
After a few more days, it would go back to calling multiple times again.

It was as if my @Scheduled method had cloned itself and started a party! πŸ₯΄


⏳ Take 10 Seconds... Can You Guess the Root Cause?

You probably thought of:

  • Duplicate entries in the cron config? ❌

  • Bad server clock sync? ❌

  • Parallel jobs? ❌

Nope. The real issue was deeper... and sneakier.


🧠 Root Cause Analysis (RCA)

After some serious debugging and log tracing, I found the actual cause:

πŸ‘‰ Multiple ApplicationContext initializations inside the codebase!

Here’s what was happening:

  • Some report functionality / test utility code was programmatically initializing ApplicationContext like this:

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
  • Every time that functionality was triggered, a new context was created.

  • That context would load the scheduler bean again.

  • Result? Your scheduled job now existed multiple times in memory!

So if the test method ran 5 times, there were 5 instances of your scheduler bean — all firing at the scheduled time. πŸ’₯


πŸ“˜ What is ApplicationContext?

Let’s take a quick refresher.

ConceptDescription
🧠 What             It's the central container in Spring that manages the lifecycle of beans
πŸ› ️ Why              Handles bean creation, dependency injection, and configuration
⏱️ When              It should be initialized once during application startup
❌ Don’t            Manually create it inside business logic or methods



❌ What Went Wrong

⚠️ A helper method used in reports/tests was initializing ApplicationContext again
⚠️ This reloaded the @Scheduled beans
⚠️ Now multiple scheduler beans existed simultaneously
⚠️ Each was independently triggering at the same cron time 😨


✅ The Fix: Singleton ApplicationContext

To fix this, we made sure:

ApplicationContext is initialized once only at app startup
✅ All scheduler beans are defined only in the main Spring context
✅ No dynamic or hidden context initialization is done in any logic layer

Here's what a proper scheduler looks like:


@Component public class MyScheduler { @Scheduled(cron = "0 0 10 * * ?") // Every day at 10 AM public void runJob() { // Fetch and process data } }

Let Spring manage this as a singleton, not you!


πŸ’‘ Best Practices

🌟 Never initialize ApplicationContext manually in code
🌟 Use @SpringBootApplication to bootstrap your context
🌟 Ensure all scheduler classes are under component scan paths
🌟 Use dependency injection — not manual context fetching


πŸ”š Wrapping Up

This post hopefully saves you from chasing vague errors across layers.
The moment your @Scheduled method starts behaving like a haunted loop — think ApplicationContext. πŸ‘»

Have you dealt with weird Spring behavior like this before?
Drop your stories in the comments or reach out!

Until next time,
Anand ☕ @ Java Bean Bag

Comments

Popular posts from this blog

🐱 Tomcat vs ⚡ Netty – Which One Should You Use?

🐱 Tomcat vs ⚡ Netty – Which One Should You Use? So recently I got curious about this too πŸ€”. Everywhere in Spring Boot tutorials we see Tomcat . Then suddenly while exploring Spring WebFlux , the name Netty pops up. And I was like – “Wait, who’s this Netty guy trying to replace Tomcat?” πŸ˜… Let’s break it down with real-time examples , icons , and fun comparisons . 🐱 Tomcat – The Traditional Web Server Type: Servlet Container (blocking I/O) World: Used with Spring MVC Style: Thread-per-request model πŸ‘©‍πŸ’» Pros: Stable, widely used, battle-tested Cons: Struggles with huge concurrent connections πŸ‘‰ Example in real life: Tomcat is like a restaurant with fixed waiters 🍴. - Each customer = one thread/waiter - If too many customers come in at once → waiters run out → customers wait outside πŸšͺ ⚡ Netty – The Reactive Rockstar Type: Asynchronous Event-Driven Network Framework World: Default for Spring WebFlux Style: Event-lo...

🎭 Spring’s Secret: Why @Transactional & Friends Betray You Silently

πŸ’‘ Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” 😡‍πŸ’« “Why is Redis cache not working?” 🀯 Turned out, the issue was one silent villain: 🧱 Self-invocation 🀷 What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. 🧠 Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } πŸ•΅️ Real-Life Analogy — The Gateway Community 🏘️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: πŸ›‚ Watchman = Spring Proxy 🏠 Your apartment = Your service class πŸšͺ Your room = A method inside that class πŸƒ Scenario 1: Outsider Visits Your friend from outside...

🌟 My Journey – From Zero to Senior Java Tech Lead 🌟

 There’s one thing I truly believe… If I can become a Java developer, then anyone in the world can. πŸ’― Sounds crazy? Let me take you back. πŸ•“ Back in 2015… I had zero coding knowledge . Not just that — I had no interest in coding either. But life has its own plans. In 2016, I got a chance to move to Bangalore and joined a Java course at a training center. That’s where it all started — Every day, every session made me feel like: "Ohhh! Even I can be a developer!" That course didn’t just teach Java — it gave me confidence . πŸ§ͺ Two Life-Changing Incidents 1️⃣ The Interview That Wasn't Planned Halfway through my course, I had to urgently travel to Chennai to donate blood to a family member. After that emotional rollercoaster, I found myself reflecting on my skills and the future. The next day, as I was preparing for my move to Bangalore to complete the remaining four months of my course, I randomly thought — "Let me test my skills... let me just see...