日期:2014-05-16  浏览次数:20340 次

JSM消息的发送和接收
1.JSM消息的发送
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.Session;

import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
import com.sun.messaging.Queue;
/**
 * JMS发送工具类
 * @author owner
 *
 */
public class JMSUtil {
	/**
	 * 发送Queue消息
	 * 
	 * @param content
	 *            消息传递内容
	 * @param queueName
	 * 	   消息对象传送序列名称.如:MQtest
	 * 
	 */
	public static void send(Object content,String queueName) {
		try {
			ConnectionFactory connectionFactory = null;
			QueueConnection connection = null;
			Session session = null;
			MessageProducer producer = null;
			Message message = null;
			try {
				/** 建立消息服务器连接 */
				connectionFactory = new ConnectionFactory();
				/**服务器地址*/
				String jmsServer = "localhost:8080";
				/**用户名称*/
				String jmsUser = "admin";
				/**密码*/
				String jmsPassword ="admin";

				connectionFactory.setProperty("imqAddressList", jmsServer);
				connectionFactory.setProperty(ConnectionConfiguration.imqDefaultUsername, jmsUser);
				connectionFactory.setProperty(ConnectionConfiguration.imqDefaultPassword,jmsPassword);
				/**创建queue点对点模式连接。topic是发布模式*/
				connection = connectionFactory.createQueueConnection();
				/**通过服务连接创建会话*/
				session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);//AUTO_ACKNOWLEDGE自动提示
				Destination destination = null;
				try {
					/**通过序列名称创建会话目标对象*/
					destination = new Queue(queueName);
				} catch (Throwable throwable) {
					return;
				}
				/**创建消息制造者:用消息制造者来发送消息*/
				producer = session.createProducer(destination);
				/**开始连接消息服务器*/
				connection.start();
				// 字符串类型内容
				if (content instanceof String) {
					/**如果消息传递内容为字符串则通过会话创建文本消息*/
					message = session.createTextMessage(content.toString());
				}
				// hash表型内容
				else if (content instanceof HashMap) {
					/**如果消息传递内容为HashMap则通过会话创建Map消息*/
					HashMap contentMap = (HashMap) content;
					MapMessage mm = session.createMapMessage();
					/**把要发送的map内容转入会话创建的消息Map中*/
					Iterator it = contentMap.keySet().iterator();
					boolean isDifficulty = false;//判断是Map中值是否是对象:true为对象
					while (it.hasNext()) {
						String name = it.next().toString();
						Object o = contentMap.get(name);
						/**该会话创建的map消息中只存储值为String的字符串,如果是对象,则会创建值为object的对象消息*/
						if (o != null && !(o instanceof String)) {
							isDifficulty = true;
							break;
						}
						if (o == null) {
							mm.setString(name, "");
						} else {
							mm.setString(name, o.toString());
						}
					}
					if (isDifficulty) {
						/**如果是Map中存的值为非字符串则创建Object消息*/
						message = session.createObjectMessage((Serializable) content);
					} else {
						message = mm;
					}
				}
				// 其它类型内容
				else {
					/**如果消息内容不是map也不是字符串则直接创建一个Object类型消息*/
					message = session
							.createObjectMessage((Serializable) content);
				}
				/**消息制造者发送消息*/
				producer.send(message);

			}
			// JMS链接例外,记录例外日志,并抛出错误信息
			catch (JMSException ex) {
				ex.printStackTrace();
			} finally {
				try {
					/**关闭连接*/
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		} catch (Throwable ex1) {
			ex1.printStackTrace();
		}
	}


}

2.JMS消息的接收

第一步:创建mq连接工厂的实现。用于设置工厂构建连接地址
package com.test.jms;
import javax.jms.Connection;
import javax.jms.JMSException;

import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;

public class OpenMqConnectionFactory implements javax.jms.ConnectionFactory{
  
  private ConnectionFactory connectionFactory;

  public OpenMqConnectionFactory(String brokerAddress) throws JMSException {
      connectionFactory = new ConnectionFa