Sitemap
Mark

Mark

Frontend engineer

Feature flags in Struts 2 applications

3 min readAug 29, 2021

Feature flags or toggles are a handy technique for releasing your changes to production without much worry as they won’t be visible to actual users until you decide to make them visible. Contrast feature flags to feature branches. If the branch is long running, it may diverge from the main branch. We’ve been using feature flags for this use case when it takes many weeks to complete a feature. The feature branch is merged often into the main, but the feature or changes aren’t exposed to users.

Here’s the definition of feature flag per wikipedia:

> a technique in software development that attempts to provide an alternative to maintaining multiple branches in source code(known as feature branches), such that a feature can be tested even before it is completed and ready for release. A feature toggle is used to hide, enable or disable the feature during runtime. For example, during the development process, a developer can enable the feature for testing and disable it for other users.

Press enter or click to view image in full size

Feature flags help in minimizing risk and modifying the application behavior in production without disruptive changes.

At work, we use Struts 2, Java and Spring Boot to maintain a large website. We’re gradually migrating to React and Node.js but it’ll be some time before we achieve that goal. So I decided to write a tutorial showing how to use feature flags with Struts 2. We use Unlaunch Java SDK which is open source and free.

First, you need to add the dependency to pom.xml (or Gradle)

<dependency>            
<groupId>io.unlaunch.sdk</groupId>
<artifactId>unlaunch-java-sdk</artifactId
<version>1.0.0</version>
</dependency>

Then in the Main action (or wherever you need to use it,) initialize the Unlaunch client and access the feature flag.

Get Mark’s stories in your inbox

Join Medium for free to get updates from this writer.

Note: you’ll need to visit unlaunch.io to create a free account and a new feature flag. Here’s what my flag looks like:

Press enter or click to view image in full size

In my hypothetical example, I have created a new UI altogether and saved it as `/index_new.js`. Let’s use feature flag to decide whether to show this UI or not.

package com.tchatapp.chats.action;import com.opensymphony.xwork2.ActionSupport;
import io.unlaunch.UnlaunchClient;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Main extends ActionSupport {private static UnlaunchClient ulClient;private static synchronized UnlaunchClient getUnlaunchClient() {
if (ulClient == null) {
ulClient = UnlaunchClient.create("SDK-KEY");
try {
ulClient.awaitUntilReady(5, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException e) {
System.out.println("Client wasn't ready, " + e.getMessage());
}
}
return ulClient;
}
public String execute() {
UnlaunchClient client = getUnlaunchClient();
// get variation
String variation = client.getVariation("chat-new-layout-2", UUID.randomUUID().toString());
System.out.println("unlaunch variation " + variation);
if (variation.equals("on")) {
return "ON";
} else {
return "OFF";
}
}
}

Then in struts.xml based on the outcome of the feature flag, we can choose to display new or old UI.

<package name="default" namespace="/" extends="json-default">
<default-action-ref name="index"/>
<action name="index">
<result>/index.jsp</result>
<action name="index" class="com.tchatapp.chats.action.Main" method="execute">
<result name="OFF">/index.jsp</result>
<result name="ON">/index_new.jsp</result>
</action>
<action name="token" class="com.tchatapp.chats.action.TokenGenerator" method="execute">
<result type="json"/>
</action>

Instead of creating a new UI, you can also pass feature flag variation to the JSP and then use templating to decide whether to show something or not. The possibilities are endless.

Here’s the source code of the application. https://github.com/mysticbacon/Struts2ChatAppFeatureFlag

Hope you enjoyed this article.

--

--

Mark
Mark

Written by Mark

0 followers

Frontend engineer