Jms java-se客户端和javax.naming.NoInitialContextException



我是JMS的新手,这是我第一次尝试使用它。我使用glassfish4。我遵循了本教程,设置了glassfish资源,并在EAR应用程序中添加了消息驱动的bean。另外,我在防火墙中打开了7676端口。之后,我创建了一个SE项目(不同的主机,即不是服务器jvm),其中包含一个类SeClient和以下内容package-seclient;

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
public class SeClient {
 public static void main(String a[]) throws Exception {
  // Commands to create Topic
  // asadmin --port 4848 create-jms-resource --restype javax.jms.Topic TestTopic
  // asadmin --port 4848 create-jms-resource --restype javax.jms.TopicConnectionFactory TestTopicConnectionFactory
  String msg = "Hello from remote JMS Client";
  SeClient test = new SeClient();
  System.out.println("==============================");
  System.out.println("Publishig message to Topic");
  System.out.println("==============================");
  System.out.println();
  test.sendMessage2Topic(msg);
  System.out.println();
  System.out.println("==============================");
  System.exit(0);
 }

 private void sendMessage2Topic(String msg) throws Exception{
  // Provide the details of remote JMS Client
  Properties props = new Properties();
  props.put(Context.PROVIDER_URL, "mq://x.x.x.x:7676");//I use my server's IP
  // Create the initial context for remote JMS server
  InitialContext cntxt = new InitialContext(props);
  System.out.println("Context Created");
  // JNDI Lookup for TopicConnectionFactory in remote JMS Provider
  TopicConnectionFactory qFactory = (TopicConnectionFactory)cntxt.lookup("TestTopicConnectionFactory");
  // Create a Connection from TopicConnectionFactory
  Connection connection = qFactory.createConnection();
  System.out.println("Connection established with JMS Provide ");
  // Initialise the communication session
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  // Create the message
  TextMessage message = session.createTextMessage();
  message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
  message.setText(msg);
  // JNDI Lookup for the Topic in remote JMS Provider
  Topic topic = (Topic)cntxt.lookup("TestTopic");
  // Create the MessageProducer for this communication
  // Session on the Topic we have
  MessageProducer mp = session.createProducer(topic);
  // Broadcast the message to Topic
  mp.send(message);
  System.out.println("Message Sent: " + msg);
  // Make sure all the resources are released
  mp.close();
  session.close();
  cntxt.close();
 }
}

但我得到以下错误:

线程"main"javax.naming.NoInitialContextException中出现异常:需要在环境或系统属性中指定类名,或者作为applet参数,或在应用程序资源文件中:java.nameg.factory.initialjavax.nameming.spi.NamingManager.getInitialContext(NamingManager.java:662)在javax.nameing.InitialContext.getDefaultInitCtx(InitialContext.java:307)在javax.nameming.InitialContext.getURLDefaultInitCtx(InitialContext.java:344)位于seclient。SeClient.sendMessage2Topic(SeClient.java:64)位于seclient。SeClient.main(SeClient.java:45)java结果:1

请说明我的代码/操作有什么问题。

EDIT:我在gf-configuration-server config-JMS hosts-default_JMS_host中更改了密码,并添加到代码中

props.put(Context.SECURITY_PRINCIPAL, "admin");
props.put(Context.SECURITY_CREDENTIALS, "xxxxxx");

但结果是一样的。

您需要设置变量CLASSPATH的值:从您的Glassfish lib文件夹和从Glassfish模块文件夹;c: \玻璃鱼\lib*;c: \玻璃鱼\模块*;

相关内容

最新更新