Tuesday, 13 August 2013

Eclipselink Unique Key Constraint Log issue in glassfish server

You can find lot of people asking how to disable eclipselink unique key constraint violation logs in glassfish server, and there are lot of replies where none of them really works. The real solution lies in glassfish server configuration and not in eclipselink configuration. You can find lot of posts referring to eclipselink configuration where we have to add some property tags in persistence.xml file to switch off logs, but that wont stop the logs in glassfish.

<property name="eclipselink.logging.level" value="OFF" />
<property name="eclipselink.logging.parameters" value="false"/>        
<property name="eclipselink.target-server" value="SunAS9"/>
<property name="eclipselink.logging.exceptions" value="false"/>


By the time you catch exception, database constraint voilation would have already written to glassfish server log. So you have to switch off glassfish JTA server logs. But where and how to do it??? This blog entry has the answer for that.

Please follow the below steps :

  1. Log in to glassfish admin console
  2. Go to Configurations->server-config->Logger Settings
       



























 


   

    3. select javax.enterprise.resource.jta option and select SEVERE from log level combobox.

    4. Save and restart the server


Friday, 2 August 2013

Spring security REST Authentication ( apikey or token based authentication )

Spring Security REST Authentication:
     One of the most searched terms on internet. Yes, I have searched a lot to accomplish a successful and well secured RESTful authentication. A lot of articles are available on internet regarding spring security, most of the articles are talking about the advanced technology behind it, but none of them explained the basic workflow of it. After searching for almost 2-3 weeks, Finally I have got a blog link for the entire code to achieve what I want, though, the explanation was not quite understandable. If you can understand just by looking at the code, you can copy paste the below code straight away. Users who prefer to understand the work flow nature of spring security REST authentication, please see the below picture. Its better to explain concepts using pictures rather than writing 1000 of words.

Let me list out the names of all classes we need

1)  RESTAuthenticationFilter.java
2)  RESTAuthenticationToken.java
3)  RESTCredentials.java
4)  UserSecurityServiceImpl.java
5)  UserSecurityService.java
6)  UserSecurityRepository.java
7)  UserSecurityRepositoryImpl.java
8)  RESTUser.java
9)  HMacShaPasswordEncoder.java
10) RESTDaoAuthenticationProvider.java
11) UserNotFoundException.java
12) NoRedirectStrategy.java



Above diagram shows the basic authentication flow of spring security. It all begins with a user who tries to login to a secured system. For example, lets take a simple url ( http://localhost/web-app/secured/payment) We are going to let only users who have the right permission/role to access secured urls. In that URL, "web-app" is the context-name and "/secured" is the relative url prefix mapped to DispatcherServlet.  Whevever a url request hits the server with "/secured" as its relative prefix, DispatcherServlet handles/process the request. Put the below code in web.xml to create url-mapping. 
Web.xml
Before this request is handed over to Spring controller and DispatcherServlet, we have to make sure the user has the rights/authority/permission to access this particular page. To bring authentication in to play, Enable spring security filter in web.xml and map spring security filter to this url-mapping (.../secured/...) in applicationContext-security.xml to intercept the secured url calls. Use the below code
web.xml
applicationContext-security.xml
The above tag applies spring security to all urls begins with "/secured" as their relative path. Now, no one can access secured urls without proper authentication. 
The above are the basic config tags that you must apply to implement any authentication scheme offered by spring. Lets take a look at some of the new bean instance references shown in applicationContext-security.xml (forbiddenEntryPoint and defaultRestAuthenticationFilter). 

forbiddenEntryPoint: When a user tries to access a protected url without proper authentication details, display forbidden message ( httpstatus 405 ).

defaultRestAuthenticationFilter: As shown in the diagram, its the entry point for all url requests. This is the place where all secured url authentication is validated. This filter extracts the token, signatures and salt parameter and validates the authentication details. If the url has all the required authentication details, then it is passed on to the next spring security filter chain and finally to the actual spring controller. In our case its "payment" controller. Take some time to understand the below code, you can find a few new bean references.
applicationContext-security.xml
Authentication Manager processes an authentication request. As shown in the diagram above, Authentication Filter passes an authentication object to Authentication Manager and finally returns a fully populated authentication object(including granted authorities ) if successful. http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/authentication/AuthenticationManager.html. AuthenticationManager is a kind of wrapper class, It can support multiple AuthenticationProviders.

AuthenticationProvider is where the core authentication details validation logic is defined. It has retrieveUser(......) delegate method that takes username and  UsernamePasswordAuthenticationToken as params and returns UserDetails instance which includes GrantedAuthority if successful.

Saturday, 27 July 2013

Java Automated Build Process With Hudson, Nexus, Git and Maven

Automated Build Process



I am going to explain the above picture work flow, then we will look at each technologies in detail.
Validate - validate the project
Test - Test the compiled source code using a suitable unit testing framework. Test code is separated from source code. Its not packaged and deployed with source code.
Package- Take the compiled source code and packaged in to a distributed format, such as       jar/war
Integration-test - process and deploy the package if necessary into an environment where integration tests can be run
Verify- Run and check to verify the package is valid or not
Install - Install the package in to the local repository
Deploy- Done in release environment. Deploy the package in to remote repository for sharing   with developers or other projects.



1. Developer has git installed in his local system. Lets assume that he has completed the task, he issues git commit command to commit his current changes in his local repository.

2. He, then issues git push command to push his changes to the remote repository which is a centralized repository server where the source code version is maintained. Clients connects to this centralized version control server to clone, pull, commit, push their changes. To learn more about git command please visit http://git-scm.com/documentation

3. Hudson(Continuous Integration) server. CI main focus is to notify the users who breaks the build due to erroneous code changes. Its possible that developers sometimes commit and push their changes without proper testing. So, CI connects to our central code repository server and picks the latest changes periodically and verify the changes by running test against them. It can be configured to notify all the developers in case if the build breaks. As shown in the diagram, It connects to the central repository to fetch the latest changes

4. Picks the latest changes and validate, compile, build, package, install in to the local repository or notify developers in case of any failure

5. If build and release phase is configured in the pom.xml file, It performs a release and deploy the new version in nexus repository management server.

6. This step is performed to automate the deployment of new version release in our web application server. In this step, We have to create a script file and configure it to execute after a successful release in pom.xml. This script file main focus is to connect to the nexus repository and download the latest release.

7. Once the download is complete, script file automatically deploy the distribution war in our web application server(glassfish opensource edition).


In java world, if you are a programmer who frequently working with server side applications, then an automated build process is very essential. In this post I will try to explain the work flow of an automated build system that we are currently following in our company. Automated build process of java application become very popular with the release of Maven technology. Without Maven its very tedious to build an automated system. Maven simplified the entire automated build process. Maven has its own life cycle to build an application phase by phase. It starts with


Compile- Compile the source code of the project







pom.xml is the heart of any maven project. Its the file where we can configure the build phases, what to include/exclude in a phase, how to filter unwanted dependencies, and most importantly how to maintain the dependencies. Managing dependencies version is one of the most important aspect of maven technology. In early days, developers has to taken care of the dependency jar file versions manually. But maven simply eliminated that need and provided a framework to handle all dependency files versions automatically. If we put "LATEST" as the version under "<version>" tag, maven automatically fetches the latest version of that dependency from maven central repository. Its that simple.

Sonatype Nexus: Nexus is a repository management standard. It helps the developers to share the artifacts with other projects.

Hudson: Continuous Integration, CI is a technology to automate the build process. It periodically check the code changes in source code repositories with the help of SCM tags and validate, compile, test, build and install the distribution package in local repository. It alerts the developers in case of any build cycle failure.

GIT: GIt is a distributed version control and source code management (SCM) system.

Sunday, 21 July 2013

JVM Tuning

JVM is the heart of any java based applications. Most of the issues we face in our applications and servers are due to incorrect JVM settings and memory leaks. Memory leaks, well, we have no option we have to sit and debug the code. But the former is entirely a different task, Tuning a JVM is a dark art. You need to have complete knowledge about the JVM architecture. Also, tuning a JVM for a specific hardware is another headache. I have struggled a lot to configure our Glassfish server on Ubuntu with 16GB RAM. The default glassfish server JVM settings are not suitable for production use and that required some alteration. Now, after so much struggle, our server is running fine. So I thought this post would be helpful for some people who are struggling like me to configure JVM settings for different application servers. First we will learn about each segments of JVM in detail and in my next post I will show the glassfish jvm settings for production use.






-Xms: This jvm argument request the operating system to allocate the minimum amount of heap memory at start-up. i.e, -Xms1g allocates 1GB as the initial heap memory. It accepts m and g as the units

-Xmx: Set the maximum amount of JVM heap memory. OutOfMemoryException is thrown when memory goes beyond this level due to memory leaks or because of overload condition.


-XX:NewRatio: It defines the old/new generation ratio. In other words, this ratio defines the size of old(tenured) and new(eden) generation space. i.e, 

-XX:NewRatio=2 allocates 2:1 ratio for old to new generation.
If our max heap space is -Xmx=9g, then the calculation goes like this
2 / 3 * 9 = 6GB for old generation
1 / 3 * 9 = 3GB for new generation

-XX:PermSize: Allocates the initial permanent generation non heap size


-XX:MaxPermSize: Allocates the maximum permanent generation non heap size


-XX:SurvivorRatio: This parameter controls the size of two survivor spaces. i.e, -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6, each survivor space will be one-eighth of the young generation.

UseAdaptiveSizePolicy parameter must be disabled, otherwise jvm ignore this parameter. If the size is too small, copying collection overflows directly in to tenured space. If the size is too large, they will be empty.

-server: jvm can be tuned for client or server machine. For example, If you download glassfish app server, by default, it is configured for client machine using -client parameter. But for production, we have to change this parameter to -server for better performance. -client and -server changes the garbage collection algorithms. -client uses Serial collector and -server uses Parallel collector algorithms. We will see GC algorithms in a short while.


How Garbage Collection works?

    All GC algorithms follows the same basic principle to deliver better memory utilization, though their implementation differs, some algorithms favor better throughput and some favor reduced latency. But their core principle is the same. When new instances/objects are created by the application, it is pushed in to Eden/young generation space, from there, algorithm detects whether its a short lived or long lived object, if its a long lived object, it is further moved on to survivor spaces and then on to tenured or old generation.
Finally when full GC occurs, this old object will be cleaned up if the object has no references. Short lived object stays in eden or in survivor spaces and cleaned up when short pause GC occurs.

Saturday, 20 July 2013

Install UML in Netbeans 7.3

Netbeans supported UML in 6.7 release, but in later versions, I couldn't find any of the UML plugins. Anybody knows whats the reason behind that?. Fortunately I figured out a way to integrate UML with later versions of netbeans. I have tried this approach in Netbeans 7.3 and it worked for me.
But, unfortunately, Netbeans 7.3 unable to open the diagrams which I created earlier. I have created the project and a use case diagram successfully, saved it and closed the project, After some time, I reopened the project and tried to open the use case diagram, Nebeans couldn't open the diagram at all. Now I have to fall back to Netbeans 6.9 to continue with UML.
If anyone looking for UML in latest versions of Netbeans, Please follow the below steps. But its useless.

1) Star Netbeans

2) Go to Tools-> Plugins->Settings(tab)
     and click on Add button and enter the below URL     http://dlc.sun.com.edgesuite.net/netbeans/updates/6.9/uc/m1/dev/catalog.xml

then, Click ok

3) Go to Tools-> Plugins-> Available Plugins(tab)
     Search for "UML" in search textfield at the right side corner

4) Select the UML plugin and click on install and restart netbeans


Monday, 15 July 2013

Cron Jobs Schedule Pattern

Cron job patterns:

To run a job at every hour of every day

   0 0 * * * *

To run a job at every 15 seconds

  */15 * * * * *

To run a job at every 30 min

   0 0/30 * * * *

To run a job at 6, 7 and 8 'o clock' of every day

   0 0 6-8 * * *

To run a job at 6, 6:30, 7, 7:30 and 8 'o clock' of every day

   0 0/30 6-8 * * *

To run only on week days between 6 and 8 'o clock'

   0 0 6-8 * * MON-FRI

To run on every christmas day at mid night

   0 0 0 25 12 ?

To run at midnight every day

   0 0 0 * * ?

Saturday, 6 July 2013

Spring Task Execution And Scheduling

If your applications needs a single or batch of tasks to be executed in an asynchronous manner at some point in future, then what you are ultimately looking for is TaskExecuter implementations and TaskScheduler of spring framework.
TaskExecuter allows us to execute tasks in an asynchronous manner
TaskScheduler allows us to schedule a task to run at some point in future.
To know more about taskexecuter and taskscheduler visit http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html

Here we can see how to declare taskexecuter and taskscheduler in spring configuration file
and how to create a bean with cron job @Scheduled annotation to schedule the date and time of execution.

Spring configuration file

Now to schedule a job for every 20 minutes

Make sure to replace xxxxxxxxxxx of <context:component-scan base-package="xxxxxxxxxxx" /> with the package name of your ScheduleService class.

Now, lets analyse the attributes of task executer and scheduler.


<task:scheduler id="taskScheduler" pool-size="1"/>

pool-size="1": Here the value "1" indicates that only one task can be scheduled at a time. If you want one or more task to be scheduled in an asynchronous manner, increase its value. For example, to schedule 5 tasks at a time ( <task:scheduler id="taskScheduler" pool-size="5"/> )

<task:executor id="taskExecutor" pool-size="1" queue-capacity="2"/>

As in the case of scheduler, pool-size with value "1" indicates that only one task can be executed at a time. If you set value 1 to pool-size of scheduler and executer, it behaves in a synchronous manner.ie, only one task can be scheduled and only one task can be executed at a time. If you want to execute more tasks at a time, just increase the value of the pool-size. For example, If you want to execute 5 tasks at a time  
<task:executor id="taskExecutor" pool-size="5" queue-capacity="2"/>

<task:executor id="taskExecutor" pool-size="1" queue-capacity="2"/>

Capacity of TaskExecuter queue. Before an item is pushed to the pool, it checks the current capacity of the pool. If the pool is completely filled and busy, it will be pushed to the queue, and it remains there until the TaskExecuter finish running a task. Here the value "2" defines that only 2 tasks can be put in to the queue before it moves on to the pool.