问题
I've abandoned GlassFish 4-point-anything in favor of Payara41. Amazingly GF has unresolved JDBC and JMS Resources configuration bugs. See: Glassfish Admin Console throws java.lang.IllegalStateException when creating JDBC Pool
Payara perfectly fixed the JMS configuration issues. So all I need are the environment properties my standalone Java Client needs to get an InitialContext(env) to lookup() those Resources.
Note: InitalContext() doesn't work in a standalone. Only in an EJB Container that can look up the {Payara Home}/glassfish/lib/jndi-properties file. That file has one property so that's what I have in my code below:
Key: "java.naming.factory.initial"
Value: "com.sun.enterprise.naming.impl.SerialInitContextFactory"
That set off a series of NoClassDerfinitionFound Exceptions that led me to add these jars with these classes to my client's buildpath, and to /glassfish/lib/. They are in the order I encountered them.
"glassfish-naming.jar" w/ "com.sun.enterprise.naming.impl.SerialInitContextFactory"
"internal-api-3.1.2.jar" w/ "org.glassfish.internal.api.Globals"
" hk2-api-2.1.46.jar " w/ "org.glassfish.hk2.api.ServiceLocator"
"appserv-rt.jar" from glassfish/lib added to client build path
But now my code throws a java.lang.NoSuchMethodError for Globals.getDefaultHabitat(). Please note, below the Exception doesn't get caught in my catch block. (And I don't see it in Payara's service.log either.)
I know my client finds Globals.class, because adding it caused the NoClassDefinitionFound for ServiceLocator. Are there two "Globals.class" out there ... one w/ and one w/o that method. Or is the "Lorg" in console output really different from "org", i.e. is there a "Lorg/glassfish/hk2/api/ServiceLocator"?
I'm stuck. And this seems such a bread and butter kind of need -- environment properties a standalone Java client needs to get Payara's InitialContext -- it would be nice to be able to add it here for everyone to use (in addition to the jars I've already located.) I'd love to see Payara soar, because I love its Admin Console compared to JBoss and MayFly's XML orientation. Any suggestions? I'm stumped.Code and console output follows:
Code
package org.america3.testclasses;
import java.util.Properties;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.america3.toolkit.U;
public class Test2 implements MessageListener {
static final Properties JNDI_PROPERTIES = new Properties() {
private static final long serialVersionUID = 1L;
{/*This property key:vlaue pair is specified in Payara41/glassfish/lib/jndi-properties*/
/*The class it calls for is in Payara41/glassfish/lib/glassfish-naming.jar*/
this.put ("java.naming.factory.initial","com.sun.enterprise.naming.impl.SerialInitContextFactory");}
};
//constructor
public Test2 () {
String iAmM = U.getIAmMShort(Thread.currentThread().getStackTrace());
System.out.println(iAmM + "beg");
try {
Context jndiContext = (Context) new InitialContext(JNDI_PROPERTIES);
} catch (Exception e) {
System.out.println(" " + iAmM + "InitialContext failed to instantiate");
System.out.println(" " + iAmM + "Exception : " + e.getClass().getName());
System.out.println(" " + iAmM + "e.getMessage(): " + e.getMessage());
System.out.println(" " + iAmM + "e.getMessage(): " + e.getCause());
e.printStackTrace();
}
System.out.println(iAmM + "end");
}
public static void main(String[] args) {
Test2 messageCenter = new Test2 ();
}
public void onMessage(Message arg0) {
// TODO Auto-generated method stub
}
}
Console
Test2.<init> () beg
Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.internal.api.Globals.getDefaultHabitat()Lorg/glassfish/hk2/api/ServiceLocator;
at com.sun.enterprise.naming.impl.SerialInitContextFactory.<init>(SerialInitContextFactory.java:126)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at org.america3.testclasses.Test2.<init>(Test2.java:24)
at org.america3.testclasses.Test2.main(Test2.java:36)
PS: Could someone with enough points add a "Paraya" tag below. I mean with Glassfish's console throwing exceptions when used to configure any JNDI or JMS Resource I think many people will switch.
回答1:
JAR internal-api-3.1.2.jar is for Glassfish v3, and its Globals class has a method getDefaultHabitat() that returns Habitat:
public static Habitat getDefaultHabitat() {
return defaultHabitat;
}
However, Glassfish v4 has changed method signatures, and you have to use new Glassfish v4 internal API whose Globals class has appropriate method getDefaultHabitat() that returns ServiceLocator:
public static ServiceLocator getDefaultHabitat() {
return defaultHabitat;
}
In other words, replace internal-api-3.1.2.jar with internal-api-4.1.jar which can be found on Maven Central here
回答2:
You should add ${PAYARA-HOME}/glassfish/lib/gf-client.jar
to your classpath as this references all the other required jars in it's META-INF/MANIFEST.MF
. Please note, it uses relative path references so you really need to install Payara on the client machine.
来源:https://stackoverflow.com/questions/33114983/what-are-environment-properties-for-payara-initialcontext-from-free-standing-jav