Saturday, 29 May 2021

Anonymous Class

As the name implies, an anonymous inner class isn’t defined using an explicit name.

An anonymous inner class is created when you combine instance creation with inheriting a class or implementing an interface. Anonymous classes come in handy when you wish to override methods for only a particular instance. They save you from defining new classes.

The anonymous class might override none, few, or all methods of the inherited class. It must implement all methods of an implemented interface. The newly created object can be assigned to any type of variable—static variable, instance variable, local variable, method parameter, or returned from a method. Let’s start with an example of an anonymous inner class that extends a class.



Tuesday, 18 May 2021

Thread Pool

Thread pool is a solution to limit the number of threads in usage, and so as to avoid the lower performance and system crash due to resource exhaustion.  

A thread pool is a group of pre-instantiated, idle threads that stand ready to be given work. Using a thread pool gives a better performance than creating a new thread for each task. It uses existing thread first, only having no existing available, then creating a new thread.

ExecutorService extends Executor

ScheduleExecutorService extends ExecutorService

ExecutorService manages to submit tasks much better than Executor. It accepts both Runnable and Callable.

ScheduleExecutorService manages to submit tasks and run in planned time slices.


ExecutorService offer APIs 

'IsTerminated()': testing if all submitted tasks having been accomplished.

'IsShutDown()': testing if the executor is shut down. 

'shutDownNow' means that attempting to halt active running threads, and stop picking up tasks from the queue, and returning un-implemented tasks in a collection. 'shutDownNow' is more like shutting down at once.


'shutDown' means executing previously accepted tasks, but no more task accepted; if you do add one after it; it won't give a compiling error, but a run-time exception, i.e. java.util.concurrent.RejectedExecutionException

Through ExecutorSerive, the client may submit Runnable or callable to the thread pool.

Executor consists of a queue and a pool.

Pool types: fixed-sized, single thread, scheduled thread pool, cached thread pool

The 'Executors' is a factory, where a client achieve a specific type of thread pool.

       ExecutorService es = Executors.newCachedThreadPool();

        es.execute(new PrintChar(10, 'a'));
        es.execute(new PrintNum(10, 66));
        es.execute(new PrintNum(10, 20));
        es.shutdown();

Sunday, 2 May 2021

Microservice Start up Lifecycle

When a microservice starts up, it goes through several steps in its lifecycles, i.e. Assembly, BootStrapping, Discovery, and Monitoring.  

Service assembly: 

packing all the source code and all its dependencies, together with its runtime engine in a single installable artefact. 

It can eliminate the traditional Configuration drift problem, which is due to server configuration and source are managed by different admin team and development team respectively.  

It can quickly deploy the micro-service in response to a sudden influx of requests.  

Service bootstrapping: 

The key point for service bootstrapping is how to manage its configuration file. 

Distributed microservice instances often run into the same type of configuration requirements.  With a high number, and geographically dispersed services, becomes unfeasible to redeploy services to pick up new configuration data. Storing configuration externally solves this problem. 

The configuration store should close to the service, and with minimal latency.  The most important configuration store should always up running, otherwise, it would become a single point of failure. 

When a microservice instance comes up, it calls an ending point to read its configuration, which resides in a repository that may be a relation database, files under source control, or a key-value data store.

Changes to configuration management are typically handled through the build and deployment pipeline where changes of the configuration can be tagged with version information and deployed through the different environments.

When making a configuration change, services consuming it must be notified and refresh their copies. 

Service discovery

From a client perspective, microservices should be IP transparent; all service instances behind one microservice are considered as the same physical resource. Microservice normally maintain a short life cycle, and it has a unique but non-permanent IP address assigned to it. In this way, it achieves a high degree of scalability and availability. a microservice needs to register itself with the third-party agent, thus exposing itself by a logical name, rather than a permanent IP. 

Service monitor

Most service instances will expose a health check URL that will be called by the service discovery agent. If the call returns an HTTP error or doesn't respond in a timely manner, the service discovery agent can shut down the instance or just not route traffic to it. 

Service discovery agent monitors the health of a service instance. If the instance fails, the agent will remove it from a pool of available instances. 

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...