aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes New To JMS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "New To JMS" Watch "New To JMS" New topic
Author

New To JMS

reshmi prasad
Greenhorn

Joined: Nov 08, 2003
Posts: 4
hi all,
when iam trying to run a sample JMS program using weblogic 7.0 it throws classcast exception
Please anybody provide a solution
This is SimpleQueueSender.java
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueSender {
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
final int NUM_MSGS;
if ( (args.length < 1) || (args.length > 2) ) {
System.out.println("Usage: java SimpleQueueSender " +
"<queue-name> [<number-of-messages>]");
System.exit(1);
}
queueName = new String(args[0]);
System.out.println("Queue name is " + queueName);
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}
try {
//////////////////////jndiContext = new InitialContext();
java.util.Properties env = new java.util.Properties();
env.put("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
jndiContext = new InitialContext(env);
} catch (NamingException e) {
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
System.out.println("AFTER LOOKUP");
queue = (Queue) jndiContext.lookup(queueName);
System.out.println("CASTING ............");
} catch (NamingException e) {
System.out.println("JNDI API lookup failed: " +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create sender and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}
/*
* Send a non-text control message indicating end of
* messages.
*/
queueSender.send(queueSession.createMessage());
}
catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
The Error what it throws ......
Exception in thread "main" java.lang.ClassCastException
at SimpleQueueSender.main(SimpleQueueSender.java:71)
Thanks in Advance
Reshmi
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Is it this row that's throwing the exception?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
reshmi prasad
Greenhorn

Joined: Nov 08, 2003
Posts: 4
yes excatly... any solution pls awaiting.....
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. You shouldn't try to lookup the Queue object (what would be the reason for looking up the QueueConnectionFactory if you can lookup the Queue directly...).
Instead, the flow goes something like this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New To JMS