Monday, March 12, 2012

Java Synchronization

Synchronization in Java:

Java Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner where only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object, while another thread is in the process of using or updating the object's value.

Synchronization prevents such type of data corruption. Synchronization is best use with the Multi-Threading in Java. Synchronization can be done at two levels:
  • Synchronizing a function
  • Synchronizing a block of code
Please find the sample code below

Synchronizing a function
public synchronized void  Display() 
{
   // related code
}
Synchronizing a block of code inside a function:
 public myFunction ()
{
  synchronized (this)
   {
  // Synchronized code
   }
}

Read more...

Thursday, February 9, 2012

Oracle Weblogic Server CLASSPATH

Setting up CLASSPATH in Weblogic Server:

Oracle WebLogic Server installs the following script that you can use to set the classpath that a server requires
  • WL_HOME/server\bin/setWLSEnv.cmd (on Windows)
  • WL_HOME/server/bin/setWLSEnv.sh (on UNIX)
In addition, WebLogic distribution server includes shell scripts that you can use to start the server. These scripts, which are located in the domain directories under the config directory of your WebLogic Server distribution, automatically set up the CLASSPATH variable in the shell before starting the server.

Read more...

Tuesday, January 24, 2012

Java Spring Framework Dependency Injection Variants

Variants in Java Spring Framework Dependency Injection:

Constructor dependency Injection: Dependencies are provided through the constructors of the component.
Setter dependency injection: Dependencies are provided through the JavaBeanstyle setter methods of the component and are more popular than Constructor dependency injection.

Constructor Dependency Injection Syntax:
public class ConstructorInjection
{
private Dependency dep;
public ConstructorInjection(Dependency dep)
{
this.dep = dep;
}
}

Setter Dependency Injection Syntax
public class SetterInjection
{
private Dependency dep;
public void setMyDependency(Dependency dep)
{
this.dep = dep;
}
}

Read more...

Friday, January 20, 2012

Java Stack and Heap Memory

Java Stack and Heap Memory: 

Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed. In a multi-threaded application each thread will have its own stack but will share the same heap. This is why care should be taken in your code to avoid any concurrent access issues in the heap space. The stack is threadsafe (each thread will have its own stack) but the heap is not threadsafe unless guarded with synchronization through your code. 

A method in stack is re-entrant allowing multiple concurrent invocations that do not interfere with each other. A function is recursive if it calls itself. Given enough stack space, recursive method calls are perfectly valid in Java though it is tough to debug. Recursive functions are useful in removing iterations from many sorts of algorithms. All recursive functions are re-entrant but not all re-entrant functions are recursive. Idempotent methods are methods, which are written in such a way that repeated calls to the same method with the same arguments yield same results. For example clustered EJBs, which are written with idempotent methods, can automatically recover from a server failure as long as it can reach another server.

Read more...

Tuesday, January 10, 2012

STRUTS 2 Interceptors

Interceptors in STRUTS 2

Many of the features provided in the Struts2 framework are implemented using interceptors; examples include exception handling, file uploading, lifecycle callbacks and validation. Interceptors are conceptually the same as servlet filters or the JDKs Proxy class. They provide a way to supply pre-processing and post-processing around the action. Similar to servlet filters,interceptors can be layered and ordered. They have access to the action being executed, as well as all environmental variables and execution 
properties. (Reference: Ian Roughley - Starting Struts 2 Book)

Below are the implementing interceptors mentioned:
  1. Spring Framework  the ActionAutowiringInterceptor interceptor.
  2. Request String and Form Values  the ParametersInterceptor interceptor.
  3. Servlet-based objects  the ServletConfigInterceptor interceptor.
The first two interceptors work independently, with no requirements from the action, but the last interceptor ServletConfigInterceptor is different.It works with the assistance of the following interfaces:
  • SessionAware to provide access to all the session attributes via a Map
  • ServletRequestAware to provide access to the HttpServletRequest object
  • RequestAware to provide access to all the request attributes via a Map
  • ApplicationAware to provide access to all the application attributes via a Map
  • ServletResponseAware to provide access to the HttpServletResponse object
  • ParameterAware to provide access to all the request string and form values attributes via a Map
  • PrincipalAware to provide access to the PrincipleProxy object; this object implements the principle and role methods of the HttpServletRequest object in implementation, but by providing a proxy,allows for implementation independence in the action
  • ServletContextAware to provide access to the ServletContext object

Read more...

Sunday, January 1, 2012

Java Mail API

Java Mail API is used to send and receive emails between applications. To send and receive the emails  SMPT, POP and IMAP protocols are used. The message sending and receiving is done by creating a framework using set of abstract classes in the API. This framework allows the application to create customized cross-platform mail application by having basic knowledge of e-mail. There are methods and classes that are used to access mail folders, message downloading and sending messages along with attachments feature.

Java Mail API is used to create personal mail filter, simple mailing lists and personal mail applications. Java mail also includes the capabilities to add the emailing process to an enterprise application or even to create a full-fledged e-mail client. Many companies in the industry have written new e-mail clients using Java Mail.
  • JavaMail is a set of abstract classes that create a framework for sending, receiving and handling e-mail. 
  • The package that Sun provides contains implementations of IMAP and SMTP, which allow sending and receiving mail. 
  • The framework eases the creation of cross-platform mail application without an in-depth knowledge of e-mail.
  • There are methods and classes that allow access to mail folders, download messages, send messages with attachments and filter mail.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP