WebStart Client Can't Get Glassfish InitialContext

时间:2015-08-07 01:33:59

标签: java jms jndi java-web-start jnlp

I'm desperate for any suggestions. I've read at least a hundred posts. But I can't find a single one that addresses what I assumed was a trivial problem.

How does a Java Client get an InitialContext from a glassfish server ... when the client is downloaded using WebStart's JNLP?

The Class below extracts just the relevant code from a Java GUI that needs JMS communication with the MessageDrivenBean deployed by Glassfish to handleC persistence.

The Class makes two attempts to get the glassfish InitialContext using two sets of properties:

Try One: InitialContext (envOne). That works, so I know Glassfish has a ConnectionFactory properly configure and named "jms/goConnectionFactory). Unfortunately that set of properties uses a file stored in the Glassfish directory. And that will not be available when the client in downloaded via JNLP.

Try Two: InitialContext (envTwo). That should work for a Glassfish located at any URL, but it fails. And for the life of me I don't know why.

The Class code is pasted below.

It is followed by the console output. (The little helper methods just format console output.)

For reference: I ran StackTraceExample.main() from Eclipse on the development PC. And in this case Glassfish runs on the development PC under "localhost".

Any help at all will be appreciated. Right now I'm dead in the water.

package org.america3.testclasses;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.sun.messaging.ConnectionFactory;

public class StackTraceExample {

  static public void main (String[] args) {
    InitialContext ctx = null;
    ConnectionFactory factory = null;

    //InitialContext environmental properties - set One
    Properties envOne = new Properties ();
    envOne.setProperty ("java.naming.factory.initial", "com.sun.jndi.fscontext.RefFSContextFactory");
    envOne.setProperty ("java.naming.provider.url","file:///C:/glassfish4/mq/opt/java/my_broker");      

    //InitialContext environmental properties - set One
    Properties envTwo = new Properties ();
    envTwo.setProperty(Context.INITIAL_CONTEXT_FACTORY,  "com.sun.enterprise.naming.impl.SerialInitContextFactory");
    envTwo.setProperty(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
    envTwo.setProperty("org.omg.CORBA.ORBInitialPort","3700");
    envTwo.setProperty("org.omg.CORBA.ORBInitialHost","localHost");

    //Get ConnectionFactory - use set One properties
    try {
      p ("TRY ONE: Instantiating new ItialContext");
      ctx = new InitialContext(envOne);
      p("  InitialContext returned: " + ctx.getClass().getName());
      p("  InitialContext returned: " + ctx);
      // look up Connection Factory
      p("calling ctx.lookup()");
      factory = (ConnectionFactory) ctx.lookup("jms/goConnectionFactory");
      p("  ConnectionFactory returned: " + factory.getClass().getName());
      //System.out.println("  ConnectionFactory         : " + factory);
      printFactory(factory);
    } catch (Exception e1) {
      p("Caught exception: " + e1.getClass().getName());
      printST(e1.getStackTrace());
    }

    //Get ConnectionFactory - use set Two properties
    try {
      p ("\nTRY II: Instantiating new ItialContext");
      ctx = new InitialContext(envTwo);
      p ("  InitialContext returned: " + ctx.getClass().getName());
      p ("  InitialContext         : " + ctx);
      // look up Connection Factory
      p ("\ncalling ctx.lookup()");
      factory = (ConnectionFactory) ctx.lookup("jms/goConnectionFactory");
      p ("  ConnectionFactory returned: " + factory.getClass().getName());
      p ("  ConnectionFactory         : " + factory);
    } catch (Exception e2) {
      p ("\nCAUGHT EXCEPTION: " + e2.getClass().getName());
      p ("  MESSAGE       : " + e2.getMessage() + "\n");
      p ("  CAUSE         : " + e2.getCause() + "\n");
      p ("  STACK TRACE   : ");
    printST(e2.getStackTrace());
    }
  }

  static void printST (StackTraceElement[] trace) {
    for (StackTraceElement element : trace) {
      System.out.println(element);
    }
  }

  static void printFactory (ConnectionFactory  factory) {
    if (factory == null) return;
    char c = (char)10;
    System.out.println(factory.toString().replace('{', c).replace(',', c));
  }
}

Here is the console output

TRY ONE: Instantiating new ItialContext
  InitialContext returned: javax.naming.InitialContext
  InitialContext returned: javax.naming.InitialContext@72ea2f77
calling ctx.lookup()
  ConnectionFactory returned: com.sun.messaging.ConnectionFactory
Sun Java System MQ ConnectionFactory
Class:          com.sun.messaging.ConnectionFactory
getVERSION():       3.0
isReadonly():       true
getProperties():    
imqOverrideJMSPriority=false
 imqConsumerFlowLimit=1000
 imqOverrideJMSExpiration=false
 imqAddressListIterations=1
 imqLoadMaxToServerSession=true
 imqConnectionType=TCP
 imqPingInterval=30
 imqSetJMSXUserID=false
 imqConfiguredClientID=
 imqSSLProviderClassname=com.sun.net.ssl.internal.ssl.Provider
 imqJMSDeliveryMode=PERSISTENT
 imqConnectionFlowLimit=1000
 imqConnectionURL=http://localhost/imq/tunnel
 imqBrokerServiceName=
 imqJMSPriority=4
 imqBrokerHostName=localhost
 imqJMSExpiration=0
 imqAckOnProduce=
 imqEnableSharedClientID=false
 imqAckTimeout=0
 imqAckOnAcknowledge=
 imqConsumerFlowThreshold=50
 imqDefaultPassword=guest
 imqQueueBrowserMaxMessagesPerRetrieve=1000
 imqDefaultUsername=guest
 imqReconnectEnabled=false
 imqConnectionFlowCount=100
 imqAddressListBehavior=PRIORITY
 imqReconnectAttempts=0
 imqSetJMSXAppID=false
 imqConnectionHandler=com.sun.messaging.jmq.jmsclient.protocol.tcp.TCPStreamHandler
 imqSetJMSXRcvTimestamp=false
 imqBrokerServicePort=0
 imqDisableSetClientID=false
 imqSetJMSXConsumerTXID=false
 imqOverrideJMSDeliveryMode=false
 imqBrokerHostPort=7676
 imqQueueBrowserRetrieveTimeout=60000
 imqSetJMSXProducerTXID=false
 imqSSLIsHostTrusted=false
 imqConnectionFlowLimitEnabled=false
 imqReconnectInterval=3000
 imqAddressList=
 imqOverrideJMSHeadersToTemporaryDestinations=false}

TRY II: Instantiating new ItialContext
  InitialContext returned: javax.naming.InitialContext
  InitialContext         : javax.naming.InitialContext@72d818d1

calling ctx.lookup()

CAUGHT EXCEPTION: javax.naming.NamingException
  MESSAGE       : Lookup failed for 'jms/goConnectionFactory' in SerialContext[myEnv={org.omg.CORBA.ORBInitial
Port=3700, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, org.omg.CORBA.
ORBInitialHost=localHost, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImp
l, java.naming.factory.url.pkgs=com.sun.enterprise.naming}

  CAUSE         : javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEn
v={org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitCon
textFactory, org.omg.CORBA.ORBInitialHost=localHost, java.naming.factory.state=com.sun.corba.ee.impl.presentat
ion.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is java.
lang.NullPointerException]

  STACK TRACE   : 
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
javax.naming.InitialContext.lookup(Unknown Source)
org.america3.testclasses.StackTraceExample.main(StackTraceExample.java:54)

{NOTE:  THIS BIT OF OUTPUT ORIGINATES ON THE System.err ... from something other than my class.  It shows up whereever it feels like in the normal System.out stream.}

java.lang.NullPointerException
    at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:347)
    at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:354)
    at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:384)
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:329)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:477)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.america3.testclasses.StackTraceExample.main(StackTraceExample.java:54)

0 个答案:

没有答案