Run modes in AEM allow you to configure AEM instances for specific purposes.
For example, We can configure environment specific OSGI Services meta data manually from felix console, But the problem here is whenever new build is deployed the OSGI meta data reset to default values , so again we have to configure. Instead of doing manually in felix console, we can create run mode config nodes in repository, sling will take these configuration automatically when we start the instance or config node update based on run mode values.
- Different types of run modes.
- Standard run modes set up.
- Custom run modes set up.
- Create run mode nodes in the repository.
- Order of precedence of run modes.
- Read Sling run modes in Java
There are two types of run modes.
- Standard run modes – author,publish,samplecontent,nosamplecontent
- Custom run modes – dev,qa,stage,prod ..etc.
We can configure standard run modes directly by renaming jar file ,Standard run modes are used at installation time and then fixed for the entire lifetime of the instance, they cannot be changed.
The naming convention to for AEM 5.6.1 and later is:
1. author – Run mode for author instance.
2. publish – Run mode for publish instance.
3. samplecontent – Run mode will install sample content.
4. nosamplecontent – Run mode will not install sample content.
We can define custom run modes different ways ,below are ways .
Using the sling.properties file :
The sling.properties file can be used to define the required run mode:
- Edit the configuration file:
- Add the following properties; the following example is for author:
Using the -R option :
A custom run mode can be activated by using the -r option when launching the quickstart. For example, use the following command to launch a AEM instance with run mode set to dev.
java -jar cq-56-p4545.jar -r dev
Using a system property in the start script:
- A system property in the start script can be used to specify the run mode.
For example use the following to launch an instance as a production publish instance located in the US:
Here my question is what happens if we did all above custom run mode configuration at a time,which one will take precedence .
below are the order of precedence of run modes.
- Start jar (by double clicking) – In this you do not have option to set run mode in sling.properties, start script first time. JAR name takes precedence.
- Unpack jar and specify run mode as system properties in start script – JAR name doesn’t comes to picture here. In this you do not have option to set run mode in sling.properties. System properties takes precedence.
- Even if we change run mode in JAR name, it doesn’t changes the installation time run mode. For custom run mode, JAR file name is not applicable. Order of precedence is sling.properties -> specifying -r option (command line jar option) -> system properties (start script).
We can read sling run modes directly from SlingSettingsService OSGI service.
[code language=”java”] import org.apache.sling.settings.SlingSettingsService;[/code]
private SlingSettingsService slingSettingsService;[/code]
[code language=”java”]slingSettingsService.getRunModes(); [/code]