• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

JNDI setup for MyEclipse and TomCat

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm working to get some JMS code to work locally. Doing a typical message send and message received. My code needs to get the JNDI configured so I can get a look up working. I'm looking to get the JNDI lookup to work however I'm not sure how to setup the JNDI config for MyEclipse and TomCat locally. The exception I'm getting is as follows...

[code]
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at pointToPoint.AsyncReceiver.main(AsyncReceiver.java:22)
[/code]

my code for the listener is as follows..

[code]
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class AsyncReceiver implements MessageListener, ExceptionListener {
public static void main(String[] args) throws Exception {
// get the initial context
InitialContext ctx = new InitialContext();

// lookup the queue object
Queue queue = (Queue) ctx.lookup("queue/queue0");

// lookup the queue connection factory
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx
.lookup("queue/connectionFactory");

// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();

// create a queue session
QueueSession queueSession = queueConn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

// create a queue receiver
QueueReceiver queueReceiver = queueSession.createReceiver(queue);

// set an asynchronous message listener
AsyncReceiver asyncReceiver = new AsyncReceiver();
queueReceiver.setMessageListener(asyncReceiver);

// set an asynchronous exception listener on the connection
queueConn.setExceptionListener(asyncReceiver);

// start the connection
queueConn.start();

// wait for messages
System.out.print("waiting for messages");
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
System.out.print(".");
}

// close the queue connection
queueConn.close();
}

public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
System.out.println("received: " + msg.getText());
} catch (JMSException ex) {
ex.printStackTrace();
}
}
public void onException(JMSException exception) {
System.err.println("an error occurred: " + exception);
}
}
[/code]

TIA

-Dale
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic