Code migration into cloud | Community
Skip to main content
JakeCham
Level 6
January 2, 2024
Solved

Code migration into cloud

  • January 2, 2024
  • 3 replies
  • 757 views

 

Above code in AEM 6.5. How to convert it in AEM as a cloud service ?

 

a. Use Sling Event Listeners 

b. Use Sling Scheduler jobs

c. Use Polling importer 

d. Use Sling model exporter 

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 Bhuwan_B

@jakecham With AEM as a Cloud Service, now the application will be running on cluster nodes which means instances can go down and new ones can come up any time. The code must be resilient especially as an instance might be stopped at any point in time. Refer below article for implementation details.

https://sudeshaempodcast.wordpress.com/2021/07/04/aemaacs-slingjobs/

3 replies

Bhuwan_B
Community Advisor
Bhuwan_BCommunity AdvisorAccepted solution
Community Advisor
January 2, 2024

@jakecham With AEM as a Cloud Service, now the application will be running on cluster nodes which means instances can go down and new ones can come up any time. The code must be resilient especially as an instance might be stopped at any point in time. Refer below article for implementation details.

https://sudeshaempodcast.wordpress.com/2021/07/04/aemaacs-slingjobs/

Raja_Reddy
Community Advisor
Community Advisor
January 2, 2024

Hi @jakecham  you can use Sling Scheduler jobs. it will work
1.Create a Job Interface

import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;

public interface MyScheduledJob extends JobConsumer {
}


2.Implement the Job Consumer

import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;

public class MyScheduledJobImpl implements JobConsumer {
@Override
public JobResult process(Job job) {
}

3.Register the Job Consumer

import org.osgi.service.component.annotations.Component;

@Component(service = JobConsumer.class,
property = {
JobConsumer.PROPERTY_TOPICS + "=" + "my/scheduled/job"
})
public class MyScheduledJobImpl implements JobConsumer {

}

4.Configure Scheduler

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@Designate(ocd = MyScheduledJob.Configuration.class)
@Component(service = Runnable.class)
public class MyScheduledJob implements Runnable {
@ObjectClassDefinition(name = "My Scheduled Job Configuration", description = "Configure the interval for the scheduled job.")
public @interface Configuration {
@AttributeDefinition(name = "Scheduler Expression", description = "Cron expression for scheduling the job.")
String scheduler_expression() default "0 0/5 * 1/1 * ? *"; // Run every 5 minutes as an example
}

@Activate
protected void activate(Configuration config) {
...Use the configuration to set up the scheduler
}

@Override
public void run() {
Implement the logic that needs to be executed periodically
 This method will be called when the job is triggered by the scheduler
}
}

In this example, the job consumer(MyscheduledJobImpl)processes the job when it is triggered by the scheduler. The scheduler configuration is typically done using a cron expression to determine the frequency of job execution.


Thanks.