# Togglz Java: Feature Toggle for Java Apps

# 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:

```java
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:

```java
@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**

```java
@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**

```java
@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

```java
@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

```java
// 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

```java
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

```xml
<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.
