下面是我的代码:
import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
public class Sender
{
public static void main(String[] args) throws Exception
{
// get the initial context
InitialContext ctx = new InitialContext();
// lookup the queue object
Queue queue = (Queue) ctx.lookup("queue/queue0");
// lookup the queue connection factory
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
lookup("queue/connectionFactory");
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();
// create a queue session
QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);
// create a queue sender
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// create a simple message to say "Hello"
TextMessage message = queueSession.createTextMessage("Hello");
// send the message
queueSender.send(message);
// print what we did
System.out.println("sent: " + message.getText());
// close the queue connection
queueConn.close();
}
}
Eclipse没有报告上述代码中的任何错误--我能够成功编译。但是,当我尝试运行它时,我得到了以下异常:
Exception in thread "main" 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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at Sender.main(Sender.java:21)
有没有人能帮我修复这个bug?我试着修了几个小时,但还是弄不明白。
发布于 2013-04-03 13:34:15
我们需要指定JNDI的INITIAL_CONTEXT_FACTORY、PROVIDER_URL、用户名、密码等来创建InitialContext
。
在独立应用程序中,您可以按如下方式指定
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ldap.wiz.com:389");
env.put(Context.SECURITY_PRINCIPAL, "joeuser");
env.put(Context.SECURITY_CREDENTIALS, "joepassword");
Context ctx = new InitialContext(env);
但是,如果您在Java容器中运行代码,这些值将由容器获取并用于创建InitialContext
,如下所示
System.getProperty(Context.PROVIDER_URL);
和
这些值将在启动容器时设置为JVM参数。因此,如果您在容器中运行代码,则可以执行以下操作
InitialContext ctx = new InitialContext();
发布于 2015-09-04 17:24:13
如果使用EJB客户端库:
您需要提到获取初始上下文的参数。
InitialContext ctx = new InitialContext();
如果不这样做,它将在项目文件夹中查找属性文件。您还可以将属性、凭据或值包括在类文件本身中,如下所示:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
InitialContext ctx = new InitialContext(props);
URL_PKG_PREFIXES:常量,它保存环境属性的名称,该属性用于指定在URL工厂中加载时要使用的包前缀列表。
EJB客户端库是调用远程EJB组件的主库。
这个库可以通过InitialContext使用。为了调用EJB组件,该库通过URL上下文工厂创建EJB客户端上下文。惟一必要的配置是解析java.naming.factory.url.pkgs属性的值org.jboss.ejb.client.naming以实例化InitialContext。
https://stackoverflow.com/questions/15779510
复制相似问题