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 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值