Interview Questions


Hystrix - Multiple Serivce Instance Endpoints

        The following code example implements Hystrix Command for multiple service endpoints.
Assume there are two service endpoints (different hosts/port) providing same service. In this case we would have two hystrix command to call the service at both endpoints. The following code chooses the endpoint(hystrix command) on round robin fashion . Each command will have its own circuit breaker. The properties are configured using archaius.

Java - Lambda Expressions

package com.test;

public class Lambda {

        public static void main(String[] args) {
                Runner r = new Runner();
      , y) -> {
                        System.out.println("Lambda expression executed");
                        System.out.println("hello world");
                        return 5 + x;

      , y) -> {

                        return 5 - x;

      , y) -> 5 * x);

      , y) -> 5 * x + y);


interface Executable {

        int execute(int x, int y);

class Runner {

        public void run(Executable e) {
                System.out.println("inside run method");

Hystrix - Example

package com.test.hystrix;



public class Example {

        public static void main(String[] args){

 class CommandHelloWorld extends HystrixCommand<String> {

    private final String name;

    public CommandHelloWorld(String name) {

Java - Copy 1000 files from one directory to another

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FileHandler {

        public static void main(String[] args) throws Exception {
                String soursePath = "C:/logs";

                int threadCount = 25;
                List<File> files = getFileList(soursePath);
                List<File> threadFiles = new ArrayList<>();

Servlet Filter - Redirect Session Timeout

Use the following to code to override the doFilter method, which redirects to login page on session timeout. The web.xml should be configured to use this filter.

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                String ipAddress = request.getRemoteAddr();
                System.out.println("IP " + ipAddress + ", Time " + new Date().toString());

                boolean authorized = false;
                String username = null;
                String role = null;
                if (request instanceof HttpServletRequest)

Spring MongoDb Example

package com.test;

import java.util.List;


import com.mongodb.MongoClient;

public class MongoSpringTest {

        private static String MONGO_HOST = "localhost";
        private static int MONGO_PORT = 27017;
        public static final String DB_NAME = "employeeDb";

Spring - Test Rest API using RestTemplate

package com.test;

import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.client.RestTemplate;

public class TestRestAPI {

            public static final String SERVER_URI = "http://localhost:8080/SpringTest-0.0.1-SNAPSHOT";
            public static void main(String args[]){
            private static void testGetAllEmployee() {
                RestTemplate restTemplate = new RestTemplate();

JPA - Pagination Searching and sorting

The following code can be used for the following requirements

1.) To retrieve limited records to support pagination
2.) To support pagination searching & sorting

        public List<Employee> listEmployee(int fromValue, int pSize,
                        final String search, final String sort, final String filter,
                        final String order) {

                final CriteriaBuilder criteriaBuilder = entityManager
                final CriteriaQuery<Employee> criteriaQuery = criteriaBuilder

JPA - Pagination Query

Use the following query to fetch records based on limit for Pagination using JPA

public List<Employee> getEmpWithPagination(int fromValue,int toValue)
                return entityManager.createQuery("from Employee", Employee.class).setFirstResult(fromValue).setMaxResults(toValue).getResultList();
                }catch(final Exception e){
                        log.debug("Exception Occured -> "+ExceptionUtils.getFullStackTrace(e));
                        return null;

Java - One or More Arguments - VarArgs

Consider a method in java, which should accept one or more arguments of same type. There are two ways to implement it. Have a look at the following code.

class Test
        public static void main(String[] args) throws Exception
                Test test = new Test();
                test.Process(new String[]{"str1","str2","str3"});
        public void ProcessVarArgs(String st1,String...sts)
                for(String s:sts)
        public void Process(String[] sts) throws Exception