aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "JMS example" Watch "JMS example" New topic
Author

JMS example

Alfred Thomas
Greenhorn

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?

Regards
Alfred
bhaswar goswami
Greenhorn

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() {
super();
}
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");

e.printStackTrace();
}


//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");

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

e.printStackTrace();
}

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 ..");
e.printStackTrace();
}
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 ..");
e.printStackTrace();
}
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");
e.printStackTrace();
}





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..");
e.printStackTrace();
}
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
public Hashtable getProperties(){
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(Context.PROVIDER_URL, "127.0.0.1:1099");
props.put("java.naming.rmi.security.manager", "yes");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
return props;

}


}


Regards,

Bhaswar Goswami
SCJP 5.0 , SCDJWS


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JMS example
 
Similar Threads
How to set up web application when JSTL jars are in Tomcat lib?
JMS Clustering in Weblogic
ICE 287 answers for WebSphere 5.0
JMS conceptual doubt
Websphere 6.1.0.3 trail version --> Default JMS server problemm