Difference between Activate and Run in AEM Scheduler | Community
Skip to main content
Level 3
June 22, 2023
Solved

Difference between Activate and Run in AEM Scheduler

  • June 22, 2023
  • 3 replies
  • 1765 views

I was wondering what's the difference between Activate vs. Run in AEM scheduler? I am following this post: AEM 6.4 : Creating a Scheduler Using OSGi R6 Annotations | by Ankur Ahlawat | Adobe Tech Blog

 

In this article, I don't see any scheduler related code inside the run() method. What kind of code do I normally put inside the run() method and activate() method? Thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

@aemuser2345 

 

1. `run()` method: This method is the main entry point for the scheduled task's logic. It contains the code that executes when the scheduler is triggered according to its defined schedule. The `run()` method is responsible for performing the desired operations or executing the specific task for which the scheduler is designed.

 

For example, let's say you have a scheduler in AEM that is responsible for sending daily email notifications to subscribed users. The `run()` method of this scheduler would contain the logic to fetch the list of subscribed users, compose the email content, and send the email to each user.

Here's a simplified example of a scheduler with the `run()` method:

 

public class DailyEmailScheduler implements Runnable { @Override public void run() { // Fetch the list of subscribed users List<User> subscribedUsers = getUserService().getSubscribedUsers(); // Compose email content String emailContent = composeEmailContent(subscribedUsers); // Send email to each user for (User user : subscribedUsers) { EmailService.sendEmail(user.getEmail(), "Daily Update", emailContent); } } // Other methods and dependencies... }

In this example, the `run()` method is triggered according to the cron expression specified in the `scheduler.expression` property.

2. `activate()` method: This method is called when the scheduler is activated or enabled in the AEM instance. It is typically used to perform initialization tasks or set up any required resources before the scheduler starts running.

or perform initialization tasks when the scheduler is enabled.

3 replies

krishna_sai
Community Advisor
Community Advisor
June 23, 2023

Hi @aemuser2345 ,
@Activate annotation is used to notify your component that it is now loaded, resolved and ready to provide service.
@Modified annotation is notified when ever you make changes to the OSGI configuration properties and save them in /system/console/configMgr
@Deactivate is used when the component is deactivated


Run() method will be defining our task where our actual logic is executed at our scheduled time like sending emails or activating pages or generating sitemaps etc.,

Hope this helps,
Krishna

Tanika02
June 23, 2023

Hello @aemuser2345 - 

 

1. run() Method

 

  • This method is responsible for the actual execution of the job logic. It is called when the job is triggered for execution, either according to a defined schedule or manually.
  • Inside the run() method, you would typically include the code that performs the intended functionality or business logic of the job.

2. activate() Method

 

  • The activate() method is specific to OSGi components (such as servlets, services, or listeners) in AEM. It is called when the component is activated or enabled for use.
  • Inside the activate() method, you typically configure and initialize the component, set up any necessary resources or dependencies, and perform any other tasks required for the OSGI component to be ready for execution.

 

 

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
June 23, 2023

@aemuser2345 

 

1. `run()` method: This method is the main entry point for the scheduled task's logic. It contains the code that executes when the scheduler is triggered according to its defined schedule. The `run()` method is responsible for performing the desired operations or executing the specific task for which the scheduler is designed.

 

For example, let's say you have a scheduler in AEM that is responsible for sending daily email notifications to subscribed users. The `run()` method of this scheduler would contain the logic to fetch the list of subscribed users, compose the email content, and send the email to each user.

Here's a simplified example of a scheduler with the `run()` method:

 

public class DailyEmailScheduler implements Runnable { @Override public void run() { // Fetch the list of subscribed users List<User> subscribedUsers = getUserService().getSubscribedUsers(); // Compose email content String emailContent = composeEmailContent(subscribedUsers); // Send email to each user for (User user : subscribedUsers) { EmailService.sendEmail(user.getEmail(), "Daily Update", emailContent); } } // Other methods and dependencies... }

In this example, the `run()` method is triggered according to the cron expression specified in the `scheduler.expression` property.

2. `activate()` method: This method is called when the scheduler is activated or enabled in the AEM instance. It is typically used to perform initialization tasks or set up any required resources before the scheduler starts running.

or perform initialization tasks when the scheduler is enabled.

Aanchal Sikka