RSS

Category Archives: BookSentry

My pet project

Vaadin Intro

Episode 24

While being in the topic of web framework research: another one I’m considering to use is Vaadin. It has been around for quite some time, over 10 years now, so we can say that it’s mature (or “old”, if you like). Anyway it has large and active community, and is quite popular which is always helpful.

 vaadin-logo-hi

Vaadin is a server side centric framework, meaning that all content is rendered to html and passed to browser on each request. It’s a bit old school approach. Nowadays it’s more popular to keep as much as possible on the client side, mostly as Java Script and communicate with server only when needed. Vaadin guys argue that server side approach is safer, because it’s impossible to temper with the executable code.

There are other pros and cons, but what is important to me, that current version of Vaadin uses GWT as rendering engine. As you may recall from previous article I’m a big fan of GWT, since I believe that having all code in statically typed language under decent IDE control is a must for any non trivial web project (I’m yet to meet a happy Java Script programmer after successful and pleasant refactoring). But enough introduction, let’s get down to business.

To start easily with Vaadin I recommend The Vaadin Book. You can get it for free from Vaadin website (after registration, but still nice move). In short: get the Ivy and Vaadin plugins for Eclipse, then create new Vaadin project. To make things simple, you can ask the plugin to create project stub. We will rewrite it just a bit then to get something similar to what we had in articles about GWT and Spring.

In order to get things going we actually need only two classes. First doesn’t even have any code, just two annotations and a parent. It’s a Servlet class with a standard url mapping (this time as an annotation) and some Vaadin specific configuration to associate with GUI content class. Also, it extends VaadinServlet.

The content class needs to extend UI and implement init method where the page is created and hooked much like in GWT entry point. The @Theme annotation is optional, it provides us a default css and makes our button look nice. I’ve also added a Service class as our “backend”. Since everything is on server side, you don’t need synchronous and asynchronous interfaces, rpc mappings, callback and all that stuff. Just create class and call method (it’s an example, in real life you would want to have some dependency injection here). That’s all, deploy on server, run and enjoy your Hello World.

vaadincode

Comparing to pure GWT, Vaadin is much simpler to start. You don’t need any html to hook your Java code. You don’t need server communication handling. You don’t need module definitions in xml. You don’t need dev mode and all the acrobatics to debug client side code. The price is however page responsiveness. I’m not sure if it’s a high price though, keeping in mind an average modern bandwidth and latency. And you still keep all the luxury of not worrying if your method gets three Cats instead of two Dog it expects.

There is also a possibility to write client side code in pure GWT and integrating it with Vaadin but it pretty much brings all the GWT complications back.

Still better than JavaScript :)

lovely_night_fury___how_to_train_your_dragon_by_mikilake92-d50tuim

 
Leave a comment

Posted by on November 23, 2014 in BookSentry, Technology

 

Tags: , , ,

Google Web Toolkit intro

Episode 22

One hundred days of my writing have passed :) Today we are going to talk about a cool and somehow unique web framework. Meet Google Web Toolkit.

gwt-logo

What makes GWT so cool? You write all the client side stuff in Java, having all the power of statically typed language and power of your favourite IDE at your disposal. Its then automatically compiled to Java Script without having to worry if it’s for IE, Firefox or Chrome. Let’s begin.

First you need to install Google Web Toolkit SDK plugin on your Eclipse. A blue g icon will appear in the tool bar. Click it, Create a Web Application Project, enter name and package, uncheck Use Google App Engine and Generate project sample code as we will start from scratch. After finishing, new project will be created.

We will start with a module, New -> other -> Google Web Toolkit -> Module. Call it hello and finish.

Then we will create an Entry Point Class from the same menu, and use the hello module. Add RootPanel.get().add(new Label(“Hello World”)); in onModuleLoad method.

To root our GWT generated code in the web application, we need a html page, using wizard from the same group. You now need to provide a correct JS path in script src property, starting from the war folder. It should go like that: “<package>.<module>/<package>.<module>.nocache.js”.

Now we will prepare to launch our app in Dev Mode. This is a powerful tool that uses a browsers plugin to communicate with Eclipse plugin, recompile and push changes you made in near real time. After saving Java file, you just hit refresh in the browser and changes will be there. Unfortunately API for dev mode was dropped some time ago (WTF?) so you will have to either downgrade your browser or use older portable version. You will need Firefox 26 or older or Chrome 32 or older. I’ve used Chromium Portable version 24. You can get the plugin here. .

Select your project, Run As -> Web Application. It should start a new view called Development Mode. After several seconds it will start and display the link you should use in your browser with plugin, it will be something like this: http://127.0.0.1:8888/hello.html?gwt.codesvr=127.0.0.1:9997. Follow the link and enjoy your Hello World in GWT.

Let’s quickly look at something more. We will add a call to server in order to get the greeting. Since the client is operating in asynchronous mode, and server does rather the opposite, to tie them up you actually need two interfaces that express the same contract. Both goes to the client package.

On the client side the return type is void, while there is an additional callback argument that will get executed when the call finishes with data we need provided.

Methods on the server side interface returns the String we need and have an annotation @RemoteServiceRelativePath(“greet”) to bind the service implementation with request path.

The concrete service needs to implement our synchronous interface and extend RemoteServiceServlet and be placed in the server package. You will also need an entry in web.xml to bind the service class to servlet name and the servlet url from interface:

gwt web

Having the back-end ready we can come back to our entry point class. First we have to create the service handle. Then we will add a button with ClickHandler that will invoke the service. Finally in the callback code we will add a Label with String computed on the server side. Or, in case of problem, display the message from exception. Here is the Java code:

gwt

When you run this, after pressing the button, the Hello World! will appear after a brief moment.

Unfortunately as of version 2.6 of GWT Java 8 is still not supported, so you can’t exchange listeners definitions with concise lambda syntax yet.

Some people say, that GWT is dying. Lack of support for Dev Mode in newest browsers being primary reason for that often. Well, in fact there is something called Super Dev Mode which is going to fulfill similar role. I hadn’t tried it yet, but perhaps I will have to in future. Also, Vaadin is using GWT as their rendering engine, and they seem to be doing their best to keep GWT alive (they are organizing the GWT.create conference, for example). For me, writing Java code instead of JS is a huge deal. Yeah, there are frameworks for JS to make it more manageable, but still, this is dynamically typed language. No automatic refactoring. No way to automatically find all places where something is called / used. Typo in variable name found only in runtime. Maybe after working a bit with AngularJS, so hyped now, I will change my mind, we will see about that. Also, before committing to GWT in my project I wanted to check out Vaadin. See you next time.

412mtghRr-L

 
1 Comment

Posted by on November 9, 2014 in BookSentry, Technology

 

Tags: , , ,

Dreams of Spring

Episode 21

Going back to my project, which was a bit on hold for some time, I finally some time to to move things forward a bit. Just a bit, as you may recall I made a decision to use Spring as a framework and GWT for view. I’ve worked with both for quite some time, but apparently never tried to do anything from scratch. Let’s fix that and start with Spring.

cropped-logo_springbypivotal_horizontal1-645x300

Inspired by Josh Long’s talk on last Jax conference, I’ve checked the Spring Boot. There is a nice website which asks you for:

  • Few descriptive details of your project
  • If you want to use Maven or Gradle. As I mentioned earlier, I will go with Maven.
  • If you want Jar or War. As usually War is the solution.
  • Java or Groovy. Uhm.
  • Java and Spring Boot version

And then several groups of dependencies you can include in your project, like:

  • AOP and Security
  • Data: JDBC, JPA, MongoDB etc
  • I/O: Batch, JMS etc
  • Web: Web application, Websockets, WebServices…
  • Template engines
  • Social: want Facebook integration out of the box? No problem.

For online hello world you probably only need to check web. After filling in all stuff simply hit Generate Project, download, unzip, and put in your Eclipse workspace. Then import from existing Maven project and here it is. You don’t need to download anything and figuring out where to put this and what to add to some eerie xml files. Maven will handle it for you.

To see something in runtime, just put any html page in src/main/ webapp, Add application to your build-in Tomcat, publish and start. Watch console, If everything went smoothly, you should see server starting in less than a second, and then a lovely Spring Boot ascii artish logo followed by some info outputs over a few seconds. You may now check http://localhost:8080/appname/pagename.html and it should present you the very page you have created earlier. Congratulation, you have now Spring application that does nothing useful but starts twenty times longer than simple dynamic web application! What an achievement. Let’s do something with that.

First let’s see what was generated for us.

  • java, annotated with @Configuration, @ComponentScan and @EnableAutoConfiguration and containing good old main method. You don’t actually need any server to run Spring, you can just run this class as Java Application atop of pure JVM.
  • java – used to bind Spring as http request handler when run on server.
  • Some empty junit test for Application class.
  • pom.xml, Maven project definitions and dependencies

Time to add something. We will create a very simple MVC to display “Hello World!” from the depths of backend.

Our back-end in this case will be class marked with @Component annotation, which allows Spring to manage its injection in the Controller class, annotated with, well, @Controller.

service

Controller has a field which will be initiated with our back-end buddy. It also has a method to handle request, and we will annotate this with @RequestMapping(“/*”) meaning it will catch all http requests to our pretty little application. Now we need to return from the method something that will indicate where the request should land, and that’s the job of ModelAndView object initialized with name of page to display.

To tie back-end and front-end together, we will add a model attribute with value provided from our component.

controler

As quick and dirty view we will create a JSP (sorry for that) page, that will reference model with el expression ${message}. Simple as that.

Publish, restart server, type http://localhost:8080/appname/blah in the browser and here it is, your first Spring MVC web application, sweet and shining.

fb125-how-to-train-your-dragon-2-toothless-cute-art-wallpaper

 
3 Comments

Posted by on November 2, 2014 in BookSentry, Spring, Technology

 

Tags: , , ,

Legacy of the Void

Entry 11, Day 24

I’ve promised you episode on database, but I’ve lied. Okay, not exactly. Database was my plan, but I haven’t managed to play with the tools before leaving Wrocław for my short vacation. I’ve left behind my dev machine so let’s leave tools too for a week or two. But I’m not leaving you, dear reader.

Until now, I was mainly writing about enterprise stuff connected with my project. There are however many other topics I’d like to touch in this blog. One of them is Object Oriented Design. So today, for starters, since at the beginning there was nothing, I’m going to tell you about the Null Object, behavioural Design Pattern.

The definition says:

In object-oriented computer programming, a Null Object is an object with defined neutral (“null”) behaviour. Example:

nullobject1

Why bother? Because using Null Objects protects you from Null Pointer Exceptions and lets you get rid of ever-present null check logic in the code. So, instead of using null Animal references, you want to use NullAnimal objects. It will not throw NPE in your face, you don’t have to check if it’s there. It will just gracefully do nothing if interacted with. This is the classic – dedicated Null Object class, now let’s move to other, not so obvious usage.

Say you have some kind of state in the form of an enum. But sometimes the state is not present, or somehow unknown. You may use null for that, but you may also add an additional value – “UNKNOWN”, “NONE”, “SHIT_HAPPEND” or something like this. Usually cleaner and safer than null reference.

Third idea, if your object contains a collection, initialize it. Don’t rely on it being delivered via setter later. It is likely that the setter won’t be called, “because of reasons”. But if you put an empty collection at object creation, you are safe to iterate over it later, unless somebody explicitly sets null there, which is rather unlikely (you may also hide the setter itself if you like). There are situations where such practice turns out to be bad however. Be aware that empty collection has some size. It depends on system and VM implementation, but you can count from 40 bytes on ArrayList to around 70 on HashSet. Not much, but I had a case where graph node had several empty collections and when node numbers went into millions, there were corpses of virtual machines lying everywhere.

Finally, there is a nice class in Google Guava, the Optional<T>. It’s an immutable wrapper that can contain another object or null. It can return this object, or null or some default value. It can tell if there actually is an object, or not. It’s good, because you explicitly say that it’s okay to have null value here. The value is optional, as the class name says. Let’s see how it looks like in practice:

nullobject2

That’s about it. There are some issues to consider though. For example – Null Objects shouldn’t change their neutral behaviour. If they do, they actually belong to State Pattern. Null Objects may be special case of Strategy Pattern, representing strategy that well… does nothing.

There are also some caveats in Null Objects. Sometimes clean logic and lack of exceptions may hide erroneous program execution. So, for example, if you expect collection not to be empty, you should do some check before simply iterating over it zero times and move on in silence.

Project-wise, well… I’m proud owner of booksentry.org domain now, where you will find the application’s production environment in the future.

In the next episode, we will talk about SOLID code. Stay tuned.

Legacyof the Void

 
Leave a comment

Posted by on August 25, 2014 in BookSentry, Clean Code, Technology

 

Tags: ,

Into the Wild

Entry 10, Day 20

Time to talk a little environment. Go green, plant a tree, that kind of stuff. Kidding. We are going to talk about the server and the data source for the application. First, what is actually a server?

Server in general is something (hardware + software) that provides responses for requests across the network (or even locally). We are mainly interested in web servers, so we have a http request and http response. This doesn’t actually narrow down to www. They are WebServices carried over http via SOAP. You know, machine talking to another machine, skipping the unreliable protein-based components. Since I’m a programmer, not a sysadmin, by the term server I will understand the software part.

There are basically 3 tiers of server, we would be interested in:

HTTP Server. This is basically just to serve static resources, including client side scripting languages. You can build quite a big piece of an application in GWT, and as long as it does not require any server side dynamic behaviour (say database access), you are good. But who doesn’t require database access those times? The unbeatable king of http servers is the Apache HTTP server. The piece of software that gave name to the entire organisation later. Before Java settled in the web, we would just take Perl or PHP, put it somewhere around Apache, lay hands on database, or whatever is needed and be happy. But now we have…

Web Container. It is more than http server, since it implements a tiny but important part of Java Enterprise Edition api. Namely Servlet and JSP. Those seem to be a bit outdated technologies, but they are a basis for every new and fancy stuff in Java web world. For example, Spring is actually a big servlet underneath. Again, there is a clear leader in this field, Apache Tomcat. But what, if you need more?

Application Server. The monstrosity that implements entire JEE specification. So, besides what web container offers, we have JSF, persistence, transactions, messaging, batch api and other stuff, but perhaps most notably: the Enterprise Java Beans. There quite a few application servers out in the field. I’ve worked with Weblogic and Glassfish. If you don’t like Oracle, there is also JBoss (it is called WildFly now, as I’ve just learned). Or Apache Geronimo for anarchists.

So, what do I actually need? Since I’m going to use Spring, and Spring has many features that replaces JEE standard (Actually many apis in modern JEE were inspired by Spring.), I’m not going to need full application server. Web Container is enough for Spring, so it should be enough for me. Since picking up a popular server simplifies hosting and development in general (lots of people have the same problem as you) I’ll go with Tomcat 8.0.9. If there would be a need for an application server in the future, we are going to have a migration. Shouldn’t be tough, should it?

And what about database? I have serious time issues recently, so in order not to keep this post too long in the draft state, we will talk this through in the next episode. Stay tuned.

Project-wise, I’ve set up Tomcat, created a project draft, installed MySQL and did some smoke test displaying stuff from the database via simple servlet. Nothing fancy, just hellowording. Now I’m looking for some nice MySQL client besides command line :) Any suggestions?

How-To-Train-Your-Dragon-2-Wallpapers-23

 
1 Comment

Posted by on August 21, 2014 in BookSentry, Technology

 

Tags: , , ,

The First Artifact

Entry 9, Day 16

     “The product backlog is an ordered list of everything that might be needed in the product and is the single source of requirements for any changes to be made to the product.”

– Scrum Guide

Being recently in the tools department, let’s continue the topic and look for something to manage The Product Backlog.  But wait, what’s that?

As I’ve said, I’m not going to do full fledged Scrum here, because there is no point in that. However I will keep some of Scrum elements. For example, Scrum defines so called Artifacts. There is the Product Backlog, there is a Sprint Backlog and there is an Increment. I’ll keep the Product Backlog. I will probably not need Sprint Backlog. And we will see about Increments later.

So, the Product Backlog (or simply the Backlog from now henceforth) is where the idea of the product is kept. The idea might be more or less clearly and precisely defined, usually the higher the priority, the better the definition. The Backlog is composed of Items. Single Item might be a requirement, feature, enhancement, bugfix or anything that is going to change the product. Changes may be indirect and be without immediate value, say “Setup Tomcat” is actually an internal task, but it is required to proceed for me.

My needs are simple. I need a tool where I can add items in at least three categories – to do, ongoing and done. I need to be able to easily change priority (distance from top of the list), move between categories (say it’s ongoing / done) and edit Items. The tool should be cloud based (fancy term recently), free and publicly accessible.

In my work, we have mostly used Excel with quite advanced scripting (hi Kamil ;)) to help with moving, changing and calculating stuff. Sometimes we would just take a pin board and stick notes to it (It is common with Sprint Backlog, but not really with Product Backlog). Or simply there was no public Backlog whatsoever (well…).

But Excel has drawbacks. First, it is a desktop tool, and I want to share stuff in the blink of an eye. So, I could go for Google Spreadsheets, but still manipulating Items would require some effort. Let’s look for something more dedicated then. Quick research convinced me that there is an overwhelming number of tools to choose from. You can check out Wikipedia’s comparison of project management tools, You can then narrow the criteria to Agile tools. I’ve taken a look here and there. I’ve decided to give some of them a try.

So first, there is Jira. It was used by some teams around me. But apparently, it is not free, and is too big for my needs. We need simplicity. Out.

Someone mentioned Any.do when I was sniffing around. Unfortunately, while it’s simple, it’s also more of a calendar tool, not really good for me. Out.

Then, there are quite a few tools (I’ve checked iceScrum), that are open source, but there is no free hosting. Either you download and host by yourself or you pay. I want it quick, I want it now, I want it free. And I don’t have a hosting department operational yet. Out.

There are also promising tools closed source, but they are paid (e.g. Agilo for Scrum). Well, out.

There was one tool that was free and looked nice. The AgileFant.  I’ve played with it a bit, but it turns out to be too cumbersome for me. There were some UI glitches and manipulating items was not that simple as I wanted. Still it might be a valid option for fully Scrum team.

And finally there was Trello. Few friends actually recommended me this and it turns out to be perfect for my needs. It’s simple, intuitive, adorable and it is flying free above the clouds. As a welcome, you get three boards: to do, doing and done. You can quickly add Items, move vertically and horizontally, edit, add categories and make the board public. I’m not going to give you full review, that’s not what I’m here for. But you may want to check article here. The Wired had an article about coolest 7 startups you haven’t heard of, and Trello was one of them.

So, I have a lingering Product Backlog. You may check it out here. The link will be always available from the BookSentry, the main project page.

Huh, that was a long one. In the next episode, I’m going to talk about the environment. Namely the Server and the Database.

p.s. I’m using Wikipedia links a lot. It’s not because I’m a noob. My intent is to give you some quick and neutral overview of the subject at hand. You can get easily to subject’s home page (if it has any) from Wikipedia page for further, official details.

dragon2-4

 
1 Comment

Posted by on August 17, 2014 in Agile, BookSentry

 

Tags: , , ,

Tools of the Trade

Entry 8, Day 14

Over ten years ago I have written my home page in PHP. Nothing really big, but it had a user system, administration, news, comments, file downloads, image gallery with some search/paging options, rating etc. I think there was around 100 – 200kB of code. I did this almost entirely in Windows notepad, only at the end switching to some kind of editor with html syntax highlighting. But things can be done easier.

First, you need an integrated development environment. I will use Eclipse Luna, released not even two months ago. I’m using Eclipse for over five years and still learning new stuff on a weekly basis. Alternatives are: InteliJ, NetBeans or something else.

When dealing with web application, there are several useful tools on the browser side. First, the browser itself and it’s integrated development tools, at you disposal when dealing with html, css and client side scripting problems. My favourite is Firefox. When browser tools are not enough, I recommend Firebug, excellent for debugging JavaScript if you are in a pitiful situation that you need to do that (you shouldn’t if you use GWT, but…). In order to work with GWT efficiently, you need DevMode, it will compile your Java code to JavaScipt on the fly and allow to see changes almost instantly, Yo will need a plugin for Eclipse and a plugin for your browser to accomplish this. Unfortunately since Firefox 27, support for DevMode has ended (wtf dudes?), so either stay at 26 or try some workaround.

Compiling, putting into jars, bundling etc. – Maven is your friend here. If you are old school, get back to Ant.

When you write something and it works, or even before it does, you are in need of some version control system. As of my information, Git recently took over SVN in terms of popularity. Also, I’ve worked long enough under SVN, so lets try something different. Git it is.

Having a build automation and version control, you can put them together in continuous integration tool. It will take the code each time someone does commit and each night, build stuff and scream at you if you blew something. Jenkins is probably the best choice here. You may know it’s older version, Hudson, which killed itself after being taken over by Oracle.

There are plenty of additions to CI, most of them also available as IDE plugins.

Most notably, test automations frameworks. Unit tests for low level stuff (go JUnit with Mockito) and web/GUI tests for high level/integration (Selenium being a popular choice).

It is good to have some kind of static code analysis. Teach it a set of rules, and it will guard your code from deteriorating at low level and perhaps find some bugs for you, that might have slipped through runtime test suite. Check out FindBugs. For higher level of analysis I recommend SonarQube (known simply as Sonar in the old days). It will also give you a bunch of numbers about your codebase, if you like numbers. I like numbers.

There are plenty other useful stuff for your CI, e.g. some dashboard view, excellent to de displayed somewhere high on the wall, to tell others that your build is never broken. Or to tell you that it actually is now and it would be nice to fix it. You may also want to hear this.

Besides development, you want to keep track of what is to be done, ongoing and done. So basically a product and a sprint backlog. Until now I’ve used mostly simple Excel and physical pin board, I need to do some research here and get back to it.

Also there are systems for issues tracking (fancy term for bugs), which may be used instead of, or with backlogs to help you control your chaos. (Hi Kate ;))

Pedal_control

 
2 Comments

Posted by on August 15, 2014 in BookSentry, Technology

 

Tags:

Here be dragons

Entry 7, Day 11

It seems that I’m more of a writer than a coder in this little endeavour. Well, old habits die hard, I wrote quite a lot of stuff several years ago, although not really technical. Anyway, this is good. Before you sit to code in the new projects, there are some stuff, you need to discuss with other devs, the Product Owner, Stakeholders/Clients, right? (Yes, devs can talk to Stakeholders directly, even though the PO is their default world interface. It can be very beneficial to the project). So now, let’s wander into the unknown lands of dragons. The enterprise (and less enterprise) technologies. There will be a lot of links.

Usually, there is a number of so called “technologies”, you will want to use. Theoretically, you can write an application using almost pure Java on top of application server (or what the hell, even without the server, just play with sockets manually), but it is impractical to say the least.

Under normal circumstances, the technology choice depends on what you want to achieve, what are the skills in the team at your disposals or other external factors like deals with big nasty software corporations. Since I’m my own client, I’m free to do what I please. I will go for some healthy mix of things that are suitable, fun, that I want to learn, and that I already know, and want to know better. Using only unknown technologies might be more beneficial from the learning point of view, but being struck at every aspect of the application might not be funny (not that I’m a pessimist, but I’ve seen stuff…). So I need some fields, where I know I can go fast to repair my ego if needed.

So, the main technology I will use is Java 8 language, as I’ve mentioned in the previous post.

Of course, since we are talking web application, the backbone of everything is HTML, this is what the user receives at the end.

There must be some kind of presentation technology (unless you are writing some purely backend EDI system or something similar). If you are truly old school, you can go with JSP pages. A bit more modern approach would be JSF or Velocity. There are plenty fish in the sea.

Client likes the application to be responsive. So instead of reloading the entire page, you can do things in the browser, instead of on the server. We can go with JavaScript, Dart, or my favourite: GWT. Actually GWT will do the job with both static and dynamic web content, and it is a Java compiled to JavaScript. I’m a big fan.

Then there is (usually) the framework that simplifies things (or complicates, depending on the framework and usage). Spring is the choice here, since it’s tremendous popularity in recent times. I’ve used it quite a bit, but there is still much to learn for me.

There is an object-relational mapping framework, Hibernate being popular choice.

Those are the core technologies. There will probably be many more, but we are not doing Big Design Up Front, remember? I will add and comment on new stuff as needed.

By the way, I think the word technology is sometimes an overkill when it comes to software. Nuclear bomb is a technology. Internal combustion engine is a technology. Microprocessor is a technology. Although Wikipedia says that technology is “collection of tools, including machinery, modifications, arrangements and procedures used by humans”, it seems to me that a piece of software written by a guy over several evenings while drinking beer shouldn’t be called that. Not that I don’t respect contribution to mankind well being, but there is a matter of scale. Probably “library” is sometimes a better word than “technology”.

dragons

 

 
Leave a comment

Posted by on August 12, 2014 in BookSentry, Technology

 

Tags: , , ,

The Peak of the Mountain

Entry 6, Day 10

The technology stack… We’ve talked a bit about tiered application composition, now let’s take a broader look. Software is basically composed of layers. That simplifies things. You have an operating system using hardware directly. Then, there is the Java runtime providing abstraction between Java bytecode and actual program execution. Then, if we are in the JEE realm, we have a server.

The kind of server depends on you needs. If you like EJBs, you need an application server (Weblogic, Glassfish or whatever). If you prefersomething lighter, you can go with web container (like Tomcat). Finally to help you serve static content like Java Script (which is static in terms of being executed on client machine, not ours), there are http servers like Apache. You can combine all of them – use application for EJB layer, web container for web layer and http for static. There might be, of course, many more servers in the application ecosystem, namely proxies, firewalls, load balancers (or it can be dedicated hardware, and usually is), but this is more of the infrastructure domain, not application.

Then there is some kind of framework providing abstraction over JEE api or just complementing it. Think Spring.

And, at the peek of the mountain, there is Java language. The language shapes architecture of your system, ties things together and allows you to play with stuff. I’ll go with Java 8 because well… cutting edge, you recall, do you?

Actually Java 8 is quite cool. Everyone says that’s because finally we have lambda expressions, but there is more. There is, for example, internal iteration.

Internal iteration, instead of imperatively iterate over collection, allows you to declare iteration and let the runtime do the rest. Why it is important, you may ask? Because if you iterate imperatively you do that either in sequential way, or you have to play with threads, think about number of available cores and do other tedious and error prone stuff. If you let the runtime iterate, on the other hand, it may perform e.g. a new forEach operation concurrently, using all available cores, and this is transparent for you. It makes things simple and fast.  Also, a new Stram api allow you to perform map, filter, reduce, and other operations with performance improved by doing what really must be done lazily. I haven’t yet explored all new features of Java 8, but things look promising.

<scary JVM internal stuff>

By the way, lambdas aren’t just syntactic sugar for inner classes as many people think. In fact, lambdas implementation in virtual machine uses additional layer of abstraction. Lambda is actually a recipe, not an imperative piece of bytecode. It’s true bytecode is generated on runtime, not compile time, using fifth invocation mode, invokedynamic, introduced in Java SE7 to support dynamic JVM languages (Groovy, Jython etc.). Lambda call site is created using so called “language logic” or “bootstrap method” that was to be provided by specific dynamic JVM language to help dispatch calls. In Java language lambdas execution, such logic is called Lambda Metafactory.

As a result of several nifty VM tricks, lambdas are much faster than inner classes, especially on multicore systems, and have a potential to be even faster in further versions of Java. If you are into this stuff, check out a cool presentation by Brian Goetz, the Java language architect.

</scary JVM internal stuff>

So, I’m going to use Java 8. And few other runtime technologies, which I will discuss in the next post.

top of the mountain

 
1 Comment

Posted by on August 11, 2014 in BookSentry, Technology

 

Tags:

The Matrix

Entry 5, Day 8

The first matrix I designed was quite naturally perfect. It was a work of art. Flawless. Sublime. A triumph only equaled by its monumental failure.

– The Architect

Software Architecture is the high level structure of a software system. Now I’m smart, I have Wikipedia.

Sometimes, if you ask a developer what is the architecture of the system he is working on, he would say something like, “Well, we have a JSP view, we use EJB to figure things out, Hibernate to persist and everything sits on top of Weblogic server”. Actually, this is not an architecture description, this is a list of technologies used to create the system (or, if you need a fancy term: the technology stack). Of course, there is a slight hint, that we are dealing with a client – server application with at least three layers.

Architecture might be client – server, peer to peer, event driven, rule based, plug-in based or other. Or the combination of them.

As I said before, I’m aiming for agile, emergent design. It does not imply, that I don’t have some kind of vision of what I want to write. I will probably go with client – server, multi tier application. What does this mean? We know more or less, what is 3 – tier architecture: we have a Presentation layer, Business Logic layer and Data layer. So where is the fourth and fifth or any further layers? Well, you may divide those layers further. Let’s take a look.

Presentation is well… presentation. But if you go beyond simple CRUD, it might turn out, that you need some kind of logic just for managing presentation (fancy drag n drop and dynamic stuff support for example), not really business logic layer, but layer separate from static HTML inputs. So, you may split this into two.

Business Logic might have controllers, which glues together front end and back end. And then, they might have EJBs or Spring Services, which performs actual computations or choices having data and given user input. Or other input for that matter, say JMS messages or WebServices calls from foreign systems. Also, you may split service in as many layers as you need depending on the case. Some systems might be very sophisticated, and saying that there is just one business logic layer would not be an understatement.

Data layer may consists of Data Access Objects, which gives you data in the form of cute little Java objects magically drawn from the database. Then there is the magical Object Relational Mapping Framework at work, preparing and running queries in SQL native to the database, transparently for the user. Database is the, more or less, final layer, and besides table definition it may consists of triggers, scheduled jobs (for partitioned tables periodic cleanup, for example) and some other nasty things. Perhaps there are also other layers in the database department, but well… I’m more like in the Java department, so I won’t dig deep here.

Some developers shun the idea of doing any logic outside of Java code (I’m not talking about external libraries, but I will later). I was such developer once. But I’ve figured out, that some things may be better done outside of Java, even though I cannot put a breakpoint there. I’d love to think about them as Java minions who do some magic yet inferior things, but as long as they do it well, I have no objections. Otherwise I will have to frown my eyebrows, get out of coziness of my beloved Eclipse and step in to figure out why SQL or ant script is behaving badly.

 

vsbigone

 

 
Leave a comment

Posted by on August 9, 2014 in BookSentry, Clean Code, Technology

 

Tags: , , ,