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!
Solved! Go to Solution.
Views
Replies
Total Likes
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.
}
}
}
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:
Please refer to the below links for implementation help.
https://blogs.perficient.com/2018/12/16/how-to-refresh-osgi-r6-components-on-bundle-activation/
Hope this helps!
@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://sling.apache.org/documentation/bundles/repository-initialization.html
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.
}
}
}
Views
Likes
Replies