1. MyJsp.jsp
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>THIS IS BINOD JSP PAGE<%
System.out.println("THIS IS OUTPUT FROM JSP PAGE");
%></body></html>
2. Stand alone Java Code
callURL.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class CallURL {
public static void main(String[] args) {
callURL();
}
public static void callURL(){
String urlName = "http://localhost:8080/TestServlet/MyJsp.jsp";
URL url;
try {
url = new URL(urlName);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println("Output from Server :: "+line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output from Client
Output from Server :: <html>Output from Server :: <head>Output from Server :: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">Output from Server :: <title>Insert title here</title>Output from Server :: </head>Output from Server :: <body>Output from Server :: THIS IS BINOD JSP PAGEOutput from Server :: Output from Server :: </body>Output from Server :: </html>
Output from Server Console:
21:46:11,453 INFO [STDOUT] THIS IS OUTPUT FROM JSP PAGE
Tuesday, September 15, 2009
Friday, September 11, 2009
Overriding the equals and hashCode methods
Override the equals method when you want to specify the rules of logical equality of objects.
If these methods are not override then :
Person p1 = new Person("Binod",26);
Person p2 = new Person("Binod",26);
p1.equals(p2) -> Will return FALSE. But logically both object are same. Because equals method in Object class only check the references of object NOT containts.
So make the logical equals, Use override equals methods in Person class.
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if (!(obj instanceof Person)) {
return false;
}
Person person = (Person)obj;
return age == person.getAge() && name.equals(person.getName());
}
After adding this method in Person class then
Person p1 = new Person("Binod",26);
Person p2 = new Person("Binod",26);
p1.equals(p2) -> Will return TRUE.
Why override hashcode() method.
When use the hash based collection in your code then only equals method will not work. For example. Now as above code p1 and p2 are logically equal objects. So,
Set set = new HashSet();
set.add(p1);
set.add(p2);
System.out.println("Size of Set :: "+set.size()); Output would be 2, but it should be 1, as both objects are logically same and Set keeps unique data. So, we have to override the hashCode() method.
In Person class, add one more method.
public int hashCode() {
int hashCode = 0;
final int multiplier = 23;
if (hashCode == 0) {
int code = 133;
code = multiplier * code + age;
code = multiplier * code + name.hashCode();
hashCode = code;
}
return hashCode;
}
after compilation check
Set set = new HashSet();
set.add(p1);
set.add(p2);
System.out.println("Size of Set :: "+set.size()); Output would be 1.
During add the p1 object into Set, it will call hashCode() method of p1, now adding p2 object then again it will call hashCode() method of p2. If both hashcode values are equals then it will call object equals() method to final check, if it return true then set will not add second p2 object. If equals() method does not override then it will add second p2 object. If hashcode values are not equal then set would not be call equals() method.
If these methods are not override then :
Person p1 = new Person("Binod",26);
Person p2 = new Person("Binod",26);
p1.equals(p2) -> Will return FALSE. But logically both object are same. Because equals method in Object class only check the references of object NOT containts.
So make the logical equals, Use override equals methods in Person class.
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if (!(obj instanceof Person)) {
return false;
}
Person person = (Person)obj;
return age == person.getAge() && name.equals(person.getName());
}
After adding this method in Person class then
Person p1 = new Person("Binod",26);
Person p2 = new Person("Binod",26);
p1.equals(p2) -> Will return TRUE.
Why override hashcode() method.
When use the hash based collection in your code then only equals method will not work. For example. Now as above code p1 and p2 are logically equal objects. So,
Set set = new HashSet();
set.add(p1);
set.add(p2);
System.out.println("Size of Set :: "+set.size()); Output would be 2, but it should be 1, as both objects are logically same and Set keeps unique data. So, we have to override the hashCode() method.
In Person class, add one more method.
public int hashCode() {
int hashCode = 0;
final int multiplier = 23;
if (hashCode == 0) {
int code = 133;
code = multiplier * code + age;
code = multiplier * code + name.hashCode();
hashCode = code;
}
return hashCode;
}
after compilation check
Set set = new HashSet();
set.add(p1);
set.add(p2);
System.out.println("Size of Set :: "+set.size()); Output would be 1.
During add the p1 object into Set, it will call hashCode() method of p1, now adding p2 object then again it will call hashCode() method of p2. If both hashcode values are equals then it will call object equals() method to final check, if it return true then set will not add second p2 object. If equals() method does not override then it will add second p2 object. If hashcode values are not equal then set would not be call equals() method.
Tuesday, September 8, 2009
Java Data Structure, Java LinkList
public class Node {
protected Object obj;
protected Node node;
}
public class SingleLinkList {
Node linkList;
int count;
public SingleLinkList() {
linkList = null;
count = 0;
}
public void insert(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}else{
Node temp = new Node();
temp.obj = obj;
temp.node = null;
Node last = getLastNode();
last.node = temp;
}
}
public void insertFirst(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}
else{
Node temp = new Node();
temp.obj = obj;
temp.node = linkList;
linkList = temp;
}
}
public void removeFirst(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
System.out.println("Deleting from First :: "+linkList.obj.toString());
linkList = linkList.node;
}
}
public void remove(Object obj){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
if(linkList.obj == obj){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}else{
System.out.println("NOT FOUND");
}
}
else{
Node temp = new Node();
temp = linkList;
Node pre = temp;
while(temp.node!= null&&temp.obj!=obj){
pre = temp;
temp = temp.node;
}
if(temp.obj==obj){
System.out.println("Deleting data :: "+temp.obj.toString());
if(pre==temp){
linkList=temp.node;
}
else{
pre.node = temp.node;
}
}
else{
System.out.println("NOT FOUND");
}
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
// Get node before last node
Node temp = new Node();
temp = linkList;
while(temp.node.node!=null){
temp = temp.node;
}
System.out.println("Deleting data :: "+temp.node.obj.toString());
temp.node = null;
}
}
public Node getLastNode(){
Node temp = new Node();
temp = linkList;
while(temp.node!=null){
temp = temp.node;
}
return temp;
}
public void show(){
Node temp = new Node();
temp = linkList;
while(temp!=null){
System.out.println("Data :: "+temp.obj.toString());
temp = temp.node;
}
}
public static void main(String[] args) {
SingleLinkList singleLinkList = new SingleLinkList();
String str="Binod";
singleLinkList.insert(str);
String str1="MCA";
singleLinkList.insert(str1);
str1="SATYAM";
singleLinkList.insert(str1);
str1="Bangalore";
singleLinkList.insert(str1);
str1="India";
singleLinkList.insertFirst(str1);
str1="SATYAM2";
singleLinkList.insert(str1);
str1="USA";
singleLinkList.insertFirst(str1);
singleLinkList.show();
// singleLinkList.remove();
/*singleLinkList.remove();
singleLinkList.remove();*/
// singleLinkList.removeFirst();
// singleLinkList.show();
singleLinkList.remove("SATYAM2");
singleLinkList.show();
}
}
protected Object obj;
protected Node node;
}
public class SingleLinkList {
Node linkList;
int count;
public SingleLinkList() {
linkList = null;
count = 0;
}
public void insert(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}else{
Node temp = new Node();
temp.obj = obj;
temp.node = null;
Node last = getLastNode();
last.node = temp;
}
}
public void insertFirst(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}
else{
Node temp = new Node();
temp.obj = obj;
temp.node = linkList;
linkList = temp;
}
}
public void removeFirst(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
System.out.println("Deleting from First :: "+linkList.obj.toString());
linkList = linkList.node;
}
}
public void remove(Object obj){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
if(linkList.obj == obj){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}else{
System.out.println("NOT FOUND");
}
}
else{
Node temp = new Node();
temp = linkList;
Node pre = temp;
while(temp.node!= null&&temp.obj!=obj){
pre = temp;
temp = temp.node;
}
if(temp.obj==obj){
System.out.println("Deleting data :: "+temp.obj.toString());
if(pre==temp){
linkList=temp.node;
}
else{
pre.node = temp.node;
}
}
else{
System.out.println("NOT FOUND");
}
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
// Get node before last node
Node temp = new Node();
temp = linkList;
while(temp.node.node!=null){
temp = temp.node;
}
System.out.println("Deleting data :: "+temp.node.obj.toString());
temp.node = null;
}
}
public Node getLastNode(){
Node temp = new Node();
temp = linkList;
while(temp.node!=null){
temp = temp.node;
}
return temp;
}
public void show(){
Node temp = new Node();
temp = linkList;
while(temp!=null){
System.out.println("Data :: "+temp.obj.toString());
temp = temp.node;
}
}
public static void main(String[] args) {
SingleLinkList singleLinkList = new SingleLinkList();
String str="Binod";
singleLinkList.insert(str);
String str1="MCA";
singleLinkList.insert(str1);
str1="SATYAM";
singleLinkList.insert(str1);
str1="Bangalore";
singleLinkList.insert(str1);
str1="India";
singleLinkList.insertFirst(str1);
str1="SATYAM2";
singleLinkList.insert(str1);
str1="USA";
singleLinkList.insertFirst(str1);
singleLinkList.show();
// singleLinkList.remove();
/*singleLinkList.remove();
singleLinkList.remove();*/
// singleLinkList.removeFirst();
// singleLinkList.show();
singleLinkList.remove("SATYAM2");
singleLinkList.show();
}
}
Java Structure, Java Doule LinkList
public class DoubleNode {
DoubleNode pre;
Object obj;
DoubleNode post;
}
public class DoubleLinkList {
DoubleNode linkList;
public DoubleLinkList(){
linkList = null;
}
public void insert(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr=curr.post;
}
temp.pre = curr;
curr.post=temp;
}
}
public void insertFirst(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
temp.post = linkList;
linkList = temp;
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr = curr.post;
}
System.out.println("Deleting Element :: "+curr.obj.toString());
if(curr == linkList){linkList = null;}
else{curr.pre.post = null;}
}
}
public void show(){
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr!=null){
System.out.println("Data :: "+curr.obj.toString());
curr = curr.post;
}
}
public static void main(String[] args) {
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.insert("Binod");
doubleLinkList.insert("Suman");
doubleLinkList.insert("IGNOU");
doubleLinkList.insertFirst("India");
doubleLinkList.insert("SATYAM");
doubleLinkList.show();
doubleLinkList.remove();
doubleLinkList.remove();
doubleLinkList.show();
}
}
DoubleNode pre;
Object obj;
DoubleNode post;
}
public class DoubleLinkList {
DoubleNode linkList;
public DoubleLinkList(){
linkList = null;
}
public void insert(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr=curr.post;
}
temp.pre = curr;
curr.post=temp;
}
}
public void insertFirst(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
temp.post = linkList;
linkList = temp;
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr = curr.post;
}
System.out.println("Deleting Element :: "+curr.obj.toString());
if(curr == linkList){linkList = null;}
else{curr.pre.post = null;}
}
}
public void show(){
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr!=null){
System.out.println("Data :: "+curr.obj.toString());
curr = curr.post;
}
}
public static void main(String[] args) {
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.insert("Binod");
doubleLinkList.insert("Suman");
doubleLinkList.insert("IGNOU");
doubleLinkList.insertFirst("India");
doubleLinkList.insert("SATYAM");
doubleLinkList.show();
doubleLinkList.remove();
doubleLinkList.remove();
doubleLinkList.show();
}
}
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.
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.
Monday, June 15, 2009
JMS Tutorial in 5 minutes
Please visit my another blog:
http://binodsuman.blogspot.com/2009/06/jms-easy-example-get-start-with-jms-jms.html
Regards,
Binod Suman
http://binodsuman.blogspot.com
http://binodsuman.blogspot.com/2009/06/jms-easy-example-get-start-with-jms-jms.html
Regards,
Binod Suman
http://binodsuman.blogspot.com
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)
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)
Subscribe to:
Posts (Atom)