Posts

Spring cloud config

target My main target here is to show how spring cloud config work with a real example what is spring cloud config As you know, spring cloud has many projects (modules) and spring cloud config is one of these. His target is to help the programmer to build a server that work as configuration server and also to let the clients to get the configuration from the server. No one force you to use spring cloud config to build this kind of stuff, but if you use it, you will save a lot of time, because, as we are going to see, with spring cloud is very simple build what i said. why should i use a config server the problem is related expecially with microservices that you build with spring boot. As you know, when you build a microservice with spring boot, it has its configuration file and often you need to modifity it. But a distribuited application can have a lot of microservices, so it can require a lot of time when you have to configure each microservice. The solution of this pro...

Quick Sort algorithm with Java and R

Image
Algorithm description Quick description Quick sort is an algorithm to sort elements (we will use a vector) In short, the procedure for sorting is done in this way: i take a vector's element (the pivot) (for example the first element of the vector) and then i move the pivot and all the elements, with value less than the value of pivot, at left of the pivot and all the elements, with value more hight, to the right of the pivot. In this way i have the pivot in a position where at its left there are all the element of less value and at right, all the element with value more hight. So the pivot is an element just sorted in the vector. It's just in the right position. Now i can see the array as made by three elements. The pivot, the left sub vector and the right sub vector. So i can apply the same algorithm to the left sub vector and the right sub vector. This process is iterated in each sub vector until all the elements are sorted As you can see this algorithm use divide...

SAX xml parser and Java

Image
Summary In this article i want to show - an introduction to sax - a simple use case that dimostrate the basic functionality of SAX - how to validate an xml document with SAX - how to use the filters Introduction to SAX SAX (Simple API for XML) is an xml parser. It started as java api but then it becames a standard. Since SAX is a standard, there are a several implementations (in java and other languages). Java also provides a unified interface to the parsers (so also to SAX) through JAXP (java api for xml processing). Basically JAXP uses the factory pattern to access a parser like SAX and then for parse the xml document. So to parse an xml document we need an XMLReader and we can have this by retrieve the sax factory and then a sax parser object. Something like this SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.parse("/path/to/people.xml")...

JAXB: binding java with xml

JAXB allow binding between java objects and xml instance. Basically with JAXB you can do 'marshall' and 'unmarshall' operations. Marshall means the process of conversion from java objects to xml instance. With unmarshall means the opposite. Marshall example To start with marshalling we need at least our domain class. Suppose we have a city with many person and i want to have an xml file with a root element city wich contains more element of kind people. To do that we need one object of type city wich contains a collection of person objects City.java public class City { private List&ltPerson&gt list = new ArrayList&ltPerson&gt(); // get and set } Person.java public class Person { private String name; private String surname; private int age; // get and set } Now, since we want to use JAXB we need to annotate these classes so JAXB can bind them to the xml file. Basically th...

Architectural pattern: publisher-subscriber

Image
Introduction This pattern is also know as ’Observer’ (GoF). The mainly difference is that in architectural patterns there is a distinction for what is a components and what is a connectors. Here i want show just a basic working without this distinction. Besides i want show how implement it using the API of java, utility class that simplify the java implementation. What is the problem I have one or more objects (observer or listner or subscriber) that are interested to know when there are some events from another object (subject or publisher) and they want be informed when it happen. When that event happen the subscriber can perform their action that can be different from subscriber to subscriber. How the publisher-subscriber work The first thing to do for the subscribers (or observers) is registering to the publisher (or subject) so they can be notified when a event happen. To do that they just use attach() method of ConcreteSubject. When there is an event (that maybe als...

Concurrency pattern: Producer-consumer

Image
What is the problem Basically this pattern solve the problem of a buffer, in which one or more Producer can push a data and one or more Consumer can pull a data. There are a couple of problems that can arise: the first is catch to ’race condition’, indeed since to the buffer can access more productors and consumers at the same time, can arise problems of inconsistency data. The second problem is catch to ’deadlock’, mean a lock of system. How this pattern work In this pattern, the problem of ’race condition’ arise, for example, if we allow to do, at the same time, push and pull operation. In this case, since both methos changes the internal state of the buffer object, they can read not right data. Generally to avoid ’race condition’ problems on an obbject it’s enough use the ’single threaded execution’ pattern. Single threaded execution Basically, in our case, the ’single threaded execution’ allows to the methos of object of type Buffer, signed as ’guarded’, may be executed b...

Design pattern visitor with java reflection

Image
What is the problem I have a collection of object of different type and i want add some behaviour without modify existing classes. More specific i want add different behaviour based on object's type. In our case i have objects of type Ferrari and Maserati and i want modify the property colour of this objects and print some message when i do that (this is the new behaviour). Due to this i want object of Ferrari in red and object of Maserati in blue. Class diagram Why use visitor Without the visitor i could create objects of type ConcreteElement and then modify this classes and recompile, but it it a bad idea. Or i could use if statement, something like: if object is of type Ferrari then do that, if is of type Maserati do that. But every time i add new type of object i need to modify that code. The visitor avoid the modification of that classes and allow to add different behavioural on each type of object. How visitor work Basically it work so: the client loop throug...