#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    [JMS] Cannot remotely find JNDI resources using InitialContext.


    I've got 2 laptops here, running next to eachother, connected to the same network switch. One is running an almost default instance of glassfish 3x, the other is running a client OUTSIDE of the container comprised of only a few lines of code to get a connectionFactory from the Glassfish server.
    Running both on the same machine works like a charm, but when ran on different machines it refuses to connect, and my head-ache is now getting worse by the minute...

    Server side:
    ============
    Running inside Netbeans IDE 7.2

    GlassFish Server version: 3+
    JMS Service Type: LOCAL (instead of EMBEDDED)
    Created javax.jms.QueueConnectionFactory named "TapServiceConnectionFactory" under JMS Resources.

    Glassfish configuration is otherwise untouched (I've fooled around lots, so created a clean glassfish instance for this example):

    Startup log:

    Code:
    Launching GlassFish on Felix platform
    INFO: Running GlassFish Version: GlassFish Server Open Source Edition 3.1.2.2 (build 5)
    INFO: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
    INFO: Grizzly Framework 1.9.50 started in: 69ms - bound to [0.0.0.0:8080]
    INFO: Grizzly Framework 1.9.50 started in: 21ms - bound to [0.0.0.0:4848]
    INFO: Grizzly Framework 1.9.50 started in: 36ms - bound to [0.0.0.0:8181]
    INFO: Grizzly Framework 1.9.50 started in: 24ms - bound to [0.0.0.0:3700]
    INFO: The Admin Console is already installed, but not yet loaded.
    INFO: GlassFish Server Open Source Edition 3.1.2.2 (5) startup time : Felix (3,007ms), startup services(1,486ms), total(4,493ms)
    INFO: HV000001: Hibernate Validator 4.3.0.Final
    INFO: Grizzly Framework 1.9.50 started in: 12ms - bound to [0.0.0.0:8080]
    INFO: JMS010: ADDRESSLIST in setJmsServiceProvider: mq://localhost:7676/
    INFO: JMS08: JMS Service Connection URL is : mq://localhost:7676/
    INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter: Version: 4.5.2 Patch 1 (Build 3-d) Compile: Thu Jun 7 10:46:15 PDT 2012
    INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter starting: broker is LOCAL, connection mode is TCP
    INFO: Grizzly Framework 1.9.50 started in: 11ms - bound to [0.0.0.0:8181]
    INFO: MQJMSRA_RA1101: GlassFish MQ JMS Resource Adapter Started:LOCAL
    INFO: Created EjbThreadPoolExecutor with thread-core-pool-size 16 thread-max-pool-size 32 thread-keep-alive-seconds 60 thread-queue-capacity 2147483647 allow-core-thread-timeout false
    INFO: Initiating Jersey application, version 'Jersey: 1.11.1 03/31/2012 06:49 PM'
    INFO: REST00001: Listening to REST requests at context: /management/domain
    INFO: The Admin Console is already installed, but not yet loaded.
    INFO: The Admin Console is starting. Please wait.
    INFO: JMX005: JMXStartupService had Started JMXConnector on JMXService URL service:jmx:rmi://localhost:8686/jndi/rmi://localhost:8686/jmxrmi
    INFO: WEB0169: Created HTTP listener [http-listener-1] on host/port [0.0.0.0:8080]
    INFO: WEB0169: Created HTTP listener [http-listener-2] on host/port [0.0.0.0:8181]
    INFO: WEB0169: Created HTTP listener [admin-listener] on host/port [0.0.0.0:4848]
    INFO: WEB0171: Created virtual server [server]
    INFO: WEB0171: Created virtual server [__asadmin]
    INFO: WEB0172: Virtual server [server] loaded default web module []
    INFO: SEC1002: Security Manager is OFF.
    INFO: SEC1010: Entering Security Startup Service
    INFO: SEC1143: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper.
    INFO: SEC1115: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
    INFO: SEC1115: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
    INFO: SEC1115: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
    INFO: SEC1011: Security Service(s) Started Successfully
    INFO: Initializing Mojarra 2.1.6 (SNAPSHOT 20111206) for context ''
    INFO: WEB0671: Loading application [__admingui] at [/]
    INFO: CORE10010: Loading application __admingui done in 8,732 ms
    INFO: The Admin Console application is loaded.
    ClientSide:
    ===========

    Simple maven application containing 1 class with a main-method:

    java Code:
     
    import com.sun.enterprise.naming.SerialInitContextFactory;
    import java.util.Hashtable;
    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.Context;
    import javax.naming.InitialContext;
     
    /**
    * The SimpleClient class sends several messages to a
    * destination.
    */
    public class SimpleClient {
     
    /**
    * Main method.
    */
    public static void main(String[] args) {
     
    Context jndiContext = null;
    ConnectionFactory connectionFactory = null;
    Connection connection = null;
    Session session = null;
    Queue queue = null;
    MessageProducer messageProducer = null;
     
    try
    {
    Hashtable properties = new Hashtable(2);
     
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    properties.put(Context.PROVIDER_URL, "iiop://172.19.1.25:7676"); // vm://localhost:
    properties.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
    properties.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
     
    jndiContext = new InitialContext(properties);
     
    connectionFactory = (ConnectionFactory) jndiContext.lookup("TapServiceConnectionFactory");
     
    // Lookup queue
    // get MessageProducer
    // Send TextMessage
    }
    catch(Exception ex)
    {
    System.out.println("Error: " + ex );
    }
     
    finally
    {
    if(connection !=null)
    {
    try
    {
    connection.close();
    }
    catch(JMSException e)
    {
    System.out.println("Error: " + e );
    }
    }
    }
    }
    }


    POM.XML:

    Code:
    <repositories>
    <repository>
    <id>Java.Net</id>
    <url>http://download.java.net/maven/2/</url>
    </repository>
    </repositories>
    
    <dependencies>
    <dependency>
    <groupId>org.glassfish.extras</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>3.1.1</version>
    <type>jar</type>
    </dependency>
    </dependencies>
    This is what happens if my glassfish server is running and the client is started:

    Code:
    run:
    org.omg.CORBA.COMM_FAILURE: FINE: IOP00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700 vmcid: OMG minor code: 1 completed: No
    at sun.reflect.GeneratedConstructorAccessor27.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:248)
    at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:95)TapServiceConnectionFactory
    at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.handleFullLogging(WrapperGenerator.java:387)
    at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.access$400(WrapperGenerator.java:107)
    at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator$2.invoke(WrapperGenerator.java:511)
    at com.sun.corba.ee.spi.orbutil.proxy.CompositeInvocationHandlerImpl.invoke(CompositeInvocationHandlerImpl.java:99)
    at $Proxy26.connectFailure(Unknown Source)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:257)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:270)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:129)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:223)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:228)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:393)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:130)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.impl.SerialContext$ProviderCacheKey.getNameService(SerialContext.java:1241)
    at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:411)
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at nl.tap.service.client.jupiterclientnextgen.SimpleClient.main(SimpleClient.java:71)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused: connect
    at org.glassfish.enterprise.iiop.impl.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:339)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:242)
    ... 14 more
    Caused by: java.net.ConnectException: Connection refused: connect
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:364)
    at sun.nio.ch.Net.connect(Net.java:356)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:623)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:110)
    at org.glassfish.enterprise.iiop.impl.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:324)
    ... 15 more
    Error: javax.naming.NamingException: Lookup failed for 'TapServiceConnectionFactory' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory, java.naming.provider.url=iiop://172.19.1.25:7676, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory, java.naming.provider.url=iiop://172.19.1.25:7676, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: IOP00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700 vmcid: OMG minor code: 1 completed: No]]
    Also not very sure which port to use, 3700 (JMS) or 7676 (IIOPS), however, I've tried both, but both fail.
    I'm out of ideas :/
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Using:

    Code:
            
    properties.put("org.omg.CORBA.ORBInitialHost", "<IP>");
    properties.put("org.omg.CORBA.ORBInitialPort", "3700");
    ... seemed to have done the trick.

    Complete class:

    Code:
    import java.util.Properties;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    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.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import org.openide.util.lookup.ServiceProvider;
    
    /**
     * Class to send text-messages to a JMS queue.
     *
     * This class will connect to a JMS queue and put messages on the queue
     */
    @ServiceProvider(service = JmsConnector.class)
    public class JmsConnectorImpl implements JmsConnector {
        private String connectionFactoryName = "";
        private String queueName = "";
        private Context jndiContext = null;
        
        // Do not use these directly, use available getters
        private Connection jmsConnectionGlobal = null; 
        private Session jmsSessionGlobal = null; 
        private MessageProducer jmsMessageProducerGlobal = null; 
    
        /**
         * Create a JmsConnector
         *
         * @param url
         * @param connectionFactoryName
         * @param queueName
         */
        public JmsConnectorImpl() {
            createJmsContext();
            this.connectionFactoryName = JmsServiceProperties.getInstance().getJmsConnectionFactoryName();
            this.queueName = JmsServiceProperties.getInstance().getJmsQueueName();
            // TODO: check property values for validity?
        }
    
        /**
         * Creates a context
         *
         * @param host
         * @param port
         */
        private void createJmsContext() {
    
            String contextFactory = JmsServiceProperties.getInstance().getJmsContextFactory();
            String host = JmsServiceProperties.getInstance().getJmsHost();
            String port = JmsServiceProperties.getInstance().getJmsPort();
    
            Properties properties = new Properties();
    
            properties.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
            properties.put("org.omg.CORBA.ORBInitialHost", host);
            properties.put("org.omg.CORBA.ORBInitialPort", port);
    
            try {
                jndiContext = new InitialContext(properties);
            } catch (NamingException ex) {
                Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        
    
        /**
         * Sends a text-message to the queue
         */
        @Override
        public void sendMessage(String message) {
            MessageProducer messageProducer = getMessageProducer();
    
            try {
                TextMessage textMessage = jmsSessionGlobal.createTextMessage(message);
                messageProducer.send(textMessage);
    
                //example for sending some object
                //ObjectMessage message = session.createObjectMessage();
                //MyObj myObj = new MyObj ("Name"); //this class must be serializable 
                //message.setObject(myObj );
                //messageProducer.send(message);
            } catch (Exception ex) {
                System.out.println("Error: " + ex);
            }
    
        }
    
        
        /**
         * Create connection if it is not created yet.
         * 
         * @return The Connection
         */
        private Connection getConnection(){
            if (jmsConnectionGlobal == null){
                try {
                    ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup(connectionFactoryName);
                    jmsConnectionGlobal = connectionFactory.createConnection();
                } catch (JMSException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NamingException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return jmsConnectionGlobal;
        }
        
        /**
         * Create Session if it is not created yet.
         * @return The Session
         */
        private Session getSession() {
            if (jmsSessionGlobal == null) {
                try {
                    Connection connection = getConnection();
                    jmsSessionGlobal = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                } catch (JMSException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                } 
            }
            return jmsSessionGlobal;
        }
    
        /**
         * Create MessageProducer if it is not created yet.
         * @return The MessageProducer
         */
        private MessageProducer getMessageProducer() {
            if (jmsMessageProducerGlobal == null) {
                try {
                    Queue queue = (Queue) jndiContext.lookup(queueName);
                    jmsMessageProducerGlobal = getSession().createProducer(queue);
    
                } catch (JMSException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NamingException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return jmsMessageProducerGlobal;
        }
        
        
        /**
         * Close all resources used by this class
         */
        @Override
        public void close() {
            if(jmsMessageProducerGlobal != null){
                try {
                    jmsMessageProducerGlobal.close();
                } catch (JMSException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if (jmsSessionGlobal != null) {
                try {
                    jmsSessionGlobal.close();
                } catch (JMSException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
            if (jmsConnectionGlobal != null) {
                try {
                    jmsConnectionGlobal.close();
                } catch (JMSException ex) {
                    Logger.getLogger(JmsConnectorImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
        }
    }
    The class uses a property-reader, to read these properties from a properties file:
    Code:
    jms.host=<IP or "localhost">
    jms.port=3700
    jms.contextfactory.fullclassname=com.sun.enterprise.naming.SerialInitContextFactory
    jms.connectionfactory.classname=JmsServiceConnectionFactory
    jms.queue.name=JmsServiceQueue
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Thread closed

IMN logo majestic logo threadwatch logo seochat tools logo