Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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!

1 Accepted Solution

Avatar

Correct answer by
Level 4

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.
        }
    }
}

 

 

 

View solution in original post

3 Replies

Avatar

Community Advisor

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-c...

 

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

 

Hope this helps!

 

Avatar

Community Advisor

@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...

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

 

Avatar

Correct answer by
Level 4

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.
        }
    }
}