Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Difference between Activate and Run in AEM Scheduler

Avatar

Level 3

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!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Community Advisor

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.

 

 

Avatar

Correct answer by
Community Advisor

@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