Togglz Java: Feature Toggle for Java Apps
Welcome to TopperBlog! 👋
I'm a tech content creator passionate about helping developers level up their careers and master cutting-edge technologies.
🎯 What I Write About:
• AI/ML Engineering & LLMs
• Web3 & Blockchain Development
• System Design & Architecture
• Interview Preparation (FAANG)
• Freelancing & Remote Work
• Modern Tech Stacks (Next.js, React, Rust, TypeScript)
• Performance Optimization & Best Practices
💼 Mission: Sharing practical, actionable insights that accelerate your tech career and maximize your earning potential.
📚 15+ In-Depth Guides covering everything from earning $10k/month as a freelancer to cracking FAANG interviews.
🌐 Let's connect and grow together in this amazing tech journey!
#TechBlogger #SoftwareEngineering #CareerGrowth #WebDevelopment #AIEngineering
Togglz: Feature Toggle Framework for Java
Togglz is a feature toggle (feature flag) framework for Java applications that enables you to control feature availability at runtime without redeploying code.
Key Features
| Feature | Description |
| Runtime Control | Enable/disable features without redeployment |
| User-Based Toggles | Target features to specific users or user groups |
| A/B Testing | Run experiments with different user segments |
| Admin Console | Web-based UI for managing toggles |
| Multiple Backends | File, database, in-memory, and cloud storage support |
| Spring Integration | Seamless Spring Framework integration |
| Servlet Support | Works with standard Java web applications |
Core Concepts
1. Feature Enum
Define your features as an enum:
public enum MyFeatures implements Feature {
@Label("New Dashboard")
NEW_DASHBOARD,
@Label("Beta Checkout")
BETA_CHECKOUT,
@Label("Advanced Analytics")
ADVANCED_ANALYTICS;
public String name() {
return toString().toLowerCase();
}
}
2. Feature State Provider
Determines if a feature is active:
@Configuration
public class TogglzConfiguration {
@Bean
public FeatureProvider featureProvider() {
return new InMemoryStateRepository();
}
@Bean
public UserProvider userProvider() {
return new UserProvider() {
@Override
public FeatureUser getCurrentUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
return new FeatureUser(auth.getName(), auth.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList()));
}
};
}
}
3. Using Feature Toggles
@Service
public class OrderService {
@Autowired
private FeatureManager featureManager;
public void processOrder(Order order) {
if (featureManager.isActive(MyFeatures.BETA_CHECKOUT)) {
// New checkout logic
processBetaCheckout(order);
} else {
// Legacy checkout logic
processLegacyCheckout(order);
}
}
}
4. Annotation-Based Approach
@Service
public class DashboardService {
@FeatureActive(MyFeatures.NEW_DASHBOARD)
public Dashboard getNewDashboard() {
return new ModernDashboard();
}
public Dashboard getDashboard() {
if (featureManager.isActive(MyFeatures.NEW_DASHBOARD)) {
return getNewDashboard();
}
return getLegacyDashboard();
}
}
Configuration Example
@Configuration
@EnableTogglz
public class TogglzConfig {
@Bean
public TogglzConfig togglzConfig() {
return TogglzConfig.builder()
.featureProvider(new InMemoryStateRepository())
.userProvider(new UserProvider() {
@Override
public FeatureUser getCurrentUser() {
// Return current user
return null;
}
})
.stateRepository(new InMemoryStateRepository())
.console()
.path("/togglz-console")
.enabled(true)
.build();
}
}
State Repositories
// File-based
new FileBasedStateRepository(new File("togglz.properties"))
// Database
new JDBCStateRepository(dataSource)
// In-memory
new InMemoryStateRepository()
// Redis
new RedisStateRepository(redisClient)
Admin Console
Access the web console at /togglz-console to:
- View all features
- Enable/disable features
- Set user-specific activations
- View activation strategies
Activation Strategies
featureManager.isActive(MyFeatures.NEW_DASHBOARD,
new ActivationStrategy() {
@Override
public boolean isActive(Feature feature, ActivationContext context) {
// Custom logic
return context.getUser().getName().startsWith("admin");
}
});
Benefits
✅ Zero Downtime Deployments - Deploy code with features disabled
✅ Gradual Rollouts - Enable for percentage of users
✅ Quick Rollback - Disable problematic features instantly
✅ A/B Testing - Compare feature variants
✅ Operational Control - Non-technical users can manage toggles
Maven Dependency
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
Togglz is essential for modern continuous deployment practices, enabling teams to decouple feature releases from code deployments.