Saturday, 3 September 2016

Building AKKA actor system with Spring on Jetty


The best way to know AKKA or the actor system is to know the Reactive Pattern.


Reactive Design Patterns is a clearly-written guide for building message-driven distributed systems that are resilient, responsive, and elastic. In it, you'll find patterns for messaging, flow control, resource management, and concurrency, along with practical issues like test-friendly designs.


And that’s what Akka is : “Akka is a toolkit and runtime for building highly concurrent, distributed, resilient, message-driven applications on the JVM” (https://www.lightbend.com/community/core-projects/akka)


How does Akka do this ‘reactiveness’ ?

Akka enables this with its reknowed model called ‘Actor Model’. In the Actor Model, an application is broken up into small units of execution called actors. These actors encapsulate the behavior and state of a part of the application. The first part of the definition of actors makes them sound very much like objects in a standard object-oriented design, which is true. However, what distinguishes an actor from a standard object is a third property: communication.

Actors never interact directly with each other and instead communicate via messages. The behavior of an actor is primarily defined by how it handles messages as they are received. Since the state of an actor is isolated from the rest of the system, an actor never has to worry about synchronization issues, such as thread locking, when modifying its state.
(more : https://dzone.com/refcardz/reactive-programming-akka)


How can we build an actor system? 


It’s very easy to create an actor system with a single line of code, 

final ActorSystem system = ActorSystem.create("MySystem"); 
 
But creating an ActorSystem is very expensive, so you want to avoid creating a new one each time you need it. Also your actors should run in the same ActorSystem, unless there is a good reason for them not to. The name of the ActorSystem is also part the the path to the actors that run in it. E.g. if you create an actor in a system named ‘MySystem it will have a path like ‘akka://MySystem/user’. User’s are the actors which you create.You can create an actor with the following line of code.
final ActorRef akkaBot = system.actorOf(Props.create(AkkaBot.class), "akkaBot");

And then ref it as akka://MySystem/user/akkaBot

(more : https://dzone.com/refcardz/reactive-programming-akka)

So How do we solve the proble of creating an AKKA actor system?

The best way which I found is using Spring framework on Jetty Server (As it is verymuch easy to use spring boot classes with Jetty).

Initialize the spring context ( I used WebApplicationInitializer you can also use the dispatcher xml)


Initialize Akka Actor system as a bean using annotation.




That’s it!



Tuesday, 23 August 2016

Setup Apache reverse proxy for multiple nodeJs server applications.


The major problem in hosting in in-house environment NodeJs application servers are the configuration of ports. The possible solution to host multiple NodeJs applications on the same system is reverse proxies. One of the major reverse proxy used for NodeJs hosting set-up is Nginx.

Apache also can be used as a reverse proxy for NodeJs applications.

After Apache installation, the site configuration file need to be modified to enable the reverse proxy.

The apache site configuration file 000-default.conf can be found in /etc/apache2/sites-enabled directory.

The first set-up is to add the necessary module file to enable proxy.


After the module setup the virtual host can be set. It would be preferable to use multiple subdomain for multiple NodeJs application for better proxy settings.

The following virtual host set-up enables all the 3 types of transports used by socket.io (http, xhr-polling, ws)



Now, restart your apache using /etc/init.d/apache2 restart

Run Dart Http web server as a service using Docker


Dart is a general-purpose programming language originally developed by Google and later approved as a standard by Ecma (ECMA-408). It is used to build web, server and mobile applications, and for Internet of Things (IoT) devices.

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. It packages your application into a standardized unit for software development. Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server.

How to make dart http server as a service?

Step 1: Install Dart (https://www.dartlang.org/install)
Step 3: Write your first Dart Http web server (server.dart)
 Step 4: Write Docker file (Dockerfile)
Step 5: Build docker container
 

Step 6: Run docker container as a daemon
 
Note: Check whether the process is running using "sudo docker ps"

Open your browser and type the url "http://localhost:4040"



Monday, 22 August 2016

Write Future’s Javascript NOW!


What is Javascript?

JavaScript (JS) is a lightweight, interpreted, programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as node.js and Apache CouchDB. JS is a prototype-based, multi-paradigm, dynamic scripting language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

Some of the available Javascript Implementations:

The first ever JavaScript was created by Brendan Eich at Netscape, and has since been updated to conform to ECMA-262 Edition 5 and later versions. This engine, code named SpiderMonkey, is implemented in C/C++. The Rhino engine, created primarily by Norris Boyd (also at Netscape) is a JavaScript implementation written in Java. Like SpiderMonkey, Rhino is ECMA-262 Edition 5 compliant.
Several major runtime optimizations such as TraceMonkey (Firefox 3.5), JägerMonkey (Firefox 4) and IonMonkey were added to the SpiderMonkey JavaScript engine over time. Work is always ongoing to improve JavaScript execution performance.
Besides the above implementations, there are other popular JavaScript engines such as:-
  • Google's V8, which is used in the Google Chrome browser and recent versions of Opera browser. This is also the engine used by Node.js.
  • The JavaScriptCore (SquirrelFish/Nitro) used in some WebKit browsers such as Apple Safari.
  • Carakan in old versions of Opera.
  • The Chakra engine used in Internet Explorer (although the language it implements is formally called "JScript" in order to avoid trademark issues).

Can we use it is NOW?

The final ECMAScript 6 standard (ECMA-262) was published in June 2015 after resolving some its nasty aspects. As programming never is just about getting the necessary functionality done, it is advised to also use the best language, too. ECMAScript 6's language design is cleaner than ECMAScript 5, its syntax increases the expressiveness of your code, it decreases the necessary boilerplate code (e.g. function vs. arrow syntax) and it especially let you get rid of some very nasty but required hacks and workarounds from the ECMAScript 5 era (e.g. var self = this).
But the major issue in using ECMA Script 6 is the lack of support by major browsers. 

How to use Futures Javascript NOW?

The solution to use the future standards of Javascript in your current browsers is Transpilers, or source-to-source compilers.
The most popular transpilers are;
1) TypeScript (https://www.typescriptlang.org/)
2) Babel (https://babeljs.io/)
3) Traceur (https://github.com/google/traceur-compiler)
Also Type chekers like flow (https://flowtype.org/) is adding value to your future’s code

Sunday, 21 August 2016

5 Libraries that makes your Java coding easy





1) Jsoup

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.
jsoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do.

  • scrape and parse HTML from a URL, file, or string
  • find and extract data, using DOM traversal or CSS selectors
  • manipulate the HTML elements, attributes, and text
  • clean user-submitted content against a safe white-list, to prevent attacks
  • output tidy HTML
jsoup is designed to deal with all varieties of HTML found in the wild; from pristine and validating, to invalid tag-soup; jsoup will create a sensible parse tree.





2) Xembler


Xembly is an Assembly-like imperative programming language for data manipulation in XML documents. It is a much simplier alternative to XSLT and XQuery.


Since version 0.9 you can directly transform directives to XML:

String xml = new Xembler(
  new Directives()
    .add("root")
    .add("order")
    .attr("id", "553")
    .set("$140.00")
).xml();
 
This code will produce this XML document:

<root>
  <order id="553">$140</order>
</root>
 
URL: http://www.xembly.org/
 
3) Gson


Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes; something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals. 

URL: https://github.com/google/gson



4) Gagawa

Gagawa is an HTML Generator library written in Java and PHP.
It allows developers to easily and dynamically build well-formed HTML in web or non-web applications. In Java, Gagawa is especially useful when traditional HTML generation engines such as Java Server Pages (JSP's) are unavailable. In PHP, Gagawa frees developers from hand-writing HTML in PHP web applications. Although you can build an entire site using Gagawa, most developers use Gagawa in conjunction with other libraries to build relatively small blocks or pieces of HTML. For example, Gagawa is perfect when a developer needs to return a small chunk of well-formed HTML in an AJAX response.

URL: https://code.google.com/archive/p/gagawa/



5) ICEpdf


ICEpdf is an open source PDF engine for viewing, printing, and annotating PDF documents. The ICEpdf API is 100% Java, lightweight, fast, efficient, and very easy to use.
ICEpdf can be used as standalone open source Java PDF viewer, or can be easily embedded in any Java application to seamlessly load or capture PDF documents. Beyond PDF document rendering, ICEpdf is extremely versatile, and can be used in a multitude of innovative ways, including:

  • PDF to image conversion
  • PDF annotation (see below)
  • PDF Interactive Forms
  • PDF text and/or image extraction
  • PDF search
  • PDF printing
URL: http://www.icesoft.org/java/projects/ICEpdf/overview.jsf

File Delta / Versioning in Java


Delta encoding is a way of storing or transmitting data in the form of differences (deltas) between sequential data rather than complete files; more generally this is known as data differencing.
The differences are recorded in discrete files called "deltas" or "diffs". In situations where differences are small – for example, the change of a few words in a large document or the change of a few records in a large table – delta encoding greatly reduces data redundancy. Collections of unique deltas are substantially more space-efficient than their non-encoded equivalents.
VCDiff and Gdiff are the common format used for data encoding.
Xdelta is a library that helps to compare differences and make patch files. It also has lot more features like compare folders, apply patches etc..
The Java libary for creating and patching delta is Javaxdelta (http://javaxdelta.sourceforge.net/)
Javaxdelta library provides classes for generating binary (and also text diffs) in GDIFF format and patching binary and text streams.
Find it on maven repo : 

<dependency>

     <groupId>com.nothome</groupId>

     <artifactId>javaxdelta</artifactId>

     <version>2.0.0</version>

</dependency>

Also you can find a much useful wrapper for javaxdelta here : https://github.com/alexkasko/delta-updater
The other library with can be used is Badiff (http://badiff.org/wiki/Badiff)
Badiff is a pure-java byte-level diffing library for working with large inputs, taking advantage of parallel processing during optimization. badiff is pretty fast. badiff has no external dependencies, but requires Java 1.6 or higher. badiff is released under a BSD license.
Find it on maven repo :


<dependency>

     <groupId>org.badiff</groupId>

     <artifactId>badiff</artifactId>

     <version>1.2.0</version>

</dependency>