yet another blog about computer, technology, programming, and internet

Thursday, July 10, 2014

Tentang Microservices

Thursday, July 10, 2014 Posted by Ismail Habib 21 comments
Apa sih microservices itu? Sepertinya tidak ada definisi yang baku. Sebagian orang mengaitkannya dengan SOA (Service Oriented Architecture). Kalau saya pribadi lebih suka menggunakan term "SOA a la UNIX" untuk mendefinisikannya. Maksudnya disini adalah setiap service akan fokus pada satu fungsionalitas tertentu. Satu service dan service lainnya saling berkomunikasi untuk menghasilnya business value yang sesungguhnya.

Sedikit latar belakang, saya bukan ahli dalam bidang ini. Hanya kebetulan topik ini sedang banyak didiskusikan, seperti yang dituliskan juga di report Technology Radar 2014nya ThoughtWorks. Reportnya bisa dilihat di sini: http://thoughtworks.fileburst.com/assets/technology-radar-july-2014-en.pdf.

Kalau diskusi tentang microservices, sepertinya tidak lengkap kalau tidak dimulai dari arsitektur monolitik. Arsitektur monolitik, seperti namanya, punya arti bahwa semua fungsionalitas berada pada satu unit saja. Unit ini menangani segalanya, business logic, database access, templating, dll. Dalam kasus web application, struktur aplikasi monolitik biasanya terdiri dari client-side, server-side, dan database. Arsitektur monolitik ini tentunya punya kelemahan-kelamahan yang solusinya bisa dialamatkan oleh microservices.

Apa saja kekuatan microservices dibandingkan dengan monolitik?
Yang pertama, microservices memberikan fleksibilitas bagi developer untuk menentukan tools yang akan digunakan untuk implementasi sebuah service tanpa harus memaksakan tools tersebut untuk digunakan pada service-service lainnya. Hal ini sangat memungkinkan karena antara satu service dan service lainnya tidak tightly coupled. Komunikasi antara satu service di microservices dengan service lainnya umumnya dilakukan via messaging atau web service. Keduanya bisa diimplementasi dengan menggunakan teknologi yang berbeda.
Kedua, microservices memungkinkan team splitting dilakukan dengan lebih mudah. Satu tim yang menangani sebuah microservice bisa dengan mudah memisahkan diri dari tim lainnya karena, mereka tidak berbagi code base dan hanya terikat dengan interface yang didefinisikan di dalam service-service tersebut.
Ketiga, memisahkan fungsionalitas dalam service-service terpisah menjadikan service-service tersebut terisolasi satu sama lain. Dengan kata lain, resiko dan problem dalam satu service akan lebih mudah dilokalisir. Termasuk di dalamnya adalah permasalahan dengan legacy system. Salah satu contohnya adalah website The Guardian yang mulai dengan aplikasi monolitik dan kemudian berevolusi menjadi microservices. Aplikasi yang dikembangkan di awal tetap dapat dipertahankan meskipun sudah menjadi legacy dan tidak fleksibel.
Keempat, microservices memungkinkan pembedaan profil satu service dengan service lainnya, terutama yang berhubungan dengan scaling up.

Masih banyak keunggulan microservices yang tidak saya sebutkan disini, terutama karena empat poin yang saya sebutkan di atas adalah major concern yang saya rasakan selama bekerja sebagai software engineer. Yang perlu diingat, selain punya kekuatan, seorang sofware architect juga harus menyadari tantangan yang dihadapi dalam mengimplementasikan sistem dengan arsitektur microservices. Tapi mungkin ini bahasan di posting yang lain :)

Menulis Kembali

Thursday, July 10, 2014 Posted by Ismail Habib 24 comments
Sudah agak lama blog ini dibiarkan tanpa tulisan baru. Untuk menyegarkan kembali motivasi menulis, rencananya saya akan mengubah target pembaca dan topik tulisan. Kemungkinan besar kedepannya blog ini akan diisi dengan tulisan dalam Bahasa Indonesia.

Sunday, January 27, 2013

Java and Lombok

Sunday, January 27, 2013 Posted by Ismail Habib 22 comments
If you are an Indonesian, it's pretty easy to make a connection between Java and Lombok. However, this post is not about that Java and Lombok, but rather for the Java programming language and Project Lombok.

One of the drawback of Java language is its verbosity. One very easy example can be found in any typical Java POJO.

public class Order{

  private long id;
  private String name;
  private int size;

  public Order(){
  }

  public Order(long id){
    this.id = id;
  }

  public long getId(){
    return id;
  }

  public void setId(long id){
    this.id = id;
  }

  public String getName(){
    return name;
  }

  public void setName(String name){
    this.name = name;
  }

  public int getSize(){
    return size();
  }

  public void setSize(int size){
    this.size = size;
  }

  @Override
  public String toString(){
    return String.format("%s %d %d", id, name, size);
  }
}


Already that much code just for a very simple structure with constructors, getters, setters, and toString method. This is where Lombok comes to save the day! Instead of writing that much code, you can have something like this:

@Data
public class Order{

  private long id;
  private String name;
  private int size;
}

...and that's it! A clean, nice code that is easy to maintain. The @Data annotation will tell Lombok to generate the constructor, getters and setters, toString method (and even equals and hashCode method!) all during compilation time! (instead of hiding the code in other file ala AspectJ). Since Lombok can be integrated with your IDE, you will not lose the nice content assist or any other feature that you currently enjoying.

There are several other features provided by Lombok (see: Lombok features), although personally for me, this one is going to be the one I use the most. I am not too scared of introducing Lombok dependency to my project since Lombok itself presented a nice way to stop using Lombok dependency with a tool called "delombok". By using delombok, all magically generated code will be written to the source code and the dependency to Lombok library will be removed.

The only drawback I can think of is whenever I rename one of the field name and the rest of code which refer to the getter/setter will also need to be updated manually (instead of having them automatically updated using a refactor tool in your IDE), but it is a very small price to pay (I guess).

Saturday, January 07, 2012

Software of the day: Fence

Saturday, January 07, 2012 Posted by Ismail Habib , 37 comments

Fence is a software created by Stardock that helps organize icons/files in your Windows's desktop. Basically what a user can do is to create a "fence" (kind of group) and associate icons of your choice into it. The fences are blocks that does not enlarge/shrink according to the number of icons/files you put there, but instead the size are determined by the user themselves. Thus, no matter how many icons/files you have, it will still look like it's very well organized ;)

For someone like me who loves to put everything in desktop (who doesn't?), Fence is a must-have-tool. Quite often I put something that I would need immediately, but most probably wouldn't be for long in the desktop. Fence help he organize it so that in the future it is very easy to find find and remove them. Another nice thing that I like: Fence does not separate the icons in different fences to actual different directories, so it does not mess up the files location.

Fence is free, and if you want more features, they have a paid version for it as well.

screen shot was taken from Fence website

Monday, May 02, 2011

Using ClientLogin to do Authentication for App Engine Application

Monday, May 02, 2011 Posted by Ismail Habib , 30 comments
General information about ClientLogin: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

Discussion on Google Groups (including the original solution posted by geoffd123):
http://groups.google.com/group/google-appengine-java/browse_thread/thread/c96d4fff73117e1d

My solution uses Apache Http library instead of HttpUnit.
public static String loginToGoogle(String userid, String password,
   String appUrl) throws Exception {
  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost(
    "https://www.google.com/accounts/ClientLogin");
  
  MultipartEntity reqEntity = new MultipartEntity();
  reqEntity.addPart("accountType", new StringBody("HOSTED_OR_GOOGLE",
    "text/plain", Charset.forName("UTF-8")));
  reqEntity.addPart("Email", new StringBody(userid));
  reqEntity.addPart("Passwd", new StringBody(password));
  reqEntity.addPart("service", new StringBody("ah"));
  reqEntity.addPart("source", new StringBody(
    "YourCompany-YourApp-YourVersion"));
  post.setEntity(reqEntity);
  HttpResponse response = client.execute(post);
  if (response.getStatusLine().getStatusCode() == 200) {
   InputStream input = response.getEntity().getContent();
   String result = IOUtils.toString(input);
   String authToken = getAuthToken(result);
   post = new HttpPost(appUrl + "/_ah/login?auth=" + authToken);
   response = client.execute(post);
   Header[] cookies = response.getHeaders("SET-COOKIE");
   for (Header cookie : cookies) {
    if (cookie.getValue().startsWith("ACSID=")) {
     return cookie.getValue();
    }
   }
   throw new Exception("ACSID cookie cannot be found");
  } else
   throw new Exception("Error obtaining ACSID");
 }
A simple example on how to use it:

String authCookie = logonHelper.loginToGoogle("email@gmail.com",
   "password","http://yourapp.appspot.com");  
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://yourapp.appspot.com/service");
get.setHeader("Cookie", authCookie);
HttpResponse response = client.execute(get);
response.getEntity().writeTo(System.out);

Thursday, February 03, 2011

GWT and Delayed Execution for Google Maps Geocoding

Thursday, February 03, 2011 Posted by Ismail Habib , 25 comments
Google Maps provide us with an asynchronous way of doing a geocoding, which is pretty much how GWT is dealing with all type of client-server communication. However, sometimes we need a way to synchronize them. By synchronizing I mean waiting for the required asynchronous call to return a value before continue with other execution. Using something like Timer to wait for a reply doesn't work since it will only block the whole process on the browser since JavaScript interpreter is single-threaded. Fortunately, there is a way to deal with it within GWT by using DeferredCommand.

I'm going to take an example of using Geocoding from Google Maps. What I would like to do is geocode two address into latitude, longitude coordinate and use them as parameters for another method.

Geocoder geocode = new Geocoder();
final Callback firstCallback = new Callbak();
geocode.getLatLng("Jl. Tb Ismail Bandung Indonesia", firstCallback);
final Callback secondCallback = new Callbak();
geocode.getLatLng("Jl. Ganesha Bandung Indonesia", secondCallback);
final Command command = new Command() {
   @Override
   public void execute(){
      if ((firstCallback.location != null) 
            && (secondCallback.location != null)) {
         //call the method
         anotherMethod(firstCallback.location, secondCallback.location);
      } else {
         DeferredCommand.addCommand(this); //delay execution
      }
   }
}
command.execute();

With "Callback" as a class to temporarily store value from geocode.

class Callback implements LatLngCallback {
   public LatLng location;

   @Override
   public void onFailure() {
      //put something here
   }

   @Override
   public void onSuccess(LatLng point) {
      location = point;
   }
}

This works for me well, however, I do realize that DeferredCommand is now deprecated. Any other solution?

Tuesday, December 21, 2010

Decoding Polylines from Google Maps Direction API

Tuesday, December 21, 2010 Posted by Ismail Habib , , 36 comments
The Google Maps Directions API provides a way to retrieve directions data from the back-end as opposed to the original JavaScript version when one should retrieve the data from browser. Google provides the routing results as an encoded polylines format. as explained in the Encoded Polyline Algorithm Format. The JavaScript version contains a library for both Encoding and Decoding, which is not the case for a back-end solution written in another language.

Since I'm more interested in the decoding part for Java, here is a code that can be used:

import java.util.ArrayList;

public class PolylineDecoder {
 public static ArrayList decodePoly(String encoded) {
  ArrayList poly = new ArrayList();
  int index = 0, len = encoded.length();
  int lat = 0, lng = 0;
  while (index < len) {
   int b, shift = 0, result = 0;
   do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
   } while (b >= 0x20);
   int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
   lat += dlat;
   shift = 0;
   result = 0;
   do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
   } while (b >= 0x20);
   int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
   lng += dlng;
   Location p = new Location((((double) lat / 1E5)),
     (((double) lng / 1E5)));
   poly.add(p);
  }
  return poly;
 }
}

"Location" is a self-defined, simple class that I use to store coordinate (Latitude and Longitude). Google Map's LatLng is not used because this is for backend.

public class Location implements IsSerializable {

 private double latitude;

 private double longitude;

 public Location() {

 }

 public Location(Double latitude, Double longitude) {
  this.latitude = latitude;
  this.longitude = longitude;
 }
 
 /**
  * @return the latitude
  */
 public double getLatitude() {
  return latitude;
 }

 /**
  * @return the longitude
  */
 public double getLongitude() {
  return longitude;
 }
}

This PolylineEncoder class code is taken from JeffreySambels.com with several modifications.