dev@glassfish.java.net

Re: standalone apps & new InitialContext()

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Fri, 03 Nov 2006 18:12:18 +0100

Hello,

>>
>> I'm trying to use GF JNDI namespace repository in standalone
>> application (GFv2 b20).
>> And have 2 problems:
>> 1) Screens of logging text appear (Level.INFO).
>> 2) After program completes execution it just hangs... And
>> System.exit() is only solution, which helps.
>>
>> Are there any thoughts? May be I'm doing something wrong?
>>
>> Here is my code:
>> / env.put("org.omg.CORBA.ORBInitialHost", host);
>> env.put("org.omg.CORBA.ORBInitialPort", String.valueOf(port));
>> InitialContext ic = new InitialContext(env);
>> /
> Are you using JMS resources and have you left any JMS session or
> connection opened at the
> end of the app?

Here is simple app client attached, which hangs after sending a message.
JMS Session and connection were closed.
Is there something wrong in code?

Thanks.
Alexey.



/*
 * JMSTest.java
 */

package test;

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class JMSTest {
    public void process(String host, int port, String factory, String queue) throws Exception {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial",
                "com.sun.enterprise.naming.SerialInitContextFactory");
        props.setProperty("java.naming.factory.url.pkgs",
                "com.sun.enterprise.naming");
        props.setProperty("java.naming.factory.state",
                "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        
        props.put("org.omg.CORBA.ORBInitialHost", host);
        props.put("org.omg.CORBA.ORBInitialPort", String.valueOf(port));
        
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        TextMessage message = null;
        
        try {
            InitialContext ic = new InitialContext(props);
            ConnectionFactory connectionFactory = (ConnectionFactory) ic.lookup(factory);
            Queue destinationQueue = (Queue) ic.lookup(queue);
            
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(destinationQueue);
            
            message = session.createTextMessage("Hello");
            
            producer.send(message);
            
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException ee) {
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException ee) {
                }
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        if (args.length < 4) {
            System.out.println("Run: JMSTest <host> <port> <jms-factory> <jms-queue>");
            System.exit(0);
        }
        String host = args[0];
        int port= Integer.parseInt(args[1]);
        String factory = args[2];
        String queue = args[3];
        
        new JMSTest().process(host, port, factory, queue);
        
        System.out.println("Message sent!");
    }
}