users@glassfish.java.net

Re: Accessing a JMS Resource on a Remote Server

From: rdblaha1 <rd_blaha_at_hotmail.com>
Date: Mon, 14 Jan 2008 11:37:38 -0800 (PST)

Thanks for the reply zoltan,


glassfish wrote:
>
> sorry for not responding sooner, I was busy during the week.
>
No problem.


> I think you're still mixing up the concept of Application Client with
> stand-alone java client.
>

Actually I am trying both Tim's (the AppClient) and your (the stand-alone)
advice and direction. I am trying to accomplish both now that I know there
is a difference and that both may accomplish my purpose. As such I was
trying to follow a separate thread off the main with Tim and pursue the
AppClient that way. I messed up the works on your thread by referring only
to the producer.java code and not how I modified it to try to accomplish
your recommended addition/modifications.

By attempting to accomplish both that knowledge should allow me to better
recommend which direction we should take. I am sorry and didn't mean to
add to the confusion.



> If you still want to write stand alone java clients then you should NOT
> try to execute it with appclient command, 'cause it is designed to execute
> Application Clients. What you should do is simply write a simple java
> console application, like hello world. Then include j2ee libraries in the
> project - you can find the necessary jars in Glassfish_home/lib. Then copy
> the code I wrote in my first reply and paste it in the application, then
> you're done, it should execute properly if the remote server (on which the
> application tries to reach the queue) is up and running. So you simply
> have to execute this program as you would execute a simple hello world.
> without using the appclient command.
>

Again, I apologize for not giving you the right code. The code I pointed
you to (from the Java EE 5 Tutorial) was the main body of code I used, but
modified with your suggested code additions/modifications as shown below
(this code is from the Java EE 5 Tutorial with suggested modifications found
earlier in this thread).

package producersa;

/**
 * The ProducerSA class consists only of a main method,
 * which sends several messages to a queue.
 *
 * Run this program in conjunction with SimpleSynchConsumer.
 * Specify a queue name on the command line when you run the
 * program. By default, the program sends one message. Specify
 * a number after the queue name to send that number of messages.
 */
import javax.jms.*;
import javax.naming.*;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.JMSException;

public class Main {
        private static Queue queue;
        //_at_Resource(mappedName = "jms/Topic")
        private static Topic topic;

        /**
         * Main method.
         *
         * @param args the queue used by the example and,
         * optionally, the number of messages to send
         */

    public static void main(String[] args) throws NamingException {
        // TODO code application logic here
                final int NUM_MSGS;
                Connection connection = null;

                //the following 2 lines are equivalent with setting Java VM parameters
with the same
                //name and value (like -Dorg.omg.CORBA.ORBInitialHost=serverhost)
                System.setProperty( "org.omg.CORBA.ORBInitialHost", "localhost" );
//default is localhost
                System.setProperty( "org.omg.CORBA.ORBInitialPort", "3700" ); //default

        if ((args.length < 1) || (args.length > 2))
                        {
            System.err.println(
                    "Program takes one or two arguments: "
                    + "<dest_type> [<number-of-messages>]");
            System.exit(1);
                        }

        String destType = args[0];
        System.out.println("Destination type is " + destType);

        if (!(destType.equals("queue") || destType.equals("topic")))
                        {
            System.err.println("Argument must be \"queue\" or " +
"\"topic\"");
            System.exit(1);
                        }

        if (args.length == 2)
                        {
            NUM_MSGS = (new Integer(args[1])).intValue();
                        }
                else
                        {
            NUM_MSGS = 1;
                        }

        Destination dest = null;

        try
                        {
            if (destType.equals("queue"))
                                {
                dest = (Destination) queue;
                                }
                        else
                                {
                dest = (Destination) topic;
                                }
                        }
                catch (Exception e)
                        {
            System.err.println("Error setting destination: " +
e.toString());
            e.printStackTrace();
            System.exit(1);
                        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create producer and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
        try {
                Context jndiCtx = new InitialContext();
                ConnectionFactory connectionFactory = (ConnectionFactory)
jndiCtx.lookup("ConnFactJNDIName" );
                //dest = (Destination) jndiCtx.lookup( "MyQueueOrTopicJNDIName" ); //
defined earlier from cmd line

                connection = connectionFactory.createConnection();
                Session session = connection.createSession( false,
Session.AUTO_ACKNOWLEDGE );

            MessageProducer producer = session.createProducer(dest);
            TextMessage message = session.createTextMessage();

            for (int i = 0; i < NUM_MSGS; i++)
                                {
                message.setText("This is message " + (i + 1));
                System.out.println("Sending message: " + message.getText());
                producer.send(message);
                                }
            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            producer.send(session.createMessage());
                        }
                catch (JMSException e)
                        {
            System.err.println("Exception occurred: " + e.toString());
                        }
                finally
                        {
            if (connection != null)
                                {
                try
                                        {
                    connection.close();
                                        }
                                catch (JMSException e)
                                        {}
                                }
                        }
                }
        }


I ran the following:

java -cp
C:\AppTest\lib\javaee.jar;C:\AppTest\lib\appserv-rt.jar;C:\AppTest\lib\appserv-deployment-client.jar;C:\AppTest\lib\a
ppserv-ext.jar;C:\AppTest\lib\appserv-admin.jar;C:\AppTest\lib\imqjmsra.jar
-jar dist\ProducerSA.jar queue 7
Destination type is queue
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(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown
Source)
        at javax.naming.InitialContext.lookup(Unknown Source)
        at producersa.Main.main(Main.java:117)

You stated previously that the jndi.properties in the appserv-rt.jar were
already setup to correctly connect. My jndi.properties in the
appserv-rt.jar has the following active lines:

java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
# Required to add a javax.naming.spi.StateFactory for CosNaming that
# supports dynamic RMI-IIOP.
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl

Obviously I didn't accomplish the goal you believed I should. It appears I
have one or two more steps to finish setup. Thanks again for your help
when you have time to give it. I look forward to your suggestions on what
to do next.

rdb


-- 
View this message in context: http://www.nabble.com/Accessing-a-JMS-Resource-on-a-Remote-Server-tp14580977p14811144.html
Sent from the java.net - glassfish users mailing list archive at Nabble.com.