Friday, November 21, 2014

Java-OOP fermentation

Java is a wonderful language. No doubt about that. Almost all my generation of university students were taught java. Most of them who became software engineers end up doing Java for the rest of their lives. Java and Object oriented principles (OOP) (also taught in uni) complements each other very well. If you love Java, you love OOP. When you only work with Java and OOP for years, with every passing year, there is something happening in-side you. I call it "Java-OOP fermentation". While fermentation has a positive effect on milk hence yoghurt, it has less desirable outcome on Java and OOP.

Here are some clues to know whether your Java-OOP is fermented.
  1. Your day-today thinking is based on objects and behaviours. Ball is an object and bounce is a behaviour of that object for you. ;)  (which is horribly wrong)
  2. When you see a class full of static methods,  you go "WTF?" and remove statics.
  3. You instantly vomit on your desk and leave the office if you happen to see a new key word.
  4. You believe that the purpose of life is to inject more and more beans as dependencies to surrounding beans.
  5. You usually get in to bar fights while arguing IOC is not DI.
  6. You believe, Mockito is not a mocking framework. Its the meaning of testing.
  7. You feel miserable, when someone refer to one of your stubs as a mock, and you instantly interrupt the conversation and correct them "Well..Its not a mock, its a stub"
  8. You either believe 1) Never test Private methods. Never. or I kill you. or 2) Test every method, or I kill you.
  9. You feel, POJOs are the purest form of code and extracted from earth.
  10. You wonder why "Organic" is not part of - POJO.
  11. You feel, annotations are so synthetic and never mix with your organic POJOS.
  12. You are big on layers.
  13. You love data transfer objects. To transfer data among Layers. Then Mappers. Enrichers...!
  14. Your DTOs lives in their own layers. (more layers)
  15. You usually get in to bar fights while discussing what level of testing you should do, Unit or Integration or functional and who does what.
  16. For you, curly braces defines the scope of everything. You are big on where to put the curly braces and they should ALIGNE !!
  17. For you, a language without curly brace is no language. Period.
  18. Your head spins and feeling bloated if you happen to see a Clojure snippet.
  19. You are proud of the speed of  for loop and you really feel it when it loops, and you think its mechanical sympathy.
  20. You use ORM. Use Hibernate. All the way. Everyday.
  21. You think, ORMs ? NO WAY ! It kills performance. Lets hand code SQL. All the way. Everyday.

The list goes on.. ;)

Non of these are essentially wrong. Actually, all these are fine and should consider doing in moderate. But not every time, every day, every where. There is no silver bullet, and there is no *one absolute right way*.  But once your Java-OOP fermented, you become highly opinionated and end up choosing camps. For an instance, if you can't think of writing unit tests without mocking the world using Mockito ? There is certainly something wrong !

Docker - Part 1

Never heard of docker ?

I found this docker explanation is the quickest to get up-to speed with docker. Also docker official documentation is more complete and up to date.

So what is docker ?

In short its a better "VM orchestration tool".

But a docker VM is not a VM you used to know. Usually the VM you know is sort of itchy, bloated and makes you vomit. Docker VM (to be more precise, we call it docker container) is like a Pineapple juice in a desert!

Lets cut to the chase.

Docker has two parts. Docker client and Docker server (Docker Host). Docker host can have multiple docker containers. Docker server expose REST Api which the docker client use to manipulate docker containers in the docker server.

Docker Host should run in a Linux box. In MAC, we can use boot2docker VM to run docker host.

Starting up in MAC

boot2docker up   ← How to start docker in MAC.

 asanga@MacBoo-Pro:~$ boot2docker up  
 Waiting for VM and Docker daemon to start...  
 Writing /Users/asanga/.boot2docker/certs/boot2docker-vm/ca.pem  
 Writing /Users/asanga/.boot2docker/certs/boot2docker-vm/cert.pem  
 Writing /Users/asanga/.boot2docker/certs/boot2docker-vm/key.pem  
 To connect the Docker client to the Docker daemon, please set:  
   export DOCKER_TLS_VERIFY=1  
   export DOCKER_HOST=tcp://  
   export DOCKER_CERT_PATH=/Users/asanga/.boot2docker/certs/boot2docker-vm  

Congratulations. Your docker host is up !! 

But, Now what ? Host is up. But if you want to control the host to do anything useful, you need a remote control to your docker host, which is a docker client.

Remember : docker host is just a server with a REST API. You need a docker client which talks to that API to interact with docker.

Ok lets connect a docker client to the host we just spun up. docker client needs to know about your docker host in-order to connect (Usually in production environments, docker client and docker hosts are in different boxes)

So how does docker client know about docker host ? Your guess is right. Via Environmental Variables.

So here we set those.

   export DOCKER_HOST=tcp://  
   export DOCKER_CERT_PATH=/Users/asanga/.boot2docker/certs/boot2docker-vm 

If you ask me how I knew what those variables and their values, and if I tell you it comes with experience and practise, then you believe me..mmm thats alright ;)

But if you look at the out put of the first command boot2docker up at the end, last 4 lines, Docker actually gives you those values ;)

boot2docker ip  ← shows docker host ip
boot2docker ssh ← How to connect to docker server in MAC
Or you can use traditional ssh command.  
ssh docker@
pass: tcuser

See stuff

docker ps ← Show all docker containers
docker images ← Show all docker images
docker logs CONTAINER_NAME  ←Show logs from running container
docker logs 5d3852fa1aef

Start continers

docker run -d IMAGE_NAME command ← Starts container in Deamon mode
docker run -P IMAGE_NAME command ← Starts with all ports exposed to docker host
docker run -d -P --name web nginx    ← Starts nginx deamon with all ports published
docker run -t -i IMAGE_NAME /bin/bash  ← Jump in tp docker container

Port maps are in this format.  HOST:PORT -> CONTAINER:PORT
docker run -p 8080:8080 IMAGE COMMAND

Remove / Stop stuff
docker stop CONTAINER_NAME ← Stops container
docker rmi 195eb90b5349  ← Removes docker image by image id
docker rm 195eb90b5349  ← Removes docker container by container id
docker rm $(docker ps -a -q) ← Remove all docker containers