`
blaiu
  • 浏览: 128650 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

IBM MQ 简单例子

    博客分类:
  • MQ
阅读更多
package com.founder.gome.bg.service.mq;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;

/**
 * @author HP
 *
 */
public class MQCommon {

	public static String MQ_MANAGER = null;  			//队列管理器名称 
	public static MQQueueManager qMgr = null;
    public static Properties props = null;
	
	public static MQQueueManager getMQQueueManager() {
		if(qMgr==null) {
			try {
				qMgr = new MQQueueManager(MQ_MANAGER);
			} catch (MQException e) {
				e.printStackTrace();
			}
		}
		return qMgr;
	}
	
	public static Properties getProperties() {
		if(props==null) {
			try {
				props = new Properties();
				InputStream ips = new BufferedInputStream(new FileInputStream("D://MQ.properties"));
				props.load(ips);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return props;
	}
	
	public static void mqClose() {
		try {
			if(qMgr!=null) {
				qMgr.close();
				qMgr.disconnect();
				qMgr = null;
			}
		} catch (MQException e) {
			e.printStackTrace();
		}
	}
	
	
}


package com.founder.gome.bg.service.mq;

import com.founder.gome.bg.service.mq.impl.POServiceImpl;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class MQReceiver implements Runnable  {    
	
    private String MQ_QUEUE_NAME = "QL_GOME02_EC_SO_TO_DRAGON_SO";
    private MQQueueManager qMgr = MQCommon.getMQQueueManager();

    public MQReceiver (String mqQueueName) {
		if (null != mqQueueName && !"".equals(mqQueueName)) {
			this.MQ_QUEUE_NAME = mqQueueName;
		}
    }
    
    @SuppressWarnings("unchecked")
	public void run() {   
        int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;   
        MQQueue queue = null;   
        try { 
        	MQEnvironment.hostname = MQCommon.props.getProperty("MQ_HOST_NAME");   
            MQEnvironment.channel = MQCommon.props.getProperty("MQ_CHANNEL");   
            MQEnvironment.port = Integer.valueOf(MQCommon.props.getProperty("MQ_PROT"));   
            MQEnvironment.CCSID = Integer.valueOf(MQCommon.props.getProperty("MQ_CCSID"));
			MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
            queue = qMgr.accessQueue(MQ_QUEUE_NAME, openOptions, null, null,null);   
            int depth = queue.getCurrentDepth();   
            while(depth-- > 0)   
            {   
                MQMessage msg = new MQMessage();
                MQGetMessageOptions gmo = new MQGetMessageOptions();   
                queue.get(msg, gmo);
				int index = msg.getMessageLength();
				byte[] buffer = new byte[index];
				msg.readFully(buffer, 0, index);
				String message = new String(buffer, "utf-8");
				new POServiceImpl().getXML(message, MQ_QUEUE_NAME);
            }   
        } catch (MQException e) {   
            e.printStackTrace();   
        } catch (Exception e) {   
            e.printStackTrace();   
        } finally {
            if(queue!=null){   
                try {   
                    queue.close();   
                } catch (MQException e) {   
                    e.printStackTrace();   
                }   
            }  
        }   
    }
	
}


package com.founder.gome.bg.service.mq;

import java.io.IOException;

import com.founder.gome.bg.service.mq.impl.POServiceImpl;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
  
public class MQSender implements Runnable {   
    
    private String MQ_QUEUE_NAME = "QR_GOME02_EC_SO_TO_DRAGON_SO"; 
    private String xml;
    private MQQueue mqQueue = null;   
    public MQSender(String xml, String mqQueueName) {
		this.xml = xml;
		if (null != mqQueueName && !"".equals(mqQueueName)) {
			this.MQ_QUEUE_NAME = mqQueueName;
		}
	}
    
    @SuppressWarnings("unchecked")
	public void run() {   
        try {   
            MQEnvironment.hostname = MQCommon.props.getProperty("MQ_HOST_NAME");   
            MQEnvironment.channel = MQCommon.props.getProperty("MQ_CHANNEL");   
            MQEnvironment.port = Integer.valueOf(MQCommon.props.getProperty("MQ_PROT"));   
            MQEnvironment.CCSID = Integer.valueOf(MQCommon.props.getProperty("MQ_CCSID"));
            MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
            int sendOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;   
            mqQueue = MQCommon.getMQQueueManager().accessQueue(MQ_QUEUE_NAME, sendOptions, null, null, null);   
            MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();   
            MQMessage mqMessage = new MQMessage(); 
            mqMessage.write(xml.getBytes("utf-8"));   
            mqQueue.put(mqMessage, mqPutMessageOptions);
            POServiceImpl poServiceImpl = new POServiceImpl();
            poServiceImpl.writeFile(xml);
        } catch (MQException e) {   
            e.printStackTrace();   
        } catch (IOException e1) {   
            e1.printStackTrace();   
        } finally {   
            if (mqQueue != null) {   
                try {   
                    mqQueue.close();   
                } catch (MQException e) {   
                    e.printStackTrace();   
                }   
            }   
        }   
    }   
    
    
} 



分享到:
评论
4 楼 djp_java 2015-08-11  
信息中心.平台系统部
3 楼 clamking 2013-08-20  
相关jar包能不能提供下下载链接啊?分享精神值得学习。
2 楼 blaiu 2011-12-06  
很感谢你的share, class MQReveiver有异常,原因是MQEnvironment没有设置完成的时候,先new了一个MQQueueManager。所以,应该将MQQueueManager qMgr = MQCommon.getMQQueueManager()放到MQEnvironment后边
rokily 写道
很感谢你的share, class MQReveiver有异常,原因是MQEnvironment没有设置完成的时候,先new了一个MQQueueManager。所以,应该将MQQueueManager qMgr = MQCommon.getMQQueueManager()放到MQEnvironment后边


多谢 这位朋友指点

1 楼 rokily 2011-09-23  
很感谢你的share, class MQReveiver有异常,原因是MQEnvironment没有设置完成的时候,先new了一个MQQueueManager。所以,应该将MQQueueManager qMgr = MQCommon.getMQQueueManager()放到MQEnvironment后边

相关推荐

Global site tag (gtag.js) - Google Analytics