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.

Feature Dependencies

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.

Togglz Configuration

VersionTogglzConfiguration specifies the Togglz configuration for the example It implements the TogglzConfig interface. The methods, declared in VersionTogglzConfiguration, serve the following functions:

  • getFeatureClass returns the Java enum used in the example application, CustomerFeatures.
  • getStateRepository returns the type of repository where the state of our toggle will be persisted. In this example, the state is persisted in a file named located in the temporary directory.
  • getUserProvider returns 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 REST_VERSION_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 customerServicev1 and customerServicev2 where the former is in active state.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s