Monday, May 4, 2020

Java Technical Architect interview : Round - 2

Hi Friends,

In this post, I'm sharing Java Technical Architect interview questions asked in 2nd round.

You can also go through my other interview posts:





Question 1:

What is the difference in using Eureka and spring cloud consul?

Answer:

Eureka and Spring cloud Consul are both Service Discovery tools.
Difference between them lie in multiple factors.

The architecture of Eureka is primarily client/server with a set of eureka servers per datacentre , usually one per availability zone.
Typically clients of eureka use an embedded SDK to register and discover services.

Eureka provides a weak consistent view of services using best effort replication. When a client registers with a server , that server will make an attempt to replicate to the other servers but provides no guarantee.
Service registrations have a short Time-To-Live, requiring clients to heartbeat with the servers. Unhealthy services or nodes will stop heartbeating, causing them to timeout and be removed from the registry.


Consul provides a super set of features, including richer health checking, key/value store and multi datacentre awareness. Consul requires a set of servers in each datacentre , along with an agent on each client, similar to using a sidecar like Ribbon. The Consul agent allows most applications to be consul unaware, performing the service registration via configuration files and discovery via DNS or load balancer sidecars.

Consul provides a strong consistency guarantee since servers replicate state using the Raft protocol. Consul supports a rich set of health checks.
Client nodes in Consul participate in a gossip based health check , which distributes the work of health checking, unlike centralized heartbeating which becomes a scalability challenge.

In Consul, discovery requests are directed to the elected consul leader which allows them to be strongly consistent by default.

The strongly consistent nature of Consul means it can be used as a locking service for cluster coordination.  Eureka does not provide similar guarantee  and typically requires running ZooKeeper for services that need to perform coordination or have stronger consistency needs.



Question 2:

How many types of containers are there in spring framework?


Answer:

There are two types of  Spring IOC containers:


  • BeanFactory Container
  • ApplicationContext Container







Spring BeanFactory container is the simplest container which provides basic support for DI.
It is defined by org.springframeworkbeans.factory.BeanFactory interface. XMLBeanFactory is the main implementation of this interface. It reads configuration metadata from xml files for creating a fully configured application.

BeanFactory container is preferred where resources are limited to mobile devices or applet based applications.

Spring ApplicationContext Container:

It is defined by org.springframework.context.ApplicationContext interface.
The ApplicationContext container has all the functionalities of BeanFactory container. It is generally recommended over BeanFactory container.

The most common implementations are :


  • FileSystemXmlApplicationContext
  • ClassPathXmlApplicationContext
  • WebXmlApplicationContext







Question 3:

What is the difference between @Controller and @RestController?


Answer:

@RestController = @Controller + @ResponseBody

@RestController was added in Spring 4.

@Controller : The job of @Controller is to create a map of model object and find a view.

@ResponseBody: It will just append the result object as JSON or XML in Http response.

@RestController : It simply return the object and object data is directly written into HTTP response as JSON or XML.



Question 4:

What is the difference between @RequestParam and @RequestAttribute?

Answer:

@RequestParam:

This annotation is used to access the query parameters from the HTTP request URL. e.g.:

http://example.com?param1=1&param2=2

In this URL, param1 and param2 are query parameters and they are accessed in methods like:

@RequestMapping("/")
public void method(@RequestParam("param1") String par){

}


@RequestAttribute:

It is used to access objects which have been populated on the server-side  but during the same HTTP request.
 e.g.:

We want to maintain a counter to know the number of visits to that page or number of requests made to a specific URL. So, in this case , we take an interceptor with AtomicInteger to increment the counter on every request.

e.g.:

The Interceptor:

public class Counter  extends HandlerInterceptorAdapter{

    private AtomicInteger counter = new AtomicInteger(0);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{

        request.setAttribute("visitorCounter", counter.incrementAndGet());
    }
 
}

The Controller class:

public class CounterController{

    @RequestMapping("/")
    @ResponseBody
    public String handle(@RequestAttribute("visitorCounter") Integer counter){

        // code here.......
    }
}



That's all for this post.
Hope this post helps everyone in their interview rounds.

Thanks for reading!!




No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...