Jms java se client和javax.naming.NoInitialContextException

时间:2013-11-27 05:57:45

标签: java java-ee glassfish jms

我是JMS的新手,这是我第一次尝试使用它。我用glassfish4。我跟着this tutorial我设置了glassfish资源,并在EAR应用程序中添加了消息驱动的bean。另外,我在防火墙中打开了7676端口。之后,我创建了一个SE项目(不同的主机,即不是服务器jvm),具有一个类SeClient和以下     包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.naming.factory.initial at   javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)     在   javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)     在   javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)     在javax.naming.InitialContext.lookup(InitialContext.java:411)at   seclient.SeClient.sendMessage2Topic(SeClient.java:64)at   seclient.SeClient.main(SeClient.java:45)Java结果:1

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

编辑:我在gf中更改了密码 - 配置 - 服务器配置 - JMS主机 - default_JMS_host并添加到代码中

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

但结果相同。

1 个答案:

答案 0 :(得分:1)

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