Most Popular Posts

16/04/2013

Garbage Collection (GC)


Garbage Collection is integral part of the JVM (Java Virtual Machine) and it is used to reclaim memory occupied by the unused objects.

When the application runs, it creates objects. Once the object is created it occupies some memory. When the object is not used/ referenced it can be considered as unused and will remain in the memory until the garbage collector claims the memory occupied by this object.

Objects at first are created and put in the Young Generation space. After some time can be moved (Partial/ Minor Garbage Collection) into the Old Generation space. Once the Young and Old Generation is FULL, Full Garbage Collection is run. 

This operation might impact the flow of the application, as it might take a very long time to complete. During this operation application becomes unresponsive.

To avoid this, we can tune the JVM Heap/ other settings. Different JVMs have different GC policy algorithms which will be discussed in the next posts.



Examples:

If we add the following option to the command line for the JVM:
-verbose:gc
we will get the information about the heap and result of the garbage collection for each collection.

[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]
In this scenario we can see two minor GC and one Full/ Major GC.

  • First numbers stand for the size of live objects before and after the GC.
  • Numbers in parentheses represent memory space usable for java objects (perm/ survivor spaces not included).
  • Secs stands for the amount of time in seconds that was taken to perform the action
[GC [DefNew: 64575K->959K(64576K), 0.0457646 secs] 196016K->133633K(261184K), 0.0459067 secs]
Here the second numbers represent the heap usage reduction (196016K -> 133633K(261184K)



If we want to get even more details on the GC results we can add the following options:

-XX:+PrintGCDetails

If we add:

-XX:+PrintGCTimeStamps

we will get additional information regarding timestamps when the GC occurred  Based on such results we can see how often GC occurs.

09/04/2013

What is - EJB - Enterprise Java Bean


TL;DR

EJB - Enterprise Java Beans is a server-side components written in Java programming language that encapsulate business logic of an application.

Types of EJBs: Session, Entity, Message-Driven




Details:


Properties of EJBs:

  • EJB has distributable and deployable business logic to clients
  • EJB is reusable across application servers
  • EJB executed within container that provides management and control services

EJB Modules contain: 
class files for enterprise beans, and EJB DD (Deployment Descriptors). Such modules are packed as JAR files.


There are three types of EJBs:


  • Session
  • Entity
  • Message-driven (v 2.0)


Session EJBs
Session EJB is an object, non-persistent, that lives only between client and EJB. If the user fails to remove the session EJB, it will be removed after a period of time. In order to share the sessions between the clients, we have two types of session EJBs:

  • Stateful - addressed for particular client
  • Stateless - shared among all the clients

Entity EJBs (deprecated)
Entity EJBs are for persistent objects. These are mainly used for mapping DBs with applications. 
Its persistence is delegated to the EJB container or is managed on its own. 
In EJB 3.0, entity beans were superseded by the Java Persistence API (which was subsequently completely separated to its own spec as of EJB 3.1). Entity Beans have been marked as a candidate forpruning as of Java EE 6 [1][2] and are therefore considered a deprecated technology.

Source: http://en.wikipedia.org/wiki/Entity_Bean

Message-Driven EJBs

Message driven beans collect/ retrieve messages in asynchronous mode. When message arrives it usually triggers particular function. Clients can subscribe to specific topics and be informed upon message arrival.

what is JavaBean

TL;DR


JavaBeans are reusable components for Java.

JavaBeans are classes with objects encapsulated into single object that is a Bean.




Details:


JavaBeans are:
- serializable objects



- have public 0-argument constructor
- allow access to private properties using just getter and setter methods.

08/04/2013

Java vs J2EE vs JEE - Java Enterprise Edition Explained

TL;DR


  • Java - is a programming language
  • JEEJava Enterprise Edition based on Java 1.5+ - is a platform as a collection of technologies based on Java, for developing and deploying enterprise applications. The J2EE platform consists of a set of services, application programming interfaces (APIs), and protocols that provide the functionality for developing  highly available, scalable, secure, modular applications.
  • J2EE - older 'version' of J2EE based on java <1.5
  • JEE = Web Clients (HTML/XML) + Web Components (Servlets/ JSP) + Business Components (EJBs)
  • JEE Components - self-contained functional software assembled into JEE application with its related classes and files, and communicates with other components. In most cases component provides specific function and is part of a larger program and runs within container. Example components:
  • JEE Container - maintains individual components, it is the interface between a component and the low-level platform specific functionality that supports the component. Before a Web, enterprise bean, or application client component can be executed, it must be assembled into a J2EE application and deployed into its container.
  • JEE Modules - one or more JEE components for the same container type and one component DD (Deployment Descriptor) of that type.








Details:

JEE stands for:

"Java Enterprise Edition based on Java 1.5+ - is a platform as a collection of technologies based on Java, for developing and deploying enterprise applications"


JEE consists of many different technologies. Main parts are:

Application Components - Components are self-contained functional software assembled into JEE application with its related classes and files, and communicates with other components. In most cases component provides specific function and is part of a larger program and runs within containerExample components:
  • Client components - Application clients / applets
    • native/ 'fat' applications
    • applets on the user side

Containers - Every kind of component sits within a container and has one container.
Containers also provide a declarative mechanism for configuring applications and components during assembly or deployment through the DD (deployment Descriptors). Inside one container we can have several applications with different DDs. Example containers:
  • Web Containers
    • Web Containers provide runtime support for the user requetsts and produce responses based on the form and variables in the request. Process usually startes the execution of Servlets or JSP.
  • EJB Containers
    • These provide EJB (Enterprise Java Beans) components by automated support for transaction, security, persistence, and life cycle management of deployed components. 
  • Resource managed drivers - resource adapters
    • these are mainly drivers that provide connectivity to an external components. 

Difference between JSP and Servlet


TL;DR


  • Servlet - html in java
  • JSP - java in html



Details:



JSP (Java Server Pages) are pages that contain Java code that is put between HTML content in order to produce specific output. JSP page is compiled into Servlet at runtime, cached and presented for the end-user.

Servlet - is a Java class that produces specific output for the user. By using Java Servlet API, as a part of JEE, it responds to the requests in a specific way.


Why do people use both solutions instead of just one?


JSP can be considered more as a scripting language, hence it's easier/ faster to write and created
dynamic content.
JSP is compiled into Java Servlet (and then cached, but renewed whenever contents change).
JSP is used more as a View component, whereas Servlet is considered as Controller in the MVC (model-view-controller architecture).
Servlets by definition are faster than JSP, and should be used more for processing the data than displaying content.

// from http://www.caucho.com/resin-3.0/servlet/tutorial/helloworld/index.xtp
import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}
An example of a JSP mixing HTML and Java code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<body>
Hello, world! Your IP address is <%= request.getRemoteAddr(); %>!

<%
if (request.getParameter("test") != null) {
%>
  <p><strong>Something happened!</strong></p>
<%
}
%>
</body>
</html>