Billy Yarosh Blog

Java & Ruby Development: Coding Cures, Concepts, API examples and more


Rails Unfit for Microservices?

This is a relatively informal blog post. Looking more for feedback and response to get a conversation started. The prerequisite to this thread is that you have a fairly good understanding of Microservices. If not at least read the following link:
http://martinfowler.com/articles/microservices.html

About 2 years ago I dropped from Java development and moved on to Ruby and Rails. This was a business decision, and personally, I was excited to challenge myself and help grow as a developer. My blog has been fairly inactive since then.

There are two reasons for my inactivity:

  1. Learning a new language requires much time reading, implementing experimenting, and interacting with the community to understand and build great software.
  2. The Ruby community is already awesome.
Learning a new language is challenging and time consuming.
I have to admit that Ruby was a fairly big change from Java. With Groovy experience, it wasn't a far stretch but under the covers, it is very different. I still can't get over non declared types on initialization, but a variable can't take on a new type there forward (dynamic yet strong typed). This was bizarre to me. I also missed interfaces, but at the same time loved modules. I have to say that Ruby is the most syntactically beautiful language used in enterprise technology. The style in which you write code in Ruby is completely driven by the community, and what they deem as acceptable in their open source software. This leads me to my next point..

The Ruby community is awesome.
There is no other open source community out there as interactive and cohesive as the Ruby community. I had no reason to blog because, so many great writers and community projects had what I needed, or enforced standards through community driven development. This community is amazing and incredibly adaptable. 

Rails is pretty cool too.
Rails is by far my favorite full stack web framework. It has great docs, tooling, and community support through gems and Rails engines. If a custom write would cost my company X weeks or months to solve, there would almost always be a gem I could use. Rails is lighter then most all full stack Java web frameworks, and all lack the same community support.

The problem with Ruby: It's heavy dependency on Rails.
At the time of this writing, there were near 24000 stars on github for the Rails project. There are also countless gems that support Rails only. If Rails drops, that is a bunch of wasted time and code (which there should be a real need for some time). Obviously we know that Rails is heavily opinionated on how we work within it's framework and also lacks modularity. It's all or nothing. This can be a problem long term, if the architecture of software changes. That brings me to micro services.

A trend towards microservices
Micorservices Architecture is a loosely defined architecture based around the early SOA principles. A web application is a small service that preforms very few or a single task. A call from a client, may be an orchestration of services on the back-end. A service may just receive and persist data. It could proxy requests, mediate between services and clients, translate messages, queue to/from a topic exchange, deliver messages via various transport protocols and more.

Not everyone needs micro services architecture (MSA)
Martin Fowler has articulated in his article the requirements before thinking MSA. I would suggest reading it before continuing. Jeppe Cramon, also has a pessimistic, but realistic look at MSA architecture in his article on the TigerTeam blog. If you still feel MSA fits your needs, and have done the research, you will find Rails has a hard time adapting to this architecture.

Does Rails fail in the Microservice realm?
In MSA, services are not full stack. When you tear down a service to business logic, translation and validations, does it really need Rails? Probably not. At the same time there are parts of Rails that would make our lives easier. As a developer, we don't want to re-implement active record validation (which is modular with active model validations), or the routes API, MVC components and active record of course. The thing is that Rails is convenient in full stack development,  but lacks modularity if needing to break it apart for MSA. It's unfortunate, but a real issue. The same holds for Groovy and Grails. They are already taking an initiative to modularize their stack to prepare for MSA. This is a good step, that I feel the Ruby community should take note of.

Will Ruby fall behind if MSA is adopted?
Absolutely not. Why? Because the community is awesome. There is heavy dependence on Rails in the Ruby community, but there are also libraries that will suite perfectly in MSA. For example, CelluloidRack Server Interface (Rails is built on), and parts of Sidekiq are capable tenants for MSA. Multi-threading in ruby should be sufficient enough, since green threads are smart enough to handle Non-blocking IO. Where this may fall is if you need to thread business logic, that is not dependent on external services. This cannot be truly threaded, but only efficiently threaded (minus JRuby). 

The community will prevail
Luckily for rubiests, they drive the future of Ruby and will ultimately decide if MSA needs to be adopted. My fear as always, is that Ruby could pigeonhole itself into Rails, and not break from the standard. Why? Because it's good enough most of the time. If that is the case, and you want to build an MSA system, then think hardly about how it's designed. 

MSA and Rails
Services would likely be full stack but manage a small subset of the business, and it's models. Those could also be broken down and separated based on various data aggregates. A uniform REST API, will be crucial as well. Where would this simple implementation fall short? The ability to scale multiple services on the same server would be lost. Services would likely talk directly, and have to know each others APIs which would inherently force coupling between services. These are key issues discussed in MSA, and in the Rails world would need the right tooling to resolve. The other option is to claim you do MSA, but really be doing the stuff that makes sense. This approach may be enough for your needs. We can liken it to the hypermedia (HATEOAS) part of the REST spec. Most claim REST support but never adopt hypermedia in their API. This is because the community has decided that REST is good enough without it (most of the time). The same may bode for MSA in the future. That is an unknown (what is good enough).

I'm going back to Java (Mostly) for MSA
Java is already ahead of the curve in MSA with things like spring boot, spring HATEOAS, reactor, and various Netflix contributing libraries, along with Zookeeper for service discovery/registration. There will be support for Rails if a team would like, but they will have to build the tooling to integrate within the system. Part of that is giving up much of the free tools that Rails offers. Our Identity Provider is a Rails application. Since it does not integrate nicely with our MSA, we will have to build a service to mediate calls to it. This overhead is necessary but unwanted. Unfortunately Rails is opinionated and may not be best suited for MSA yet.

Thoughts and responses are welcome.



The Java Enum: A Singleton Pattern


The singleton pattern restricts the instantiation of a class to one object. In Java, to enforce this, the best approach is to use an enum. This great idea comes straight from the book Effective Java by Joshua Bloch. If you don't have it in your library, get it. One of the best Java books to date.


There are a few reasons why one would use an enum as a singleton in Java:
  1. Serialization for free.
  2. Guaranteed one instance (Cannot instantiate more then one enum even through reflection.)
  3. Thread safe

Bootstrap Guide: The Virtual Tour Library

Introducing a Virtual Tour Library (using TW Bootstrap)


 A jQuery library used for designing help pages for your website. Gives the user a guided walk through of your webpage and it's functions.




Rails Associations: Always think of the user

The topic of discussion in this post is Rails associations. Primarily, we will be focusing on which association you should choose in certain advanced scenarios. If you understand Rails associations but want better insight on deciding which associations to use, then this post is for you.

The Scenario

This example may not seem practical, but I feel it is a fun and simple way to convey our association strategies. The scenario is extremely important to our decision making when designing a data model. What data is required, and in what format is usually the first step.



Java Developers Need To Be Using Lombok


Lombok is a Java library meant to simplify the development of Java code writing. This article deals with writing classes that need getters/setters, override equals, hashCode, toString and/or offer a copy constructor. Way to go Java! You just made writing a simple class cumbersome. Project Lombok will make your day if you haven't seen it yet.

Adding Groovy Sources to Maven Build Path


Say you want to run a groovy class method from your Java project in Maven. You've placed the script file at src/main/groovy/script/MyGroovyScriptClass.groovy. In Java, you would load the class and execute its method like so:
final ClassLoader parent = this.getClass().getClassLoader();
final GroovyClassLoader loader = new GroovyClassLoader(parent);
URL url = this.getClass().getResource("/script/MyGroovyScriptClass.groovy");
try {
    final Class groovyClass = loader.parseClass(url.getFile());
    this.jdbcRouteCSV = (GroovyObject) groovyClass.newInstance();
    Object[] args = {};
    this.jdbcRouteCSV.invokeMethod("myGroovyScriptMethod", args);
}
catch (IllegalAccessException iaex){
    ...
}
catch (InstantiationException iex){
    ...
}

The problem is that this won't work in Maven, unless you add the groovy folder to your resource path.


Ignore Header and Trailer Lines During Java InputStream Read

Ignore first + last lines in a file

After beating my head against the wall for some time, I was finally able to come up with a solution for this.

Say you want to hand off an InputStream or InputStreamReader to an underlying API. The problem is that your input stream may have a header or trailer. Those header/trailers differ from the underlying format. This is commonly true for CSV, EDI, and fixed length file types. So if you want a CSV file parsed by a Java API like the Smooks api, you will need to hand the framework an InputStream.


Grails Part 5 - Adding Basic Security

Click here to view the full tutorial

Continuing series: Developing a Grails Web Application.
Tutorial will: walk through steps of creating a robust Grails web application.
Application name: grails-mongo-demo
The Goal: To track and record persons addresses.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request
Previous Post: Grails Part 4 - Enhanced UI Design w/ tw Bootstrap
View Project on Github

These Instructions Will Walk Through:

  • Configuring Spring Security Plugin
  • Adding static URL maps
  • Bootstrapping in security data
  • SecurityTagLib - Conditionally display gsp content
  • Configure Security Pages with Twitter Bootstrap UI

Grails Part 4: Enhanced UI Design w/ Twitter Bootstrap

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request
Previous Post: Grails Part 3 - Building a CRUD Application
View Project on Github

These Instructions Will Walk Through:

  • Enhanced UI Design with tw bootstrap
  • Configure Bootstrap with lesscss-resources plugin
  • Adding a custom controller
  • Customizing Scaffold Templates
  • Improving Scaffold Output


Grails Part 3 - Building a CRUD Application

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request
Previous Post: Grails Part 2 - Configuring the Plugins
View Project on Github: Grails MongoDB Demo

These Instructions Will Walk Through:

  • Building a CRUD Application
    • Building the Domain Model
    • Controllers using Grails Scaffolding


Grails Part 2 - Configuring Web Application Plugins

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request

Previous Post: Grails Part 1 - Setting up a Project on Maven With IntelliJ
View Project on Github: Grails MongoDB Demo

These Instructions Will Walk Through:

  • Configuring the Plugins
    • MongoDB
    • Jetty
    • Spring Security
    • Apache CXF (WSClient)

Grails Part 1 - Setting up a Project

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration
View Project on Github: Grails MongoDB Demo
These Instructions Will Walk Through:
  • Installing the Grails SDK
  • Project Setup
    1. Creating a Grails Application
    2. Integrating with Maven
    3. Integrating with IntelliJ

Applying the Strategy Pattern in Java

Working with strategy on Arrays.sort & java.util.Comparator

All these examples and more can be found on github
In Java, you cannot pass function references through a method. With this being true, how do we then apply a strategy to a method? What is suggested, is that we define an interface with the function we want to pass. This way we can on the fly, define and pass an algorithm to a method for process.

In this demo, we will be working strictly with the strategy interface: java.util.Comparator. This is a java strategy interface, readily available in the Java API as of 1.5. It supplies a compare algorithm, where its implementation is primarily for sorting large collections of objects.


Atmosphere Websockets & Comet with Spring MVC

Since writing this guide, Spring has come out with their own Comet and Websocket implementations. I suggest you first read through their guides before deciding on atmosphere as your Comet & Websocket solution.

1. Messaging with Stomp over Websockets
2. Spring Messaging

On to the guide...

Feel free to clone the source of this project on github.
springMVC-atmosphere-comet-websockets

The Websocket protocol is defined by W3C here. The protocol is considered an HTTP upgrade protocol. Essentially what happens is, a websocket request is made by a client browser, via the HTTP protocol. Once, the request is in, the server should respond with a success. Then the browser and server establish a TCP link to handle communication from there on out. This protocol enables bi-directional communication between a client and server. Here is a link to browser and servers that support Websockets and/or Comet: comet/websocket support link