Recently i had learnt JMS and personally i have to do lot of google to write simple Hello World program. With all my learnings i want to save time of others who want to use JMS.
Basics :
You can read What is JMS ? Artchitecture of JMS ? from any site, some references :
http://en.wikipedia.org/wiki/Java_Message_Service
Also can refer Tutorial pdf in zip JMSHelloWorld.zip
Hope after some reading u are familar with terms of JMS. Let me summarize.
Basically core of JMS is Queues and Topics. I will stick to just queues.
For Queue we have Sender, Receiver who push and pop messages from it.
Overall we will register queue with unique name at JMS server(through command).
Then in our java code we will create ONE sender to send messages in queue AND
ONE receiver which will receive messages from Queue.
We will need one third party JMS Provider(mq: Open Message Queue) which will allow us to register Queues and that will provide us one JMS broker with whom we will connect. JMS Broker will take care of routing messages between queue. JMS broker will maintain messages either on file system or JDBC.
Softwares required :
I have JDK 1.5 installed on my PC and i had downloaded standalone Open Message Queue 4.3 from sun site. https://mq.dev.java.net/
Otherwise u can also download j2eesdk1.4 which is Sun Application Server
and it has same message queue as one component.
Now installing these software is easy just doing next next wizard. I had installed Open Message Queue at D:\software\Sun\MessageQueue.
U will need jms.jar(download from sun site) and imq.jar(after installation of MQ find from there) for Java development. These jars are also part of JMSHelloWorld.zip.
How to use Open Message Queue(MQ) :
For reference and install guide check one of PDF in JMSHelloWorld.zip. After installing we will need to learn few command line utilites of it i.e D:\software\Sun\MessageQueue\mq\bin.
Basically it has imqbrokerd.exe which is JMS Broker mind it. This has to be always RUNNING.
This is default broker which MQ provides and we will use same in our sample program.
So this broker listens on TCP port 7676. So identity of broker is localhost:7676.
Let us register queues in this broker :
Create one file pass.txt with content as :
imq.imqcmd.password=admin
Run this command :
imqcmd create dst -u admin -passfile pass.txt -b localhost:7676 -t q -n TestQueue
Check help of these commands for details or if any error.
So till now we had started our JMS Provider imqbrokerd.exe AND through commands registered one queue TestQueue. Now let us write Java :
JMSSenderWithoutJNDI.java :
QueueConnectionFactory connectionFactory = new QueueConnectionFactory();
/*This line will by default will provide u connection to JMS broker running at ur local machine
if u have ur broker at remote PC then use
connectionFactory.setProperty(ConnectionConfiguration.imqAddressList, "pcnamexyz:7676");
if u have broker over Internet(on webserver) use
connectionFactory.setProperty(ConnectionConfiguration.imqAddressList, "http://domainxyz:8080/imqhttp/tunnel");
*/
QueueConnection conn = connectionFactory.createQueueConnection();
QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
/* NOTE : in same session do not create sender and receiver of same queue
*/
Queue queue = new Queue("TestQueue"); // other way is session.createQueue("TestQueue")
QueueSender sender = session.createSender(queue);
TextMessage textMessage = session.createTextMessage();
textMessage.setText("hello world");
sender.send(textMessage);
It is very simple to understand. We got connectionfactory and then created connection to JMS broker and then created one session and on session u can create sender, receiver. On sender
simple use send method.
JMSReceiverWithoutJNDI.java :
QueueConnection conn = connectionFactory.createQueueConnection();
QueueConnection conn = connectionFactory.createQueueConnection();
QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = new Queue("TestQueue");
QueueReceiver receiver = session.createReceiver(queue);
conn.start(); // VERY critical step otherwise wont receive message
while(true) {
Message message = receiver.receive();
if (message instanceof TextMessage) {
TextMessage msg = (TextMessage) message;
System.out.println("Reading message: " + msg.getText());
} else {
// Means end of messages
break;
}
}
So our sample program is ready for run. Run sender and then run Receiver and see u got message printed at receiver or not. If yes then say thanks to me, if no try to resolve errors
or contact me on yahoo messenger for that go to link on right hand side.
Download JMSHelloWorld.zip which has sample programs, pdf tutorials, jars for reference.
Key Points for a decision whether to use JMS or not :
1. OpenMessageQueue supports peristence storage of queue messages means even if u restart
PC, messages will remain persisted. By default it uses File System to store messages at example
location : D:\software\Sun\MessageQueue\var\mq\instances\imqbroker\fs370.
U can also configure JDBC to store messages of queue.
2. OpenMessageQueue supports sending messages to a queue on local machine, remote machine or through internet. For internet/http they provide one war file imqhttp.war which needs to be deployed in Web Server(like Tomcat) and url of that can be used to send messages in Queue over internet. See pdf tutorials in zip for details.
Gurgaon Jobs, Companies and Consultants
Those who are looking for job in Gurgaon can
visit this site : Gurgaon Jobs. Here you will find email ids/contact no of "Companies in Gurgaon" and also of "Placement Consultants in Gurgaon"
Subscribe to:
Post Comments (Atom)
7 comments:
A. I kept the pass.txt at
C:\software\Sun\MessageQueue\mq\bin location and
B. impbrokerd.exe is at this location- C:\software\Sun\MessageQueue\mq\bin
Then every thing went well
Hi Naresh
Thanks for this. I didn't know anything about JMS. But could run this example within 1 hour, including the installations. So good start of JMS for me. Thank you, I don't think it would have been so fast otherwise.
Cheers
Kamal
Could not install Sun MessageQueue at C:\Program files\.. in windows vista, by googling found that it is an existing bug. so had to install it at other location.
Hi Can You tell me how to use the J2EE server at place of MessageQueue.I have tried OPEN JMS and it is working for me. Actually I want to get the easiest and efficient solution.Please reply.
thanks
Thanks for the program....Will you please post the same by using Oracle AQ db.....
Thanks for the program....Will you please post the same by using Oracle AQ db.....
Post a Comment