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

Monday, May 27, 2013

Handling Multiple Exceptions in Java 7

Not much introduction in this post, but as most of you know, that was how Java SE used to handle multiple exceptions:
catch (DataFormatException ex) {
     ex.printStackTrace();
     throw ex;
catch (FileNotFoundException ex) {
     ex.printStackTrace();
     throw ex;
}
Now the problem rises when you do not want to write much code specifically to each clause, maybe you just are printing the stack trace or logging the exception. With the Java SE 7, you can write less code and be more flexible, something like this:
catch (DataFormatException|FileNotFoundException ex) {
    ex.printStackTrace();
    throw ex;
}

Tuesday, January 22, 2013

How to resolve 'Could not find the required version of the Java(TM) 2 Runtime Environment'?

I was encountering the 'Could not find the required version of the Java(TM) 2 Runtime Environment' error when I was trying to install JDK 64-bit on my 64-bit Windows 7 machine.

The reason for throwing such error is that JDK installer is written in Java, therefore you need to have JRE installed. I assume that this is a bug in the Java installer somewhat, because after installing JDK EE, you will have a JRE inside that as well, so you end up having two JREs. Surprisingly, if you install JRE 64-bit and set your %JAVA_HOME% and path variables, you will still see that issue remains the same.

Solution is that you should install JRE 32-bit instead of 64 bit version of JRE. If you install it, then you can install JDK EE or/and Glassfish via the installer. This might sound strange but it is true!

Wednesday, January 9, 2013

Create a Dynamic Web Project with Maven in Eclipse Juno

Recently, I began to show my interest to start using Apache Maven in nearly all of my projects. Today, I was  trying to get hold of creating a Dynamic Web Project (Jave EE) project using the M2Eclipse Plugin, which is a fascinating tool to work with, but is tricky as well. While there are other related posts like here, they are outdated and in case you will follow them, you will be exhausted.


1. Prerequisites

You have to have the followings in advance:

  • Eclipse Juno for Java EE developers
  • JDK 1.6 or above
  • Apache Maven 3.0 or above
  • M2E Plugin (You can get this easily by searching it in the search bar of Eclipse Market Place inside the Help  menu of your Eclipse)
  • Apache Tomcat 

2. Create a Maven Project

2.1 Plugin Prerequisites 

You need to have a plugin called M2Eclipse which you can fetch it via Eclipse Marketplace (In case you do not know, just browse to Help and then you can find it there)

Some of you may not know but you also need another plugin called Eclipse WTP plugin. The latter is the plugin that lets you run Web Applications with Built-in servers. Have a look at the plugins you need to have  in advance:


Also note that there is another Maven Eclipse WTP plugin which is not depreciated in favor of the one you can see in the above picture. Everytime you need to install a plugin, you also need to restart your Eclipse.


2.2 Create your project

This step is just simple and easy. First, click on File, then go to New and click on Other... . In there you can search for the word "Maven" and you will get the following options:



Then, simple click on the Next button and in the next dialogue, select maven-archetype-webapp move on:





And the final step is to name your project:



You should by now see the following structure of your project with the new look:


Notice that you have your Deployment Descriptor which enables you to run it on Eclipse's internal server. Open your pom.xml and change it to the following:



  4.0.0
  telecom
  agent
  war
  0.0.1-SNAPSHOT
  agent Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
 javax.servlet
   servlet-api
   2.5
    
  
  
    agent
  

As you can see I just added the servlet-api.jar to the dependencies of the project, when later on we will have to compile our Maven project. Now, I will add a basic Servlet in the package src/main/java/servlet/HelloWorld.java:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException {
     
             response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("The app is up and running!");
  out.close();
  
 }

}


After that, add the servlet information and pattern in the web.xml which you may find under src/main/webapp:

  Archetype Created Web Application
  
      hello
      servlet.HelloWorld
  
  
      hello
      /hello
  



Now it is the time to compile the project. Here you have two options. First is to Right click on your project, then choose Maven and Click on Maven Clean and after that on Maven Install or else, run the following command inside your project directory:

cd /project/directory
mvn clean install

You can have your .war file in your target directory now and you can deploy it in your Apache Tomcat container, however the missing part here is that Eclipse does not know that your project is a web project, therefore you cannot deploy it in your local Apache Tomcat Server (Also your development Server). Every time you code, you need to compile and see the result in your Tomcat. Now we do two more steps so that we can have both the .war file and you will be able to run the application while coding in your tomcat server. First, go to Servers menu which resides in Window and then Show View:


And simply add a Apache Tomcat 7 instance, without changing any settings and click Finish.


3. Run the Project

Now, click on your project in Eclipse's navigation bar and click Refresh. As you can see, the project structure changes. Now you can simple right click on HelloWorld.java and run it on Server, as following:


And that is it! You now have a fully working Dynamic Web Project Maven Project in eclipse!


Remark: In contrast with a lot of tutorials out there, you should not manually change Project Facets of your project, while you are working with Maven projects in Eclipse. The project should read everything from your pom file and change the facets on the fly.

Thursday, July 26, 2012

JMS (with) Spring and Apache ActiveMQ

0. Introduction

If you have worked with Java EE or you are willing to work with it, you probably have heard about Apache Active MQ. Since, for some reason, nearly each and every blog post that I have read on JMS and Spring (Including Spring In Action (Third Edition) and Just Spring (First Edition)) are using ActiveMQ. ActiveMQ is also recommended to be used while developing Java EE applications with Spring Framework.

So let us briefly explore what are advantages and disadvantages of using ApacheMQ.

First question that comes to mind is why it is so popular. apparently, you have variety of options as the following list proposes:
source: Wikipedia, http://en.wikipedia.org/wiki/Java_Message_Service#Provider_implementations


ActiveMQ seems to be quiet a popular option. As Aron Mulder mentioned in SpringSource webinar (Webinar Slides): it is open-source and community-based (as compared to JBoss MQ), its performance in being able to run standalone, or equally inside another process, application server, or Java EE application, its support for nearly everything JMS requires, and lastly its various extensions that integrates well into other products. Apart from what was mentioned in the Webinar, a key benefit of using ActiveMQ with Spring is that, Spring introduced  DefaultMessageListenerContainer object from API 2.0 and with that you only need to define an onMessage method where you can easily enable batch processing.


On disadvantages side, ActiveMQ can decrease performance, if you are not using a connection pool. As JmsTemplate asks for a connection per message and then closes it, therefore can put a huge processing load on the server.