我有一个像下面这样的课程:
public class Poligon {
public static void main(String[] args) {
try {
Context ctx = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/javaee7/ConnectionFactory");
Destination destination = (Destination) ctx.lookup("jms/javaee7/Topic");
JMSContext context = connectionFactory.createContext();
OrderDTO order = context.createConsumer(destination).receiveBody(OrderDTO.class);
System.out.println("Order received: " + order);
} catch (NamingException ex) {
Logger.getLogger(Poligon.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我想从在 localhost 上运行的服务器(glassfish)中获取 InitialContext(),但出现以下错误:
SEVERE: null
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(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at poligon.Poligon.main(Poligon.java:29)
我知道我必须在 glassfish 上创建 ldap 领域并将以下代码(? - 不知道确切的值)添加到我的类中:
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"?");
env.put(Context.PROVIDER_URL, "?");
env.put(Context.SECURITY_PRINCIPAL, "?");
env.put(Context.SECURITY_CREDENTIALS, "?");
Context ctx = new InitialContext(env);
我的问题是我不知道应该是什么值:
Context.INITIAL_CONTEXT_FACTORY
Context.PROVIDER_URL (I want it on localhost)
Context.SECURITY_PRINCIPAL
Context.SECURITY_CREDENTIALS
而且我不知道应该如何配置 glassfish 服务器?
maven依赖
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.appclient.client</groupId>
<artifactId>gf-client</artifactId>
<version>3.1.2.2</version>
</dependency>