首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

jms例证 聊天室

2012-12-28 
jms例子 聊天室refurl:http://wenku.baidu.com/view/f6f580c3d5bbfd0a79567336.htmlhttp://aspnetdb.iteye

jms例子 聊天室

refurl:http://wenku.baidu.com/view/f6f580c3d5bbfd0a79567336.html

http://aspnetdb.iteye.com/blog/1179469

http://junier.iteye.com/blog/453420 --点对点的

注意点:

1、JMS发送是要用到服务器的,要用ActiveMQ或者OpejJMS作为服务器

?

?

下载apache-activemq-5.2.0-bin.zip:http://download.huihoo.com/apache/activemq/20081221-207.html

?

?

其他参考资料:

refurl:http://flyer2010.iteye.com/blog/662047 (含JMS简单小例子)

?

--Oracle官方JMS教程

http://download.oracle.com/otn-pub/jcp/7195-jms-1.1-fr-spec-oth-JSpec/jms-1_1-fr-spec.pdf

?

--还有就是【Java消息服务第二版这本书】

?

http://blog.csdn.net/yutel/article/details/3874439 --此书中提到jms有2种模式,一种是队列,一种是点对点。在我看来,广播群发消息就用队列,即时聊天就用点对点

?

附源码如下:启动时要加3个参数

?

?

package testJms;import java.io.*;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;/* Constructor used to Initialize Chat */public Chat(String topicFactory, String topicName, String username)throws Exception {// Obtain a JNDI connection using the jndi.properties fileInitialContext ctx = new InitialContext();// Look up a JMS connection factory and create the connectionTopicConnectionFactory conFactory = (TopicConnectionFactory)ctx.lookup(topicFactory);TopicConnection connection = conFactory.createTopicConnection();// Create two JMS session objectsTopicSession pubSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);TopicSession subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);// Look up a JMS topicTopic chatTopic = (Topic)ctx.lookup(topicName);// Create a JMS publisher and subscriber. The additional parameters// on the createSubscriber are a message selector (null) and a true// value for the noLocal flag indicating that messages produced from// this publisher should not be consumed by this publisher.TopicPublisher publisher = pubSession.createPublisher(chatTopic);TopicSubscriber subscriber = subSession.createSubscriber(chatTopic, null, true);// Set a JMS message listenersubscriber.setMessageListener(this);// Intialize the Chat application variablesthis.connection = connection;this.pubSession = pubSession;this.publisher = publisher;this.username = username;// Start the JMS connection; allows messages to be deliveredconnection.start();}/* Receive Messages From Topic Subscriber */public void onMessage(Message message) {try {TextMessage textMessage = (TextMessage) message;System.out.println(textMessage.getText());} catch (JMSException jmse){jmse.printStackTrace(); }}/* Create and Send Message Using 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("Factory, Topic, or username missing");// args[0]=topicFactory; args[1]=topicName; args[2]=usernameChat chat = new Chat(args[0],args[1],args[2]);// Read from command lineBufferedReader commandLine = newjava.io.BufferedReader(new InputStreamReader(System.in));// Loop until the word "exit" is typedwhile(true) {String s = commandLine.readLine();if (s.equalsIgnoreCase("exit")){chat.close();System.exit(0);} elsechat.writeMessage(s);}} catch (Exception e) { e.printStackTrace(); }}}

热点排行