Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3750

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T05:36:07+00:00 2024-11-26T05:36:07+00:00

Danni, Johny and the Java Feature Flag Problem

  • 61k

What is Feature Toggle

Imagine you are building a new feature for your Java application. You want to test the feature with a small group of users before you roll it out to everyone. You could do this by creating two versions of your application, one with the new feature enabled and one with the new feature disabled. But this would require you to deploy two different versions of your application to production, which could be time-consuming and difficult.

Instead, you could use a feature toggle. A feature toggle is a variable that you can use to enable or disable a feature in your application at runtime. This means that you can deploy a single version of your application to production, and then use the feature toggle to control whether the new feature is enabled for different users.

The Danny and Johny Problem

Imagine a situation Danny is the only daughter of Big-B for which Danny's father
is too protective of her. He never allowed Danny for party or any outing. He works in an hospital as a doctor on shift basis and its not certain when he will go to hospital and come back. Danny has a boyfriend Johny who is too much partyholic and he never gots time to chill-out with danny. So, together they found out a solution. They bribed a Ramu Kaka who runs tea stall in front of hospital, who will inform wheather Big-B is in hospital or left for hoe, so that in that time both Johny and Danny can party together.

Let's Understand chronology

  1. PAPA_HOME is the feature that Johny and Danny are dependent
  2. Johny will call Danny for party
  3. Danny will be keep looking for Ramu Kaka's message.
  4. They will decide if they will go for party or not based on Ramu Kaka's message.

Problem image

Solution

For the solution we have several libraries available, I went with FF4J(Feature Flag for java). Below are the Code Snippet with Explanation.

1. Head to Spring Intializer and create a starter spring boot project.

I don't give a sh*t about which version you are using, But if you are using Spring Boot 3.x you have to use Java 17 or above. Gradle or Maven? I don't care. I went with Gradle.

2. Inject the following line to your dependency for gradle.

implementation 'org.ff4j:ff4j-core:2.0.0'.
If you are using maven, go to Maven Central Repository and find your own dependency,

3. Create A Java configuration something like FF4JConfig or whatever the f*ck you want.

Where to create? This piece of sh*t is not for you.

4. Add the following lines.
@Configuration //This piece of shit will tell spring boot that this is a configuration class public class FF4jConfig {     //Feature Name Definition     public static final String PAPA_FEATURE = "PAPA_FEATURE";      /*     * FF4J Configuration     * Create one bean of type FF4J. Why? Because concept of overriding is dying     * Create of instance of FF4j. Obviously to use it further     * Register your Feature     * Enable or disable according to you     * You are done, Thank me later (Code Below ⬇️)_     */     @Bean     public FF4j ff4j(){         FF4j ff4j = new FF4j();         Feature papaFeature = new Feature(PAPA_FEATURE);         papaFeature.disable(); //Default we will disable this feature later we will be enable         ff4j.createFeature(papaFeature);         return ff4j;     } } 
Enter fullscreen mode Exit fullscreen mode

5. Define your API End point for Ramu Kaka and Johny to communicate.
    @GetMapping(path = "/should-we-party")     public String shouldWeParty(){         if(ff4j.check(FF4jConfig.PAPA_FEATURE))             return "Yeah Johny Lets Go for party";         else             return "Sorry Johny, Papa is at Home";     }      /*      * End-point will change the status of the feature      * In our case it will notify johny about status of papa      */     @PostMapping(path = "/papa-at-hospital/{isAtHospital}")     public String papaAtHospital(@PathVariable boolean isAtHospital){         if(isAtHospital){             ff4j.enable(FF4jConfig.PAPA_FEATURE);             return ("Message deliverd to danny: PAPA IS AT HOSPITAL, GO PARTY");         }         else {             ff4j.disable(FF4jConfig.PAPA_FEATURE);             return ("Message deliverd to danny: PAPA LEFT HOSPITAL, COME BACK");         }     } 
Enter fullscreen mode Exit fullscreen mode

Don't forget to autowire the FF4j to instantiate before using

@Autowired     FF4j ff4j;  
Enter fullscreen mode Exit fullscreen mode

Results

Following is the endpoint for Johny to ask Danny out.

should-we-party endo point

Following is the endpoint for Ramu Kaka to ask Danny out.

papa-at-hospital

Once Ramu Kaka informed Danny that Big-B is in hospital, Johny will get the following response on calling Danny.

Success Response

After getting this response, Johny and Danny went for party. And the rest of the story cleared from browser history

Get the full Code at Github/0x4E43

Thank you

backendjavaspringbootwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.