Showing posts with label Beginning Java. Show all posts
Showing posts with label Beginning Java. Show all posts

Tuesday, October 13, 2015

Exploring CQRS with Axon Framework: Building the Entity, Aggregate, Aggregate Root and Repository Components

The previous post provided a basic explanation of Entity, Aggregate, Aggregate Root, Repository and Domain Events which are some of the building blocks in Domain Driven Design. This post looks at using Axon Framework to create some of these building blocks.

In Exploring the Commands, Command Bus and Command Handler, we looked at the command handling aspect of CQRS using Axon, where we had commands created, that were dispatched into a command bus via a command gateway which were then subsequently handled by their respective command handlers.

But in that post, all what the command handlers did was to print to the console. In real life applications, you would want the commands to lead to some business logic being executed, which might lead to state change in the application.

Achieving that, is what we look into in this post.


Monday, October 12, 2015

Exploring CQRS with Axon Framework: Overview of Some DDD Concepts

In Exploring the Commands, Command Bus and Command Handler, we looked at the command handling aspect of CQRS using Axon. We created commands that were dispatched into a command bus via a command gateway, which were then subsequently handled by their respective command handlers.

But in that post, all the command handlers did was to print to the console. In real life applications, you would want the commands to lead to state change within your application. Achieving this is what we look into next, but for us to do that, we would first need to explore some concepts that stem from the Domain Driven Design world. Namely Entities, Aggregates, Aggregate Roots, Repositories and Domain Events.

At this junction, it is worth mentioning that concepts and ideas from DDD always pop up in a typical CQRS discussion...this post and others in the series are not an exception, therefore, at least a basic understanding of these DDD concepts will be beneficiary.

This post will provide a quick overview of the relevant DDD concepts without touching on Axon Framework, while the next post will look at how Axon is used for their implementation.

That been said, let us quickly have an overview of the relevant DDD concepts that we will be needing in our exploration of CQRS with the Axon Framework.


Monday, August 24, 2015

How Well Do You Know Your Collection API?

I recently spent some time going over the Java collection API and even though, it can be said it is one of the most used API in everyday programming with Java, I still had some "Oh I totally forgot about that" moments to "I did not even know that was so" realizations.

I will note down some of the interesting observations in this post, being as succinct as possible and backing with code examples when needed. It is intended to be a place I can always consult to get an overview of the API.


Friday, July 31, 2015

Introduction to Spring Boot as a Servlet Based Web Application Framework

This is the fifth and last post in the series on Introduction to servlet based web application in java. The previous posts in this series are:
  1. Introduction to the Servlet API, Web Server and Servlet Container
  2. Introduction to .war Packaging and Maven
  3. Quick Overview of Basic Maven Concepts
  4. How to Create .war Files with Maven Archetypes and Deploy to Tomcat
  5. How to Create .war Files with IntelliJ and Deploy to Tomcat
These posts have touched on web servers and containers, the servlet API, packaging war files and how to achieve this using built in support in IDE or via maven archetype. This post would conclude the series by taking a look at Spring boot.


How to Create .war Files with IntelliJ and Deploy to Tomcat

This post is the fourth post in the series of post on Introduction to Servlet based web application development in Java.

Previous post shows how to create Maven projects, package and deploy to either a standalone server or an embedded server, by directly using the Maven archetype; but since in practice, an IDE would be used for development, this post shows how to do the same using an IDE.

Intellij IDEA is used in this post. If using other IDEA (say Eclipse or NetBeans) you would have to consult the help section or online documentation for the corresponding steps in the IDE you use.

This post is adapted from Developing a Java EE application. A post which shows how to create and run a JEE web application, but it does not accomplish this with Maven and uses Glassfish as the application server instead of Tomcat which we are interested in.

What this post does is to modify the steps to include usage of Maven and then show steps on how to deploy to Tomcat


How to Create .war Files with Maven Archetypes and Deploy to Tomcat

 Maven Archetypes are project templating mechanism, implemented as a Maven plugin.Archetypes make it possible to define the barebone of a project’s structure and then use that to create other projects.

This post, which is the fourth in a series of posts on Servlet based web application development in Java, shows how Maven archetypes can be used to generate a Web application Maven project; how to edit it to print “hello world”; and also, how you can use Maven to package it up so it can be deployed to a Servlet container.

We would make use of two Maven archetypes to achieve this:
1. The maven-archetype-webapp  and
2. The Servlet3-maven-archetype

Let's get down to business


Quick Overview of Basic Maven Concepts

This post is the third in the series of posts that Introduces Servlet based web application development in Java. It would be an overview of Maven and it serves to provide the necessary basic information about Maven before we proceed to other post in the series that touches on how to use Maven to package .war files and to use archetypes to create and deploy a .war to Tomcat.

An in depth coverage of these Maven concepts or Maven capabilities in general is not something that can be covered in a single blogpost; what this post does then is to only touch on some of the concepts, especially the ones that would help in making sense of the topics that will be addressed further on in this series of post.


Introduction to .war Packaging and Maven

The .war (Web application ARchive) is a packaging mechanism for putting together the application code of a web application to be deployed to a Servlet container. It is nothing complex and can be considered similar to ZIP files, the only difference being that it contains java related meta files that distinguishes it from regular ZIP files.

And just like ZIP files package up directory structures and their contents, .war files also do the same, the only difference being that the directory structures has to be laid out in a particular format.

This post is the second in a series of posts on Servlet based web application development in Java. It will thus introduce .war files, their structure and how to create one using Maven.


Introduction to the Servlet API, Web Server and Servlet Container

This post is the first in a series of posts on Servlet based web application development in Java. It will introduce the Servlet API and Servlet Containers.

Let's get to it...

Okay so you want to build a web application using Java? But as everyone knows, a web application needs to be hosted by a web server right? Right!

So maybe you can use the Apache HTTP Server or Microsoft Internet Information Services (IIS)...?

No.

No? But they are both web servers, aren't they?

True they are web servers, but remember we are going to be building Servlet based web applications? That means we need a web server that can, not only functions as an HTTP web server, but one that also serve as a Servlet container and can execute code written against the Servlet API...

"Servlet API? One that can serve as a Servlet container"? What does these mean?

Let us attempt an explanation…


Introduction to Servlet Based Web Application Development In Java

This post is the first in a series of post that introduces Servlet based web application development in Java.

The Goal

The aim is to help the reader, quickly create a mental model for Servlet based web application development in Java: build an appreciation for the essential moving parts and a basic understanding of how they all fit together.

It does not seek to be an exhaustive guide to a topic that is obviously broad and encompassing. That being said, the reader who is just approaching Servlet based web development for the first time should find the series of posts helpful.

Also, at appropriate junctions, I will provide links to resources where more in depth information can be found.

It is also worth pointing out that the reason we have the title as "Servlet based web application in Java", instead of just "web application in Java" is to stress the fact that using Servlet is just one option (albeit currently the most ubiquitous option) to building web applications with Java. It is possible to build web applications without using the Servlet API. Play Framework is an example of a framework that allows exactly that; Vertx is another example.

The posts contained in the series includes:

Introduction to the Servlet API, Web Server and Servlet Container
Introduction to .war Packaging and Maven
Quick Overview of Basic Maven Concepts
How to Create .war Files with Maven Archetypes and Deploy to Tomcat
How to Create .war Files with IntelliJ and Deploy to Tomcat
Introduction to Spring Boot as a Servlet Based Web Application Framework

Hopefully these series of posts help build the basic foundational knowledge of how servlet based web application works.


Saturday, March 21, 2015

Digging Deeper Into Java's HashMap

This post is about Java's HashMap. Even though it is filled under Beginning Java tag, it won't be an introductory post on how to begin using HashMap: its API or the common tasks you can accomplish with it. It would be a peep into how HashMap works, how it is implemented, with the hope that knowing this would provide more insight into what goes on whenever HashMap is being used.

I would wager that HashMap is probably one of the most readily used data structures when programming in Java. The functionality it provides: which is the ability to store stuff in pairs, is a particularly universal data structure need, not unique to Java. This facility goes by other names elsewhere: associative array, symbol table, dictionary or map: which HashMap is: a map implementation that is based on the concept of hashing; and by hashing, what do we mean?

Hashing is basically a transformation process; that takes a value and transform it into another form which is more desirable (due to shorter length, easy to remember etc) but still can be used to represents the original value.


Wednesday, December 17, 2014

Hello Logging, Hello SLF4J

This is not a full blown guide, tutorial or introduction to logging in Java with the SLF4J abstraction, but a post that captures some of the nuances and components I personally had to get familiar with when I stopped seeing logging as an activity that happens only during development, to something that is actively build into software so as to have an insight into its state in production.

Personally...In the early days it was var_dump() and alert("It works"). Then I discovered the console…(thanks to Firebug and much later, Chrome DevTool) and I dropped alert in favor of console.log() And that was it... This was what "logging" was mainly about: A process of ascertaining the correctness of the software as we develop…

But as soon as I moved into building software systems on the JVM using Java, production logging became more upfront and also, the need for me, as a software developer, to proactively build logging into my application. It was not a complex switch, but looking back now, I realized that I unintentionally, for a while, approached writing production logging as if it were the ones that took place while development.

They are in no way the same...

First, there are more moving parts, second, the purpose differs: Including the ability for an application to write out log messages in production is to have insight into the internal state of the application and also, so as to provide a good source of forensics in case something goes wrong. It has even been estimated that any trivial system would have about 4% of its code base dedicated to logging. This shows how important production logging should be.

This post thus captures the new eyes with which I had to view production logging compared to the type I had long been familiar with; which is logging during development. It is written with logback in mind, which is an implementation of SLF4J, what has grown to be the standard abstraction when it comes to logging in Java

Almost all of what is mentioned would apply to any other SLF4J implementation. The difference that may exist would mostly likely be in the area of configurations...

...with that said, let’s start.


Saturday, July 19, 2014

Date And Time With Java

How do you deal with dates and time in Java and perform common operations like finding difference between two dates or translate dates into a particular string formats?

This post seeks to provide an answer to those questions.

I would start off by giving a brief description of the available date related classes and packages you might encounter on the Java platform and then do follow up posts exploring some of the common date/time operations like how to format date-time, dealing with time zones and locale, finding differences between dates/times etc.


Sunday, March 23, 2014

The Difference between ServletContext and ServletConfig

The servletContext is used to provide configuration that would be available to all servlets in a web application, while servletConfig is used to provide configuration per servlet level.


Specifying and retrieving servletContext configurations.

To specify servletContext configuration, you do so in your web.xml via the <context-param/> tag. For example:

<context-param>
<param-name>timeout</param-name>
<param-value>60</param-value>
</context-param>

and to retrieve these parameters in any of the defined servlets you have:

String value = getServletContext().getInitParameter("timeout");

Specifying and retrieving servletConfig configurations.

Since servletConfig are available per servlet, the definition is within the servlet definition in the web.xml. This is done using the <init-param/> tag.

<servlet>
    <servlet-name>Polling Servlet</servlet-name>
    <servlet-class>com.foo.pollServlet</servlet-class>
    <init-param>  
        <param-name>timeout</param-name> 
        <param-value>60</param-value> 
    </init-param> 
</servlet>

With this, the specified parameters are only available in the com.foo.pollServlet servlet and can be retrieved by:

String value = getServletConfig().getInitParameter("timeout");

Left to me, I would say the not so intuitive choice of words, used to describe these two configuration type, is the cause of confusion most people usually have. I would rather have had the configurations per servlet level called servletConext while configurations that are on the web app level (that are available to all servlet) called appContext or webappContext.

Thursday, October 24, 2013

Understanding Entity Relationship Mapping, Part 2: Annotating Entity Relationships

In Understanding Entity Relationship Mapping, Part 1: The basic concepts I glazed over some of the basic concepts that would be encountered when dealing with establishing relationships between Entities. This post would take a look at how to use Annotations to specify these relationships.

The relationships that would be annotated are the four types that you have in relational data model:

  1. One-To-One annotated with @OneToOne
  2. Many-To-One annotated with @ManyToOne
  3. One-To-Many annotated with @OneToMany
  4. Many-To-Many annotated with @ManyToMany


If concepts like Cardinality, Direction of Relationship, Cascades, Owning side and Inverse side of Entity relationship is new to you, then it is would be good to first read Understanding Entity Relationship Mapping, Part 1: The basic concepts

The relationship would be classified into two: Single Valued Mappings and Collection Valued Mappings. It is worth stating that the examples were written with Hibernate's implementation of JPA in mind, but would work with any other standard implementation of JPA 2, as no Hibernate specific annotation is included.

So let us begin.



Monday, September 09, 2013

Enum in Java.

Let us assume you have the need for some constants in your application; say the three primary colors: Red, Green, Blue. You could create a class specifically for holding the constants thus:
class PrimaryColorsStatic {
        public static final String RED = "red";
        public static final String GREEN = "green";
        public static final String BLUE = "blue";
}

and any of these can be used: PrimaryColorsStatic.RED or PrimaryColorsStatic.GREEN or PrimaryColorsStatic.BLUE

This would work, but it is very limited. Let say you want to hold, apart from the name, the hex code and rgb code of the color as part of the constant? How would this be done with the mechanism above? Whatever way you find to implement this would feel forced as static variables of classes were not designed to help fulfil such use cases.

Enum in Java provides a more robust mechanism for dealing with constant values in your application.

Enum can be formally defined as a special data type that enables for a variable to be a set of predefined constants.

The basic syntax is simple. The primary color example can be represented as an Enum with the following code:
public enum PrimaryColorsEnum {
     RED, GREEN, BLUE
}
[this means that the file would be saved as PrimaryColorsEnum.java]

and an example of accessing it would be:
public main void() {
    PrimaryColorsEnum RedHolder = PrimaryColorsEnum.RED;
    System.out.println(RedHolder);
}

This would print red to the console as a string.

Just knowing the above syntax and when to use it could get you by with Enum, but you have hardly scratched the surface with just that.

To fully appreciate Enums, let us take a step back and see if we can grok the concept in a more detailed level.