`
wandejun1012
  • 浏览: 2695092 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

jms例子 聊天室

    博客分类:
  • java
 
阅读更多

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 file
			InitialContext ctx = new InitialContext();
			// Look up a JMS connection factory and create the connection
			TopicConnectionFactory conFactory = (TopicConnectionFactory)ctx.lookup(topicFactory);
			TopicConnection connection = conFactory.createTopicConnection();
			// 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)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 listener
			subscriber.setMessageListener(this);
			// Intialize the Chat application variables
			this.connection = connection;
			this.pubSession = pubSession;
			this.publisher = publisher;
			this.username = username;
			// Start the JMS connection; allows messages to be delivered
			connection.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]=username
				Chat chat = new Chat(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();
					System.exit(0);
			} 
			else
			chat.writeMessage(s);
			}
		} catch (Exception e) { e.printStackTrace(); }
		}
}

=======================================================================
ps:又发现一个JMS好教程的地方,我已经粗略浏览过几篇,讲的不错:
http://www.360doc.com/content/09/0712/20/18042_4241628.shtml
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics