Sunteți pe pagina 1din 12

Lab 3 : Java messaging service(JMS)

Objective:- this lab session is to implement a

JMS
Prerequisites: I have used the following
software and tools
Java programming Language
Eclipse IDE
Apache ActiveMQ message broker 5.4.3 version
JDK 1.7
JRE 1.8

ActiveMQ
is an open-source, messaging software which

serve as the backbone for an architecture of


distributed applications built upon messaging.
ActiveMQ completely implements Java
Messaging Service 1.1 (JMS).
In order to run a JMS application we need
ActiveMQ instance running on a machine
which will be treated as a JMS server.

STEPS TO CREATE JMS SAMPLE


APPLICATION
1. The first step to create a JMS application is to get

Apache ActiveMQ.
Download apache-activemq-5.4.3 from
http://activemq.apache.org/activemq-543-release
.html
depending on the operating system. Just extract
the compressed file move to apache-activemq5.4.3\bin\ win32 folder and start the server
using activemq.bat or activemq.sh file.
The server should start without any issue. To test
whether the server is running or not, browse
http://localhost:8161.
! Warning message to set environmental variable
for java may be asked

Now open the browser and type the url:

http://localhost:8161/admin/
If it is asking for user name and password
(recent versions are secured with password),
provide admin as username and password.
8)A page looks like the one shown below will
be opened.

2. Create a Java Project in eclipse, name it as


JMSSampleApplication.
3. Create a package structure inside the src
folder. It may be something like
com.mindfire.jms.
4. Create a class inside that package. Name the
class as Sender.
5. Now add the below code to the class.
comments are included to each line.

package com.mindfire.jms;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
public class Sender {
public static void main(String[] args) throws
Exception {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFa
ctory");
env.put(Context.PROVIDER_URL,
"tcp://localhost:61616");

// get the initial context


InitialContext ctx = new InitialContext(env);
// lookup the queue object
Queue queue = (Queue) ctx.lookup("queueSampleQueue");
// lookup the queue connection factory
QueueConnectionFactory connFactory = (QueueConnectionFactory)
ctx.lookup("QueueConnectionFactory");
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();
// create a queue session
QueueSession queueSession =
queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);
// create a queue sender
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// create a simple message to say " How are u my students "
TextMessage message = queueSession.createTextMessage(How are u my students");
// send the message
queueSender.send(message);
System.out.println("sent: " + message.getText());
queueConn.close();
}
}

6.Create another class inside that package.


Name the class as Receiver.
7. Now add the below code to the class.

package com.mindfire.jms;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import
import
import
import
import
import
import

javax.jms.Queue;
javax.jms.Session;
javax.jms.TextMessage;
javax.jms.QueueSession;
javax.jms.QueueReceiver;
javax.jms.QueueConnection;
javax.jms.QueueConnectionFactory;

public class Receiver {


public static void main(String args[]) throws Exception {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
env.put("queue.queueSampleQueue",AnimawQueue");

// get the initial context


InitialContext ctx = new
InitialContext(env);
// lookup the queue object
Queue queue = (Queue)
ctx.lookup("queueSampleQueue");
// lookup the queue connection
factory
QueueConnectionFactory connFactory
= (QueueConnectionFactory)
ctx.lookup("QueueConnectionFactory"
);
// create a queue connection
QueueConnection queueConn =
connFactory.createQueueConnection()
;
// create a queue session
QueueSession queueSession =
queueConn.createQueueSession(fals

// create a queue receiver


QueueReceiver
queueReceiver =
queueSession.createReceiver
(queue);
// start the connection
queueConn.start();
// receive a message
TextMessage message =
(TextMessage)
queueReceiver.receive();
// print the message
System.out.println("received:
" + message.getText());
// close the queue connection
queueConn.close();
}
}

8. Now we need to resolve all the import errors in both the


classes. Inside apache-activemq-5.4.3 folder, we have a
activemq-all-5.4.3.jar file. Add this jar file to the project
build path. Perform the below steps.
i. Right click on JMSSampleApplication project.
ii. Build Path > Configure Build Path
iii. Move to Libraries tab. Click on Add External JARs.
iv. Browse to activemq-all-5.4.3.jar file and open.
Hopefully this will resolve all the import issue.
9. First run Sender.java. This will send Hello message to the
queue.
10. Then run Receiver.java. This will receive the Hello
message from the queue. If the following error happen please add
rt.jar file manually under buildpath link after right clicking on your project

Access restriction: Is not accessible due to restriction on


required library ..\jre\lib\rt.jar
This is a simple JMS application with synchronous receiver.

S-ar putea să vă placă și