Skip to main content

Posts

Showing posts from July, 2017

Resource Pooling

Requirements for Resource Pool: Allow threads to access resources concurrently Should block threads if no resource is available at the moment Maintain a pool of initialized resources to allow reuse and avoid costly initialization of resources Classes from java.util.concurrent package that will be used: ConcurrentLinkedQueue - An unbounded thread-safe queue. This collection is ideal to act as container of resources since it provides concurrent non blocking access to its elements. Semaphore - Semaphore maintains a set of permits by having a counter. This can be used to acquire or release a permit. If no permits are available then acquire() blocks the invoking thread. Implementation Code reference on github: Resource Pooling

DIY Testing Portal

Introduction A simple portal for tracking testing reports, bugs found and then mapping all this way back to features and releases. However JIRA and other such project management tools provide the features that are mentioned in this document but still this solution can come in handy for those who don't want an outside tool rather a quick dirty solution. Note this is for a general solution overview and focuses more on data modelling. Any installation and setup related findings is homework for reader :) What all is compassed in testing ? Testing Report Bugs Releases Features Test cases Environment What all do you need to see in a testing dashboard ? Timeline of all testing that has happened till date Individual testing report Test cases, which cases map to which feature(s) Testing cycles for a specific release Bugs introduced in a release, mapped to which feature(s) The portal solution involves : Mongodb Mongodb Compass - mongodb dashboard tool Elas

Enabling CXF goodies in Spring Boot

In this post we are going to add some of the CXF features to our existing app that we developed in  previous post . These features are : ID Logging Jackson Provider for POJO to JSON conversion Swagger 2 documentation Step 1: Configuration class Create a RestServer class in config package as shown below package org . blog . config ; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider ; import org.apache.cxf.feature.LoggingFeature ; import org.apache.cxf.jaxrs.swagger.Swagger2Feature ; import org.springframework.context.annotation.Bean ; import org.springframework.context.annotation.Configuration ; /** * Created by Anand_Rajneesh on 3/23/2017. */ @Configuration public class RestServer { @Bean public JacksonJsonProvider jsonProvider (){ return new JacksonJsonProvider (); } @Bean public LoggingFeature loggingFeature (){ return new LoggingFeature (); } @Bean public Swagger2Featur

Guide : Spring Boot with Apache CXF for REST services

In this series of guide, we are going to explore writing REST services with Apache CXF using Spring Boot. The project is build using maven. I assume that you already know how to use maven. Step 1 : Adding dependencies for Spring Boot By default you have to inherit the parent pom of spring boot, but that cannot be followed everytime, so I use an alternative to that. I basically add spring boot pom as dependency so that it brings all the dependencies. <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring.version>1.4.3.RELEASE</spring.version> <cxf.version>3.1.10</cxf.version> </properties> <dependencies> <dependency> <!-- Alternative to inheriting from parent spring pom --> <groupId>org.springframework.boot&l

OOPs simply explained

What is OOPs ? It is Object Oriented Programming. It comprises of Polymorphism Encapsulation Inheritance Abstraction Already lost me ? Now here it is read about OOPs as if you deal with it every day. Let's take at OOPs from a Corporate world point of view. There is an organization which comprises of several employees which form a hierarchy. Now every employee has their way of working, some work hard, some like to delegate and some are smart. Though everyone does work in office, they do it in their own way. This is Polymorphism (implementing a function differently while keeping the semantics of function same). Employees are after all humans, we all have secrets. We have our own perspective which we may or may not share with everyone i.e. we have state and behavior only accessible to us. We share some of our secrets with our closest friends, some of our perspective to a group of people depending upon our status. In the end we do keep everything close and private, nothing

PL SQL Cheatsheet

PL/SQL Syntax for reference Basic PL/SQL block DECLARE --variable declarations go here BEGIN --program logic goes here END; Functions -  Named pl/sql blocks mainly used for computation and not for write operations. They return value to the caller as defined in function. CREATE OR REPLACE FUNCTION myFunc( arg1_in IN VARCHAR2, arg2_in VARCHAR2) RETURN BOOLEAN AS RETURN true; END; Procedure - Named pl/sql block used for write operations. CREATE OR REPLACE PROCEDURE myProc( arg1_in IN VARCHAR2, arg2_in IN VARCHAR2) AS arg3 VARCHAR2(2048); arg4 BOOLEAN; BEGIN --logic END; Variable Declarations  arg VARCHAR2(2048) := 'myString'; arg BOOLEAN := true; arg NUMBER := 10; IF ELSE Conditions IF (condition) THEN --logic 1 ELSIF (condition2) THEN --logic 2 ELSE --logic3 END IF; SWITCH Statements With selector arg1 NUMBER := 0; CASE arg1 WHEN 0 THEN --lo

Power of declarative syntax of Lambda

Since Java has introduced lambda, writing code has become a breeze especially when it comes to expressing what you want to computer to do rather than how to do it. The following is a question from Project Euler and is solved using lambdas in Java8. Let d( n ) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n ). If d( a ) = b and d( b ) = a , where a ≠ b , then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. Evaluate the sum of all the amicable numbers under 10000.  A simple brute force solution is to check every number from 2 to 10000 and if its  an amicable number then add it to sum. Listing it in steps you are basically doing 3 things: Take numbers from 2 to 10000 If number is amicable Add it In Java 7 one will

Volatile and Synchronized in Java

Volatile is just another modifier like private , protected etc. It modifies the visibility of the variables and is generally more relevant in thread context. Volatile when used on a variable ensures that every thread that is using it has the latest copy. It is important in multi threaded programming that shared variables which are frequently read and write by the threads are marked as volatile. JVM is free to do anything with the variables and every thread can, actually does keep its own copy of variable meaning if a thread writes a new value to a variable, the other thread won't get that latest value. private int x  = 5; Thread 1 uses it and makes it 8, but thread 2 will have its own copy as 5 and won't see new value 8. This can of course leads to a lot of problems and I don't think there is any need to explain that. Primary three reasons to use volatile when needed: 1. Every thread keeps it own copy of a variable, so if you want a shared variable with publish s

CAP & ACID

CAP and ACID are two distinct terms that are used often in database world, where ACID is used to define properties of a database and CAP is actually used to determine distributed systems nature. ACID stands for Atomicity Consistency Isolation Durability Before going into the meaning of these terms one should be fairly aware of transactions. A transaction is like a unit of work which may comprise of multiple write operations or just a single operation. Treating a set of operations like this actually enables databases to maintain ACID properties. Atomicity -  Either all statements/queries inside a transaction happen or none of it does. In layman terms, if you start a transaction saying I want these 5 queries to be run. The database will make sure that either all of 5 queries are successful or none of them is. So if 5th query fails automatically the previous 4 queries effect will be wiped off. Consistency - This ensures that whatever data you put into your database does not v

Procedural Programming with one while and switch cases

A well known folk theorem in computer science is that any program can be transformed into a semantically equivalent program of one procedure containing one switch statement inside a while loop. The transformation involves rewriting the program so that each case of the switch is an elementary operation of the original program followed by an assignment to a program counter variable that communicates to the switch statement the next operation to execute. The while loop terminates when the program counter reaches the last operation in the program. Well in production code nobody will opt for this style, but I think there might be a case where this could come in handy. Few times there is a use case where we have to generate some unique id based on the customer attributes.  This could used as slug for pretty urls, pseudo-username etc. Recently I had to write a stored procedure (PL/SQL) in Oracle to generate pseudo-usernames for some million email accounts that we had, with usual rules of le

Maximum Path Sum

Problem Statement : Starting from top to bottom and moving to adjacent numbers below, find the maximum total for triangle. By looking it at the given triangle for few minutes you would see that below is the path having maximum sum i.e. 23. First approach that may pop in one's mind to solve this problem would be a brute force algorithm. This would be trying every route to find the max sum. The actual number of iterations that will take place for this approach would be 2 n , which would be a problem if size of triangle increases. Bottom to Top Approach This approach is looking the triangle from bottom to top. First visualize the triangle as follows: We transform the triangle in sort of tree structure and then only 2 steps need to be repeated until we reach the root node. Step 1: Choose the max leaf node from the two child leaf nodes. Step 2: Add the chosen leaf node to the parent node. Repeat this until you get to root node. For this triangle this is how it goes,