How can I create a Java file that runs only once when my application starts? | Community
Skip to main content
Jniza
New Participant
August 17, 2023
Solved

How can I create a Java file that runs only once when my application starts?

  • August 17, 2023
  • 3 replies
  • 1132 views

Hi there!

We are attempting to configure certain system properties in our Maven project for AEM. We'd like to create a Java class that executes only once during startup.

For instance, consider these configurations:

 

Locale.setDefault(new Locale("es", "ES"));

 

How can we achieve this?

Thank you!

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 jeremylanssiers

You could add the runOnceMethod() in a service's activate() method. That will run once as soon as its gets activated. 

 

Interface

 

public interface MyService { void runOnceMethod(); }

 

 

Implementation

 

( service = MyService.class, configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true ) @Designate(ocd = Configuration.class) public class MyServiceImpl implements MyService { private static final Logger LOG = LoggerFactory.getLogger(MyServiceImpl.class); private Configuration configuration; @ObjectClassDefinition( name = "My Service", description = "My Service containing a method that only runs once." ) public Configuration { @AttributeDefinition(name = "Enabled") boolean enabled() default true; } public void activate(Configuration configuration) { LOG.info("Service activated"); this.configuration = configuration; runOnceMethod(); } public void modified(Configuration configuration) { LOG.info("Service modified"); this.configuration = configuration; } public void deactivate(Configuration configuration) { LOG.info("Service deactivated"); this.configuration = configuration; } public void runOnceMethod() { if (configuration.enabled()) { LOG.debug("Running runOnceMethod"); // Perform some actions. } } }

 

 

 

3 replies

jeremylanssiersAccepted solution
New Participant
August 17, 2023

You could add the runOnceMethod() in a service's activate() method. That will run once as soon as its gets activated. 

 

Interface

 

public interface MyService { void runOnceMethod(); }

 

 

Implementation

 

( service = MyService.class, configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true ) @Designate(ocd = Configuration.class) public class MyServiceImpl implements MyService { private static final Logger LOG = LoggerFactory.getLogger(MyServiceImpl.class); private Configuration configuration; @ObjectClassDefinition( name = "My Service", description = "My Service containing a method that only runs once." ) public Configuration { @AttributeDefinition(name = "Enabled") boolean enabled() default true; } public void activate(Configuration configuration) { LOG.info("Service activated"); this.configuration = configuration; runOnceMethod(); } public void modified(Configuration configuration) { LOG.info("Service modified"); this.configuration = configuration; } public void deactivate(Configuration configuration) { LOG.info("Service deactivated"); this.configuration = configuration; } public void runOnceMethod() { if (configuration.enabled()) { LOG.debug("Running runOnceMethod"); // Perform some actions. } } }

 

 

 

Harwinder-singh
New Participant
August 17, 2023

@jniza If you are looking to configure/create something at the time of repository initialization, you can consider Repo init scripts.

 

Some useful resources are here. 

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager-blogs/getting-started-with-apache-sling-repo-init-aem-community-blog/ba-p/373373

https://sling.apache.org/documentation/bundles/repository-initialization.html

 

sherinregi-1
New Participant
August 17, 2023

Hi @jniza 

You can create a class that implements bundleactivator and then override the start method which will execute once the bundle start.

 

After creating the class please add the class as part of the POM 

eg:

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
...
<Bundle-Activator>com.xx.xx.xx.CustomBundleActivator</Bundle-Activator>
</instructions>
</configuration>
</plugin>

Please refer to the below links for implementation help.

 

 https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/use-of-activator-java-in-cq-bundle/td-p/179319

 

https://blogs.perficient.com/2018/12/16/how-to-refresh-osgi-r6-components-on-bundle-activation/

 

Hope this helps!