这是错误提示:
javax.naming.NameNotFoundException: TopicCF
at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
at javax.naming.InitialContext.lookup(Unknown Source)
at ch02.chat2.Chat.<init>(Chat.java:33)
at ch02.chat2.Chat.ss(Chat.java:155)
at ch02.chat2.Chat.main(Chat.java:144)
这是我的运行页面:<%@ page language="java" import="javax.jms.*" pageEncoding="GBK"%>
<%@ page language="java" import="javax.naming.*"%>
<%@ page language="java" import="org.apache.*"%>
<%@ page import="ch02.chat2.*" %><%
Chat c=new Chat();
c.ss();
%>
javax.naming.NameNotFoundException: TopicCF
at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
at javax.naming.InitialContext.lookup(Unknown Source)
at ch02.chat2.Chat.<init>(Chat.java:33)
at ch02.chat2.Chat.ss(Chat.java:155)
at ch02.chat2.Chat.main(Chat.java:144)
这是我的运行页面:<%@ page language="java" import="javax.jms.*" pageEncoding="GBK"%>
<%@ page language="java" import="javax.naming.*"%>
<%@ page language="java" import="org.apache.*"%>
<%@ page import="ch02.chat2.*" %><%
Chat c=new Chat();
c.ss();
%>
package ch02.chat2;
import java.io.*;
import java.util.Properties;import javax.jms.*;
import javax.naming.*;public class Chat implements javax.jms.MessageListener{ private TopicSession pubSession;
private TopicPublisher publisher;
private TopicConnection connection;
private String username;
public Chat()
{
}
/*用于初始化Chat(聊天)的构造函数 */
public Chat(String topicFactory,String topicName,String username) throws Exception
{
//使用jndi.propertirs文件获得一个jndi连接
Properties env=new Properties();
env.put(Context.SECURITY_PRINCIPAL, "system");
env.put(Context.SECURITY_CREDENTIALS, "manager");
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
InitialContext ctx=new InitialContext(env);
//InitialContext ctx=new InitialContext();
//查找一个JMS连接工厂并创建连接
TopicConnectionFactory conFactory=(TopicConnectionFactory)ctx.lookup(topicFactory);
TopicConnection connection=conFactory.createTopicConnection();
//创建两个JMS会话对象
TopicSession pubSession=connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSession subSession=connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//查找一个JMS主题
Topic chatTopic=(Topic)ctx.lookup(topicName);
//创建一个JMS发布者和订阅者。createSubsession中附加的参数是一个消息
//选择器(null)和noLocal标记的一个真值,它表明这个发布者生产的消息不应被自己消费
TopicPublisher publisher=pubSession.createPublisher(chatTopic);
publisher.setTimeToLive(3600000); //设置有效期为2一小时
TopicSubscriber subcriber=subSession.createSubscriber(chatTopic, null, true);
//设置一个JMS消息倾听器
subcriber.setMessageListener(this);
//初始化Chat应用程序变量
this.connection=connection;
this.publisher=publisher;
this.pubSession=pubSession;
this.username=username;
//起动JMS连接,允许传送消息
connection.start();
}
/*接受来自TopicSubcriber的消息*/
public void onMessage(Message message)
{
TextMessage textMessage=(TextMessage)message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*使用发布者创建并发送消息*/
protected void writeMessage(String text) throws JMSException
{
TextMessage message=pubSession.createTextMessage();
message.setText(text);
message.setStringProperty("username", username);
message.setIntProperty("JMSXGroupSeq", 3);
publisher.publish(message);
System.out.println(message);
}
/*关闭JMS连接*/
public void close() throws JMSException
{
connection.close();
}
/*运行聊天客户端*/
public static void main(String[] args)
{
/*
try
{
//Chat c=new Chat();
//c.writeMessage("helle");
//c.close();
if(args.length!=3)
{
System.out.println("Factory,Topic,or username missing!");
}
else
{
//args[0]=topicFactory,args[1]=topicname,args[2]=username
Chat chat=new Chat(args[0],args[1],args[2]);
//从命令行中读取
BufferedReader commandLine=new java.io.BufferedReader(new InputStreamReader(System.in));
//循环读取,直到键入"exit" 为止
while(true)
{
String s=commandLine.readLine();
if(s.equalsIgnoreCase("exit"))
{
chat.clone();
System.exit(0);
}
else
chat.writeMessage(s);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
*/
Chat c=new Chat();
c.ss();
}
public void ss()
{
//args[0]=topicFactory,args[1]=topicname,args[2]=username
try
{
Chat chat=new Chat("TopicCF","topic1","Fred");
//从命令行中读取
BufferedReader commandLine=new java.io.BufferedReader(new InputStreamReader(System.in));
//循环读取,直到键入"exit" 为止
while(true)
{
String s=commandLine.readLine();
if(s.equalsIgnoreCase("exit"))
{
chat.clone();
System.exit(0);
}
else
chat.writeMessage(s);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}