1
votes

I am new-bee in JMS and trying to execute my first JMS program using Glassfish application server.

I have created Connection factory [jms/MyQueueFactory] and Destination resource [jms/myQueue] in Glassfish admin console as per following:

Connection factory: jms/MyQueueFactory

Queue: jms/myQueue

Following is my code:

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.naming.InitialContext;

public class MyReceiver 
{
    public static void main(String[] args) 
    {
        try
        {
            InitialContext ctx = new InitialContext();
            QueueConnectionFactory f = (QueueConnectionFactory)ctx.lookup("jms/MyQueueFactory"); **// Getting error here**
            QueueConnection con = f.createQueueConnection();
            con.start();

            QueueSession session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            Queue t = (Queue)ctx.lookup("jms/myQueue");

            QueueReceiver receiver = session.createReceiver(t);

            MyListener listner = new MyListener();
            receiver.setMessageListener(listner);

            System.out.println("Receiver1 is ready, waiting for messages...");  
            System.out.println("press Ctrl+c to shutdown...");

            while(true)
            {
                Thread.sleep(1000);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

But when I try to execute it gives me following error:

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(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.lookup(Unknown Source) at com.test.MyReceiver.main(MyReceiver.java:16)

Please let me know what I am missing here.

Thanks.

1
was your question answered?Thufir
I can say it helped me, I tried to put jndi-properties.jar file in my classpath, and the error was solved but then I start getting classnotfound exception for the Glassfish Jars. I have tried to configure Glassfish in my eclipse, but I could not configure it. But well I will try and let you know if I will get success in that. But thanks for your help. :)Chintan Patel
did you run it it with appclient?Thufir
No, I am working on windows environment and I tried to run directly from eclipse.Chintan Patel

1 Answers

0
votes

Since you are running the application from IDE ,you need a way to connect to the Glassfish server.

For this,you have to set some properties in environment variable or you can also create Properties object

    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    props.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
    props.put(Context.PROVIDER_URL, "http://localhost:4848/");

    InitialContext initialContext;
    try {
        initialContext = new InitialContext(props);

And using this object you can initialize the context.