Making sequential requests using Volley

Volley by nature is async as on completion of request callback gets triggered. We add request by using RequestQueue.add(Request) which gets added in internal priority blocking queue. By default 4 dispatcher or network threads run in background which pick request from queue and process it. As soon as processing is complete dispatcher picks next request in from the queue.

To make volley request sequential we need to restrict dispatcher threads to single thread instated of four. By doing this we restrict Volley from executing next request before complication of current request.

We can use RequestQueue constructor

Sorting string array using SetTree

Code is straight forward

 

Accessing H2 in memory database

Accessing H2 in memory database integrated in spring application

Add below 2 beans in spring config. H2 console will be accessible on http://localhost:8082/

<bean id=“h2Server” class=“org.h2.tools.Server” factory-method=“createTcpServer” init-method=“start” destroy-method=“stop” depends-on=“h2WebServer”>
<constructor-arg value=“-tcp,-tcpAllowOthers,-tcpPort,9092”/>
</bean>
<bean id=“h2WebServer” class=“org.h2.tools.Server” factory-method=“createWebServer” init-method=“start” destroy-method=“stop”>
<constructor-arg value=“-web,-webAllowOthers,-webPort,8082”/>
</bean>

String with variable parameters

Using string with parameters is very handy in case of Constants.

Java class in focus is: java.text.MessageFormat

 

Output will be

Welcome Mr. Nikhil to njoshi.com
Welcome Mrs. Tejashree to njoshi.com

Installing VirtualBox guest additions on CentOS or RHEL

Process is very simple

  1. Login as root
  2. Install dependencies # yum install gcc make kernel-headers kernel-devel
  3. Reboot for loading new kernel info if required and to be on safer side
  4. Use gmake instated of make #export MAKE=’/usr/bin/gmake -i’
  5. Insert guest additions CD
  6. goto installer # cd /media/VBOXADDITIONS*
  7. #./VBoxLinuxAdditions.run
  8. Reboot and enjoy

Creating multiple jars from single pom file

In ant we can create any number of jars using <jar> tag, but when we are moving to maven from ant it we may face challenge. Old code may not be modular imposing use of single pom file. In this case maven-jar-plugin come to rescue.

maven-jar-plugin

I guess xml is self explanatory, if question put it in comments

Full working source code

Adding internationalization support to spring mvc with thymeleaf

For basic Thymeleaf configuration check Spring MVC with Thymeleaf

Thymeleaf uses Standerd messange resolver unless specified which on to use. We can also use Spring internationalization support which will be supported by default by Thymeleaf.

Steps:

  1. Add messages_en file to src/main/resources/
  2. Add spring beans those will support internationalization.
  3. LocaleChangeInterceptor is handler mapping that need to supports the multiple languages. The “paramName” is the parameter value that’s used to set the locale.
  4. Use ?lang=fr to change language to French from default English.

Source: Source Code

Java Concurrency – Processes and Threads

Process

Process is self contained environment meaning it got its own basic resources importantly own memory space. For communication between processes we need interprocess communication (sockets, pipes etc) provide by operating system.

Most implementations of JMV runs as single process. Java application can create multiple or additional processes by using java.lang.ProcessBuilder object.

Threads

Threads exists within process and every process has at least one thread. Thread share process’s resources that includes memory, open files etc; hence creating new thread requires few resources. Threads sometime called as lightweight process. Each thread is associated with instance of class Thread.

Their are two strategies of  Thread
1. Manual – Control thread creation and management by instantiating Thread each time application want to perform asynchronous task
2. Executors – Separate thread management from application by passing tasks to Executor.

Defining and starting a thread

1. Provide Runnable object – Implement Runnable interface which defines a single method run()
2. Subclass Thread class – Extends Thread class.

It will be wise to implement Runnable interface instated of extending Thread class. Coz we can extend other classes if required if we implement Runnable interface but we can not if we extend Thread class.

Thread.sleep(long miliSeconds) causes current thread to suspend execution for specific time. Sleep timings are not guarantied to be precise as it depends on facilities provided by underlaying operating system.

Good example of basic thread working

Ref: Java SE tutorials