Sunday, 20 October 2019

REST and SOAP APIs


REST APIs
  • Is an Architectural style that follows six constraints defined by Roy Fielding in 2000. Those constraints are – Uniform Interface, Client-Server, Stateless, Cacheable, Layered System, Code on Demand.
  • Follows HTTP methods and media type is a chosen by the client/server. Most used media type is JSON.
  • Uses annotations like @Path, @RestController, @Mapping etc
  • REST can use SOAP protocol but SOAP cannot use REST.

SOAP APIs
  • Is a protocol that follows a strict standard to allow communication between the client and the server
  • SOAP uses only XML for exchanging information in its message format.
  • Not only tied to HTTP transport but can also be usable by SMTP.
  • Uses @WebService on interface level.
  • SOAP has SSL( Secure Socket Layer) and WS-security.
That technology for Server APIs is extremely depends on usecase. Many developers found SOAP cumbersome and hard to use. For example, working with SOAP in JavaScript means writing a ton of code to perform extremely simple tasks because you must create the required XML structure absolutely every time.

References:
  • https://smartbear.com/blog/test-and-monitor/understanding-soap-and-rest-basics/

Friday, 11 October 2019

Observer Pattern in java

Observer pattern is also refered as Publisher/Subscriber pattern.
Examples:
  • JMS in Java
  • Event listeners like onClick

Applications:
  • When you subscribe to any website
  • Follow feature on Quora/Facebook/Instagram
  • Whatsapp Group

// Subject interface
package example.java.designpatterns;

public interface Subject {
 public void register(Observer observer);
 public void unregister(Observer observer);
 public void notifyObservers();
 public Object getUpdate(Observer observer);
}



// Observer interface

package example.java.designpatterns;

public interface Observer {
 public void update();
 public void setSubject(Subject s);
}




// Class implementing Subject interface
package example.java.designpatterns;

import java.util.ArrayList;
import java.util.List;

public class MyTopic implements Subject {
 private String message;

 private List listeners;

 public MyTopic() {
  listeners = new ArrayList<>();
 }

 public void register(Observer observer) {
  listeners.add(observer);
 }

 public void unregister(Observer observer) {
  listeners.remove(observer);
 }

 public void notifyObservers() {
  for (Observer observer : listeners) {
   observer.update();
  }
 }

 public void pushMessage(String s) {
  System.out.println("MyTopic: Adding message: " + s);
  this.message = s;
  notifyObservers();
 }

 public Object getUpdate(Observer observer) {
  return this.message;
 }
}




// Class implementing Observer interface

package example.java.designpatterns;

public class MyTopicSubscriber implements Observer {
 private Subject topic;
 
 private String name;
 
 public MyTopicSubscriber(String name) {
  this.name = name;
 }
 
 public void update() {
  String message = (String) topic.getUpdate(this);
  System.out.println("MyTopicSubscriber: " + name + " Recieved: " + message);
 }
 public void setSubject(Subject subject) {
  this.topic = subject;
 }
}

Sunday, 29 September 2019

Interview at Reliance Jio

Round 1:
  • Print 1000 random numbers of single digit each. Number should be between 0 to 9.
  • Store the above elements in a array and move all the 0s into one end without changing the order of non-zero elements.
  • Find out the top 50 elements in a stream of 1000 elements. Assume that numbers are streaming and you don't know the size ahead. and if you want to know the frequency of the top 50 elements also, how would you do? Hint: Heap
  • Assume that you can only send 160 characters in one message. If you have a paragraph to send, how do you determine that and send it over network, every page should send which part the message it is, like [1/2]. Hint: Travese entire message first to determine whether there are multiple messages possible.

Round 2:
  • Given array of integers, find out the first non-repeating element.
  • Given M*N matrix of Integers, find out the area covered between 2 cells.
  • Convert a doubly linked list into binary tree.

Round 3:
  • Diff between SOAP and REST APIs.
  • Given a src and dest vertex in a graph, write a method to return true if dest can reachable all possible paths from the src vertex. There can any number of intermediate vertices.
  • [Puzzle] 4 persons of diff speeds need to cross a bridge with one torch where only two can travel at a time from the bridge, what is the minimum time that they take to reach all on the other side. How can you solve this generically.

Wednesday, 11 September 2019

[System Design] Scaling methodologies

Wikipedia says, Scalability is the property of a system to handle a growing amount of work by adding resources to the system. With increase in customer demand, Servers hit the scale problems. There are 2 standard ways of handling the Scalability.
1. Horizon Scaling: Add more servers/Machines when there is a increase in load.
  • Demands load balancing to balance client requests.
  • Results in distributed systems and possible data inconsistency.
2. Vertical Scaling: Add more resources to the same server.
  • Single server holds all the data and hence there will be no data consistency issues.
  • Will result in better IPC as things are local to single server.
  • Can result in Single Point of failure.
  • Cannot go beyond certain hardware limits.

In practical, it is better to use a combination of both to achieve better results. Perform the Vertical scaling and move to Horizontal scaling after some peek limits.

Saturday, 31 August 2019

[Java] Print all the files under a given directory

Print all the files under a given directory. If there are subdirectories traverse them recursively and figure out all the files inside it.

package test.test;

import java.io.File;

public class Test {
   public static void main(String[] args) {
      filePrintUtil("/Users/poddepally/Downloads");
   }

   private static void filePrintUtil(String pathName) {
      File file = new File(pathName);
      File[] listFiles = file.listFiles();
      for (File currentFile : listFiles) {
          if (currentFile.isFile()) {
              System.out.println(currentFile.getName()
                  + ", length(bytes) =" + currentFile.length()
                  + ", parent = " + currentFile.getParentFile().getName());
   } else {
       filePrintUtil(currentFile.getAbsolutePath());
   }
      }
   }
}

Friday, 16 August 2019

Class Loaders in Java

Class loaders in the JVM is responsible for looking out for the requested class and load it on demand. Unless set otherwise, class loaders look at the default class path locations of java installation such as {JRE_HOME}/lib/rt.jar and {JRE_HOME/ext}. rt.jar file holds the java core class files such as String, integer, etc.
You can set the class path of the application by -cp flag while compilation/execution.
Java contains following type of class loaders by default:
  • Bootstrap class loader: Looks for class files such as java.lang.* in rt.jar
  • Extension class loader: Looks for class file in /ext location.
  • Application class loader: Looks for class file in the location set by the application.

Class loaders follows delegation model while looking for class file. That is, class loader will delegate the load process to parent class loader, if not found, takes the responsibility to load the requested class.

Saturday, 3 August 2019

HTTP Status Codes

HTTP status codes are categorised into 5 buckets. These are very helpful during API development such as REST APIs.
Status Code Meaning
1xx informational
2xx Success
3xx Redirection
4xx Client error
5xx Server error