Togglz Integration with REST
This is the continuation of my previous REST with Spring - Hello World example. This example integrates Togglz with the REST example. Togglz proxy factory bean pattern is used in this example. Source code for this example can be found here.
Changes to Maven Dependencies
Add the Togglz dependencies to the existing pom.xml.
VersionFeatures defines the Togglz feature for Hello World REST example.
REST_VERSION_FEATUREfeature switches the REST controller based on the state of the toggle.
@Label annotation specifies the human readable label used in the Togglz Admin Console.
@EnabledByDefault annotation makes the
REST_VERSION_FEATURE enabled by default if no previous state of this flag persisted in the state repository.
VersionTogglzConfiguration specifies the Togglz configuration for the example It implements the
TogglzConfig interface. The methods, declared in
VersionTogglzConfiguration, serve the following functions:
getFeatureClassreturns the Java enum used in the example application,
getStateRepositoryreturns the type of repository where the state of our toggle will be persisted. In this example, the state is persisted in a file named rest_features.properties located in the temporary directory.
getUserProviderreturns the user who can access Togglz Admin Console to modify the state of the toggle (enabled or disabled). In the example, no security is enabled and anyone can access the admin page.
Changes to Business Service Implementation
In the modified REST Hello World example,
CustomerServiceImplv2 is an additional business service implementation.
CustomerServiceImplv2 is similar to the existing class
CustomerServiceImpl class except the former prepends the number 2 to the customer first name.
@Service annotation is removed since the automatic bean detection is not required for any of the implementation classes.
A New Business Service Factory
A new service factory class, CustomerServiceFactory, is introduced to abstract the service implementation class from the controller class. CustomerServiceFactory takes advantage of Togglz’s
FeatureProxyFactoryBean to switch the customer service implementing classes. The
FeatureProxyFactoryBean allows you to create a proxy object, that delegates all calls to one of two beans depending on the state of feature
FeatureProxyFactoryBean is marked with the
@Autowired annotation so that the bean can be dynamically injected into
CustomerServiceFactory during runtime.
CustomerServiceFactory is also marked with @Service for automatic bean detection.
Changes to Spring REST Controller
Our modified controller now refers to
CustomerServiceFactory class instead of
CustomerService. The controller delegates call to the factory instead of the service.
Changes to Dispatcher Servlet Configuration
The rest-dispatcher-servlet.xml file is modified to integrate Togglz’s FeatureProxyFactoryBean with our REST example. The factory requires two properties active and inactive. They must refer to two alternative implementations of same business interface, which is
CustomerService in our example. The alternative bean ids are
customerServicev2 where the former is in active state.
- Once your application is deployed in Tomcat, you can retrieve a list of customers by calling the following URL:
- Access Togglz Admin Console by typing the following URL in a browser:
- Since the Rest Version Feature toggle is enabled, the Status shows up as Green. Click the Action button to change the state of the Rest Version Feature toggle.
- Change the state of the Toggle by disabling the Enabled checkbox and clicking Save.
- Rest Version Feature toggle state now shows up as Red and disabled.
- Now try to access the example by typing the following URL in a browser:
http://localhost:8080/togglz-rest-helloworld/customers. You will notice the ‘2’ prepended to customer names.