Monday, February 21, 2022

Code that speaks up

 Writing code which talks is something the developer would love to have it. In this blog, we will talk about some of the mechanism which makes the code vocal.

Optional classes:

A very handy feature supported since Java8. However we have to use it to the extent it make things better.

  • To avoid nasty NPE at run time, Optional classes is a convenient way of forcing the user
  • Overusing Optional may become  problematic 
  • Constructor and method parameters
  • POJOs, will be an issue in reflection
  • Optional class dependency in the stateless bean


Monday, February 7, 2022

Reliable Message Delivery with RabbitMQ

 In RabbitMQ, data safety is being handled by the below two mechanism:

 I. Consumer Acknowledgements

II.  Publisher confirms


Consumer Acknowledgements:
Assuming there is no auto acknowledgement 'Fire and forget' happening, its the consumer which will stamp the message and RMQ can decide further based on the negative acknowledgements;

Publisher Confirms:
basic.ack :  positive acknowledgements
basic.nack : negative acknowledgements, with multiple message option
basic.reject :  negative acknowledgements,

Publisher confirms:

  rabbitTemplate.setConfirmCallback(rabbitEventConfirmCallback);        

   public class RabbiteventConfirmCallback implements RabbitTemplate.ConfirmCallback {

    @Override

    public void confirm(CorrelationData correlationData, boolean ack, String cause) {

    if(ack){

    }

else{

//do something. Requeue/ ignore...

}

Friday, February 4, 2022

Docker cookbook

  docker --version

  docker pull <image_name>

  docker images

 docker run -it -d <image_name>

 docker ps

 docker ps -a

 docker exec -it docker exec -it <image_name> vin bash

 docker exec -it <image_name> /bin/bash

 docker  cp <image_name>:path/to/file path/to/ur/my_file

 docker logs <image_name> > your_location/some_file.log

 docker build -t <image_name> .

 docker run --name=<image_name> -it -p 8080:8080 -h SOME_info -e SOME_PARAMETER=ABC  /bin/sh

docker login

docker login -u <user> -p <pwd> artifactory_server_host

docker logs

docker logs -f [container_name]


-v : creating storage space separate from container

 docker run  -v /var/lib/mysql 

printenv

 

Docker Compose

  • Can manage multiple containers running on a virtual network.
  • These containers are able to communicate with each other because they share the same network, which is created and managed by Docker Compose. 
  • Containers within the same network can resolve each other's names, allowing them to interact seamlessly.



Thursday, February 3, 2022

 Runnable JDK 1.0 

new Thread( () -> System.out.println("Runnable") ).start()

Callbale Java 5

Callable callable = Executors.callable(Runnable task);

Callable returns Future object

call method can throw checked exception 

Future result = exec.submit(aCallable);

Response = result.get();

Callable<String> callable = () -> {

    // Perform some computation

    Thread.sleep(2000);

    return "Return some result";

};

Remember, Future.get() is a blocking method and blocks until execution is finished,

so you should always call this method with a timeout to avoid deadlock or livelock in your application

 while(!future.isDone()) {

            System.out.println("Task is still not done...");

            Thread.sleep(200);

            double elapsedTimeInSec = (System.nanoTime() - startTime)/1000000000.0;

            if(elapsedTimeInSec > 1) {

                future.cancel(true);

            }

        }        

future.get(100,TimeUnit.MILLISECONDS) :

 Method waits for the result of the task atmost 100 milliseconds, If the wait time out, then get throws TimeoutException.

(  future : future_results){

response= future.get();

}

Keep in mind future.get() will get blocked if its not completed. if future  get is in loop , it will not proceed for the next future get call.

If application receives multiple http request thread , it is prone to get all app thread blocked resulting application unresponsive.