Pages

Thursday 28 November 2019

Check MongoDB connection using MongoTemplate in Spring Boot

@Autowired
private MongoTemplate mt;

public String ping()
{
    DBObject ping = new BasicDBObject("ping", "1");
    try {
        CommandResult answer = mt.getDb().command(ping);
        return answer.getErrorMessage();
    } catch (Exception e) {
        return e.getMessage();
}

Wednesday 16 October 2019

Heaven or Hell Puzzle

Puzzle:
You are standing before two doors. One of the path leads to heaven and the other one leads to hell. There are two guardians, one by each door. You know one of them always tells the truth and the other always lies, but you don’t know who is the honest one and who is the liar.
You can only ask one question to one of them in order to find the way to heaven. What is the question?
This is one of the classic puzzles asked in Infosys interview.
Puzzle Solution:
The question you should ask is “If I ask the other guard about which side leads to heaven, what would he answer?”. It should be fairly easy to see that irrespective of whom do you ask this question, you will always get an answer which leads to hell. So you can chose the other path to continue your journey to heaven.
This idea was famously used in the 1986 film Labyrinth.
Here is the explanation if it is yet not clear.
Let us assume that the left door leads to heaven.
If you ask the guard which speaks truth about which path leads to heaven, as he speaks always the truth, he
would say “left”. Now that the liar , when he is asked what “the other guard (truth teller) ” would answer, he would definitely say “right”.
Similarly, if you ask the liar about which path leads to heaven, he would say “right”. As the truth teller speaks nothing but the truth, he would say “right” when he is asked what “the other guard( liar ) ” would answer. So in any case, you would end up having the path to hell as an answer. So you can chose the other path as a way to heaven.

Frog and Well Puzzle

Puzzle:
A frog is at the bottom of a 30 meter well. Each day he summons enough energy for one 3 meter leap up the well. Exhausted, he then hangs there for the rest of the day. At night, while he is asleep, he slips 2 meters backwards. How many days does it take him to escape from the well?
Puzzle Solution:
28 days
Day 1 – It jumps 3 meters. 0 + 3 = 3.
Then falls back 2 at night. 3 – 2 = 1
Day 2 – It jumps 3 meters. 1 + 3 = 4.
Then falls back 2 at night. 4 – 2 = 2.
Day 27 – It jumps 3 meters. 26 + 3 = 29.
Then falls back 2 at night. 29 – 2 = 27.
Day 28 – It jumps 3 meters. 27 + 3 = 30

Tuesday 15 October 2019

Print all the Spring beans that are loaded - Spring Boot

As shown in the getting started guide of spring-boot: https://spring.io/guides/gs/spring-boot/

@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Bean
  public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

      System.out.println("Let's inspect the beans provided by Spring Boot:");

      String[] beanNames = ctx.getBeanDefinitionNames();
      Arrays.sort(beanNames);
      for (String beanName : beanNames) {
        System.out.println(beanName);
      }
    };
  }    
}
This will not list manually registered beans.
In case you want to do so, you can use getSingletonNames(). But be careful. This method only returns already instantiated beans. If a bean isn't already instantiated, it will not be returned by getSingletonNames().

Code Review

 SOLID Principles S – Single Responsibility Principle There should never be more than one reason for a class to change. O – Open-Closed Prin...