Java 与JMS的简单聊天
我正在用JMS制作一个简单的聊天应用程序,但我的代码不起作用,我不知道为什么。这是我的代码,我与JBoss一起使用,因此我打开pub并编写此代码,当我单击启动Eclipse时,会显示以下错误消息:Java 与JMS的简单聊天,java,jms,Java,Jms,我正在用JMS制作一个简单的聊天应用程序,但我的代码不起作用,我不知道为什么。这是我的代码,我与JBoss一起使用,因此我打开pub并编写此代码,当我单击启动Eclipse时,会显示以下错误消息: <pre> Topic or username missingjava.lang.ArrayIndexOutOfBoundsException: 0 at chat.pub.main(pub.java:105) </pre> and this is the code,
<pre>
Topic or username missingjava.lang.ArrayIndexOutOfBoundsException: 0
at chat.pub.main(pub.java:105)
</pre>
and this is the code, what is the problem?
package chat;
import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Properties;
public class pub implements javax.jms.MessageListener{
private TopicSession pubSession;
private TopicSession subSession;
private TopicPublisher publisher;
private TopicConnection connection;
private String username;
/* Constructor. Establish JMS publisher and subscriber */
public pub(String topicName, String username, String password)
throws Exception {
// Obtain a JNDI connection
Properties env = new Properties( );
env.put(Context.SECURITY_PRINCIPAL, "guest");
env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "localhost:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
// ... specify the JNDI properties specific to the vendor
InitialContext jndi = new InitialContext(env);
// Look up a JMS connection factory
TopicConnectionFactory conFactory =
(TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");
// Create a JMS connection
TopicConnection connection =
conFactory.createTopicConnection(username,password);
// Create two JMS session objects
TopicSession pubSession =
connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
TopicSession subSession =
connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
// Look up a JMS topic
Topic chatTopic = (Topic)jndi.lookup(topicName);
// Create a JMS publisher and subscriber
TopicPublisher publisher1 =
pubSession.createPublisher(chatTopic);
TopicSubscriber subscriber =
subSession.createSubscriber(chatTopic);
// Set a JMS message listener
subscriber.setMessageListener(this);
// Intialize the Chat application
set(connection, pubSession, subSession, publisher1, username);
// Start the JMS connection; allows messages to be delivered
connection.start( );
}
/* Initialize the instance variables */
public void set(TopicConnection con, TopicSession pubSess,
TopicSession subSess, TopicPublisher pub,
String username) {
this.connection = con;
this.pubSession = pubSess;
this.subSession = subSess;
this.publisher = pub;
this.username = username;
}
/* Receive message from topic subscriber */
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText( );
System.out.println(text);
} catch (JMSException jmse){ jmse.printStackTrace( ); }
}
/* Create and send message using topic publisher */
protected void writeMessage(String text) throws JMSException {
TextMessage message = pubSession.createTextMessage( );
message.setText(username+" : "+text);
publisher.publish(message);
}
/* Close the JMS connection */
public void close( ) throws JMSException {
connection.close( );
}
/* Run the Chat client */
public static void main(String [] args){
try{
if (args.length!=3)
System.out.println("Topic or username missing");
// args[0]=topicName; args[1]=username; args[2]=password
pub chat = new pub(args[0],args[1],args[2]);
// Read from command line
BufferedReader commandLine = new
java.io.BufferedReader(new InputStreamReader(System.in));
// Loop until the word "exit" is typed
while(true){
String s = commandLine.readLine( );
if (s.equalsIgnoreCase("exit")){
chat.close( ); // close down connection
System.exit(0);// exit program
} else
chat.writeMessage(s);
}
} catch (Exception e){ e.printStackTrace( ); }
}
}
i do this ![enter image description here][1]
and now i get this eroor
![enter image description here][2]
and i dont no what i do not ok i will be happy for help thanks!!
[1]: http://i.stack.imgur.com/c04o1.gif
[2]: http://i.stack.imgur.com/dxCIU.gif
主题或用户名丢失java.lang.ArrayIndexOutOfBoundsException:0
位于chat.pub.main(pub.java:105)
这就是代码,问题是什么?
套餐聊天;
导入javax.jms.*;
导入javax.naming.*;
导入java.io.*;
导入java.io.InputStreamReader;
导入java.util.Properties;
公共类pub实现javax.jms.MessageListener{
非公开专题会议;
私人专题会议分组;
私人主题出版商;
私有主题连接;
私有字符串用户名;
/*建立JMS发布者和订阅者*/
公共发布(字符串主题名、字符串用户名、字符串密码)
抛出异常{
//获取JNDI连接
Properties env=新属性();
环境保护局(环境安全局局长,“客人”);
setProperty(“java.naming.factory.initial”、“org.jnp.interfaces.NamingContextFactory”);
setProperty(“java.naming.provider.url”,“localhost:1099”);
setProperty(“java.naming.factory.url.pkgs”、“org.jboss.naming”);
//…指定特定于供应商的JNDI属性
InitialContext jndi=新的InitialContext(env);
//查找JMS连接工厂
TopicConnectionFactory工厂=
(TopicConnectionFactory)jndi.lookup(“TopicConnectionFactory”);
//创建JMS连接
主题连接=
createTopicConnection(用户名、密码);
//创建两个JMS会话对象
主题会话公共会话=
connection.createTopicSession(false,
会话(自动确认);
主题会话子会话=
connection.createTopicSession(false,
会话(自动确认);
//查找JMS主题
Topic chatTopic=(Topic)jndi.lookup(topicName);
//创建JMS发布服务器和订阅服务器
TopicPublisher publisher1=
createPublisher(聊天主题);
TopicSubscriber订阅方=
subSession.createSubscriber(聊天主题);
//设置JMS消息侦听器
subscriber.setMessageListener(这个);
//初始化聊天应用程序
设置(连接、发布会话、子会话、发布者1、用户名);
//启动JMS连接;允许传递消息
connection.start();
}
/*初始化实例变量*/
公共无效集(TopicConnection con、TopicSession Pubses、,
TopicSession Subss,TopicPublisher pub,
字符串(用户名){
this.connection=con;
this.pubSession=pubses;
this.subSession=subSess;
this.publisher=pub;
this.username=用户名;
}
/*从主题订阅者接收消息*/
消息(消息消息)上的公共无效{
试一试{
text消息text消息=(text消息)消息;
String text=textMessage.getText();
System.out.println(文本);
}catch(jmsceception jmse){jmse.printStackTrace();}
}
/*使用主题发布器创建和发送消息*/
受保护的void writeMessage(字符串文本)引发JMSExException{
TextMessage=pubSession.createTextMessage();
message.setText(用户名+“:”+文本);
publisher.publish(消息);
}
/*关闭JMS连接*/
public void close()引发JMSException{
connection.close();
}
/*运行聊天客户端*/
公共静态void main(字符串[]args){
试一试{
如果(参数长度!=3)
System.out.println(“缺少主题或用户名”);
//args[0]=主题名称;args[1]=用户名;args[2]=密码
pub chat=new pub(args[0],args[1],args[2]);
//从命令行读取
BufferedReader命令行=新建
BufferedReader(新的InputStreamReader(System.in));
//循环,直到键入“退出”一词
while(true){
字符串s=commandLine.readLine();
如果(s.equalsIgnoreCase(“退出”)){
chat.close();//关闭连接
System.exit(0);//退出程序
}否则
聊天.书面信息;
}
}catch(异常e){e.printStackTrace();}
}
}
我这样做![在此处输入图像描述][1]
现在我得到了这个爱欲
![在此处输入图像描述][2]
我不知道我不知道我会很高兴得到帮助谢谢!!
[1]: http://i.stack.imgur.com/c04o1.gif
[2]: http://i.stack.imgur.com/dxCIU.gif
这意味着你启动程序时没有给它参数
然后在第105行
当args为空时,您正在访问args的第0个元素
重新运行程序并根据需要提供参数(3)
编辑:
要在Eclipse中运行提供参数,请执行以下操作:
运行->运行配置->选择发布(因为您已经尝试在eclipse中运行它)->选择参数->在程序参数下指定由空格分隔的参数
欢迎来到StackOverflow。请阅读有关如何提问的说明。另外,更改问题标题。不知道FAQ中是否提到了它,但我只知道
java.lang.ArrayIndexOutOfBoundsException: 0 at chat.pub.main(pub.java:105)
pub chat = new pub(args[0],args[1],args[2]);