This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes JMS example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JMS example" Watch "JMS example" New topic

JMS example

Alfred Thomas

Joined: Apr 05, 2006
Posts: 9
Hi - Can anyone please tell me step by step, how to set up my pc, Sun application server and program to use JMS. I have been trying to send messages between applications but I keep getting exceptions, mostly class not found. I have been adding JARs from my application server directory in my application CLASSPATH but I keep getting more and more exceptions.
Can someone also explain to me how to set up connection factories etc in the Application server?

bhaswar goswami

Joined: Jul 04, 2005
Posts: 14
Hey listen ,
i am giving u some code that connects to the JBOSS-MQ and publishes
some messages to the queue-D .

If u want to connect to some other Appserver then u need to change
only the initial context factory i.e ( the getProperties()- method in
the code.
to run the code u need to have some jar in the CLASSPATH and run the
Jboss server & then run the Code .

The code is as follows :

public class JMSPublisher {

public JMSPublisher() {
public void testConnection(){
System.out.println("Before getting the proiperties..");
Hashtable properties = getProperties();
System.out.println("************Created the Environmental peopertiers**********");
Context context = null;
QueueConnectionFactory queueFactory =null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender sender = null;

// Get the initial context with given properties
System.out.println("Before creating the Initial Context..");
try {
context = new InitialContext( properties);
} catch (NamingException e) {
System.out.println("Exception while gettin the Context");


//Get the connection factory
try {
queueFactory = (QueueConnectionFactory)context.lookup("ConnectionFactory");
System.out.println("Received the Factory from context..");
} catch (NamingException e) {
System.out.println("Exception while getting the Factory");

try {
queueConnection = queueFactory.createQueueConnection();
System.out.println("Created the Queue Connection ..");
} catch (JMSException e) {
System.out.println("Exception while Creating the QueueConnection");


try {
queueSession = queueConnection.createQueueSession( false, Session.AUTO_ACKNOWLEDGE);
System.out.println("Created the Session On the created connection ..");
} catch (JMSException e) {
System.out.println("Exception while Creating the Session On the created connection ..");
try {
queue = queueSession.createQueue("D");
System.out.println("Created the Queue on the session");
} catch (JMSException e) {
System.out.println("Exception while creating the Queue On session ..");
try {
sender = queueSession.createSender( queue );
System.out.println("the Sender Has been created to the Test Queue");
} catch (JMSException e) {
System.out.println("Exception while creating the Sender");

TextMessage message = null;
int i = 1;
while( i<=2){

try {
message = queueSession.createTextMessage();
message.setText("Message :::" + i ) ;
message.setJMSDeliveryMode( DeliveryMode.PERSISTENT);

sender.send( message );
} catch (JMSException e) {
System.out.println("Exception while creating the Test Message..");
try {
} catch (InterruptedException e) {
// TODO Auto-generated catch block


public Hashtable getProperties(){
Hashtable props = new Hashtable();
props.put(Context.PROVIDER_URL, "");
props.put("", "yes");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
return props;




Bhaswar Goswami

Consider Paul's rocket mass heater.
subject: JMS example
It's not a secret anymore!