Tuesday, June 16, 2009

JMS easy example in RAD, Get started with JMS on RAD

This tutorial is based on RAD (Rational Architect Development) that uses the WebShpere Application Server V6.0.

1. start the server and go to admin console
2. Service Integration -> Buses -> New -> Give Name: BinodBus -> Apply -> save -> save
3. click on BinodBus -> In Additional Properties Section, click on Bus Member -> Next -> Finsh -> Save -> save
4. Again click on BinodBus -> In Additional Properties Section, click on Destination -> check Queue Type present or not. If
not present then click on New -> Choose Queue -> Next -> put Identifier QueueDestination -> Next -> Finish -> Save -> Save

5. Open Resources Tree from left panel
6. click on JMS Providers -> Default Messaging -> JMS Connection Factory -> New -> Name -> BinodConnectionProvider -> JNDI
Name -> jms/BinodConnectionProvider -> Bus Name -> BinodBus -> click on Apply -> Save -> Save

7. click on JMS Providers -> Default Messaging -> Go to Right side Destination -> JMS Queue -> New -> Name -> BinodQueue ->
JNDI -> jms/BinodQueue -> Bus Name -> BinodBus -> QueueName -> QueueDestination -> OK -> Save -> Save

8. Restart the server.
9. Create one Dynamic Web Project (JMSSECOND)and Write two servlet to check the simple example

10. Write first servlet (ProducerServlet.java)

import java.io.IOException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProducerServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("******** THIS IS MESSAGE PRODUCER SERVLET **********");
check();
}

public void check(){
System.out.println("********* Producer check **********");
String destName = "jms/BinodQueue";
final int NUM_MSGS = 5;
Context jndiContext = null;

try { jndiContext = new InitialContext(); }
catch (NamingException e) { System.out.println("Could not create JNDI API context: " + e.toString()); System.exit(1);
}

ConnectionFactory connectionFactory = null;
Destination dest = null;

try {
connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/BinodConnectionProvider");
dest = (Destination) jndiContext.lookup(destName); }
catch (Exception e) { System.out.println("JNDI API lookup failed: " + e.toString()); e.printStackTrace(); System.exit(1);
}

Connection connection = null;
MessageProducer producer = null;
try {
connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); producer = session.createProducer(dest);
TextMessage message = session.createTextMessage();

for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message from JMSSECOND DEMO " + (i + 1));
System.out.println("Sending message: " + message.getText());
producer.send(message);
}

producer.send(session.createMessage());
} catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); }
finally { if (connection != null) { try { connection.close(); }
catch (JMSException e) { }
}
}
}
}

11. Write second servlet (ConsumerServlet.java)

import java.io.IOException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConsumerServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("********** MESSAGE CONSUMER SERVLET 2 ************");
check();
}

public void check(){
System.out.println("********* Consumer check **********");
String destName = "jms/BinodQueue";
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination dest = null;
MessageConsumer consumer = null;
TextMessage message = null;
System.out.println("Destination name is " + destName);

try {
jndiContext = new InitialContext();
}catch (NamingException e) { System.out.println("Could not create JNDI API context: " + e.toString()); System.exit(1);
}

try {
connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/BinodConnectionProvider");
dest = (Destination) jndiContext.lookup(destName);
} catch (Exception e) { System.out.println("JNDI API lookup failed: " + e.toString()); System.exit(1);
}

try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(dest);
connection.start();
while (true) {
Message m = consumer.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println("Reading message: " + message.getText()); }
else { break; }
}
}
} catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); }
finally { if (connection != null) { try { connection.close(); }
catch (JMSException e) { }
}
}
}
}

First run Producer Servlet:
http://localhost:9080/JMSSECOND/ProducerServlet
Output:

Sending message: This is message from JMSSECOND DEMO 1
Sending message: This is message from JMSSECOND DEMO 2
Sending message: This is message from JMSSECOND DEMO 3
Sending message: This is message from JMSSECOND DEMO 4
Sending message: This is message from JMSSECOND DEMO 5

Then run Consumer Servlet:
http://localhost:9080/JMSSECOND/ConsumerServlet
Output:


Reading message: This is message from JMSSECOND DEMO 1
Reading message: This is message from JMSSECOND DEMO 2
Reading message: This is message from JMSSECOND DEMO 3
Reading message: This is message from JMSSECOND DEMO 4
Reading message: This is message from JMSSECOND DEMO 5

Please put your comments to give better information in next blog. :)
Source of example.

Thursday, June 11, 2009

what is the difference between JAR, WAR and EAR files

jar - Java archive (file with .jar extension) [For simple java application]
war - Web archive (file with .war extension) [For simple java + jsp/servlet]
ear - Enterprise archive (file with .ear extension) [For simple java + jsp/servlet + EJB]

All three files are zipped file system and used for different purpose.

JAR : JAR is a platform-independent file format that aggregates many files into one. Typically a JAR file contains the class files and auxiliary resources like libraries files, properties file, image, sound etc.

WAR : It is used to deployed in web application like Tomcat. It typically contains servlets, JSPs and their supporting classes and files. A servlet container like Tomcat is required to deploy these file. There are special files and directories within a WAR file. A WAR has a specific directory structure. The top-level directory of a WAR is the document root (WEB-INF) of the application. The document root is where JSP pages, client-side classes and archives, and static Web resources are stored. WEB-INF contains web.xml, classes, lib and Tag library descriptor files.

EAR : An EAR file is a standard JAR file with a .ear extension which is a generally J2EE application. It contains one or more entries representing the modules of the application, and a metadata directory called META-INF which contains one or more deployment descriptors. It is a top-level container which contains modules like: EJB modules, web modules, application client modules etc and deployed to an application server like WebLogic, WebSphere, JBOSS, etc. It might contain WARs, JARs, etc.

JAR -> WAR -> EAR

EAR = WAR(Web module) + JAR(can be EJB module or application client module)

Tuesday, June 9, 2009

How to compare two images, check two image are same or not

You can compare two images using imagemagick.
You can download imagemagick from here. Version must be greater than 6.0. (like ImageMagick-6.4.3-Q16). Suppose you install in "C:\Program Files " folder then you can run command from this location C:\Program Files\ImageMagick-6.4.3-Q16>

After successfully installation you can use this below command
C:\Program Files\ImageMagick-6.4.3-Q16>compare -metric AE 5001.MAIN.jpg 5002.MAIN.jpg difference.jpg
if it returns value 0 means both images are same.

Even you can use -verbose attribute with this command to get more output.
C:\Program Files\ImageMagick-6.4.3-Q16>compare -verbose -metric AE 5001.MAIN.jpg 5002.MAIN.jpg difference.jpg

You can check this command with two different images, two same images.

There are some more command
C:\Program Files\ImageMagick-6.4.3-Q16> compare -compose src rose.jpg reconstruct.jpg difference.png
C:\Program Files\ImageMagick-6.4.3-Q16> compare -verbose -metric mae rose.jpg reconstruct.jpg difference.png
C:\Program Files\ImageMagick-6.4.3-Q16> compare -channel red -metric PSNR rose.jpg reconstruct.jpg difference.png

You can get more info from these link
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/

Tuesday, June 2, 2009

How to get client and server IP address in JSP page

Some time we have to show the server IP address on JSP page and some time we need to store client IP address to next visit purpose.
Using very few lines of code, you can get both server side and client side (browsing) IP address.

GetIPAddress.jsp

<h3> Server Side IP Address </h3><br>
<%@page import="java.net.InetAddress;" %>
<%String ip = "";
InetAddress inetAddress = InetAddress.getLocalHost();
ip = inetAddress.getHostAddress();
out.println("Server Host Name :: "+inetAddress.getHostName());%><br>
<%out.println("Server IP Address :: "+ip);%>

<h3> Client Side IP Address </h3><br>
<%out.print( "Client IP Address :: " + request.getRemoteAddr() ); %><br>
<%out.print( "Client Name Host :: "+ request.getRemoteHost() );%><br>