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

向websphere mq queue里put和get消息

 
阅读更多

  连接Queue Manager的工具类:

package com.quest.mq;


import java.io.IOException;
import java.util.Hashtable;

import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.pcf.PCFMessage;
import com.ibm.mq.pcf.PCFMessageAgent;

public class MQTool {

	
	public String queueManagerName = "QM";
	public String hostname = "localhost";
	public int port = 1411;
	public String channel = "SYSTEM.DEF.SVRCONN";
	protected Hashtable<String, Object> properties = new Hashtable<String, Object>();

	public MQTool(String queueManagerName, String hostname, int port, String channel){
		this.queueManagerName = queueManagerName;
		this.hostname = hostname;
		this.port = port;
		this.channel = channel;
		try 
		{
			queueManager = new MQQueueManager(queueManagerName, getConnectionProperties());
			agent = new PCFMessageAgent();
			agent.connect(queueManager);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public PCFMessageAgent agent = null;
	public MQQueueManager queueManager = null;
	
	public MQQueueManager getQueueManager() {
		return queueManager;
	}

	public final boolean isNullOrEmpty(String input)
	{
		boolean result = false;
		if(input == null)
		{
			result = true;
		}else if(input.length() == 0)
		{
			result = true;
		}
		return result;
	}

	/**
	 * Construct MQ queue manager connection properties 
	 * @param hostname
	 * @param port
	 * @param channel
	 * @return
	 */
	private Hashtable<String, Object> getConnectionProperties()
	{
		properties.put("hostname", hostname);
		properties.put("port", port);
		properties.put("channel", channel);
		System.out.println("Connection informations: " + properties);
		System.out.println("Connect QM :" + queueManagerName);
	    return properties;
	}
	
	/**
	 * Get access to a command agent, based on a queue manager name
	 * @param queueManagerName the queue manager name
	 * @return
	 */
	public PCFMessageAgent getAgent() 
	{
		return agent;
	}
	
	protected PCFMessage[] pcfInquire(PCFMessage queuePCF) throws MQException, IOException{

		getAgent().connect(queueManager);
		PCFMessage[] responses = getAgent().send(queuePCF);
		getAgent().disconnect();
		
		return responses;
	}
} 

 

 对一条Queue的放入消息,取出消息:

import java.io.IOException;

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;
import com.ibm.mq.MQTopic;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.MQConstants;
import com.quest.mq.MQTool;

public class MQTest {

	/**
	 * @param args
	 * @throws MQException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws MQException, IOException, InterruptedException {
		
		String queueManagerName = "QM1";
		String hostname = "127.0.0.1";
		int port = 1421;
		String channel = "SYSTEM.DEF.SVRCONN";
		MQTool mqTool = new MQTool(queueManagerName, hostname, port, channel);
		MQQueueManager queueManager = mqTool.getQueueManager();

		
		//放入消息
		MQQueue queue1 = queueManager.accessQueue("apple2", CMQC.MQOO_OUTPUT);
		MQMessage message1 = new MQMessage();
		message1.writeString("33333");
		queue1.put(message1);
		
		//不消费消息
		MQQueue queue2 = queueManager.accessQueue("apple2", MQConstants.MQOO_BROWSE | MQConstants.MQOO_INQUIRE);
		MQGetMessageOptions getMsgOption = new MQGetMessageOptions();
		getMsgOption.options = MQConstants.MQGMO_BROWSE_NEXT;
		
		for(int i=0; i< queue2.getCurrentDepth(); i++){
			MQMessage message = new MQMessage();
			queue2.get(message, getMsgOption);
			System.out.println(message.readStringOfCharLength(message.getDataLength()));
		}
		//消费消息
		MQQueue queue3 = queueManager.accessQueue("apple2", CMQC.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_INQUIRE);
		int depth = queue3.getCurrentDepth();
		for(int i=0; i < depth; i++){
			MQMessage message = new MQMessage();
			queue3.get(message);
			System.out.println(message.readStringOfCharLength(message.getDataLength())+ "----");
			System.out.println("===");
		}
	}

}

 

 

分享到:
评论

相关推荐

    WebSphere MQ Queue Manager Clustering

    IBM WebSphere MQ 集群配置和管理官方演示文稿

    Websphere MQ入门教程

    2.2.1 WebSphere MQ和消息排队 31 2.2.2 队列管理器的进程 32 2.3客户机和服务器 33 客户机-服务器环境中的 WebSphere MQ 应用程序 33 2.4触发机制 33 2.4.1触发的概念 33 2.4.2触发类型 34 2.4.3触发的工作原理 35...

    IBM WebSphere MQ 安装包

    IBM WebSphere MQ是IBM业界领先面向消息的中间件产品,也是MQ系列产品的基础和核心,它使不同的应用程序能够以企业级的性能,在广泛的平台上安全而可靠地通讯。

    WebSphere MQ自学笔记

    本人自学IBM的WebSphereMQ自学笔记,内有MQ安装文档,建MQ的例子等。 1、 MQ6.0安装 2 1.1 启动MQ v6.0 安装程序 2 1.2 软件需求检查 2 1.3 WebSphere Eclipse Platform V3.0.1软件安装 2 1.4 网络配置检查 3 1.5 ...

    WebSphereMQ_V7.5.0.2_for_Windows(4-1)

    WebSphere® MQ (也称...IBM 消息中间件MQ以其独特的安全机制、简便快速的编程风格、卓越不凡的稳定性、可扩展性和跨平台性,以及强大的事务处理能力和消息通讯能力,成为业界市场占有率最高的消息中间件产品。

    C# 实现消息的收发IBM WebSphere MQ 消息队列

    一个C#实现IBM WebSphere MQ 消息收发的实例,包含 发送接收等. 使用的时候只需要修改 appconfig 文件的内容即可. 如有问题.请留言

    Websphere MQ入门教程7

    全书共分为3部分共14章,第一部分 WebSphere MQ原理和体系结构,分为两章;第二部分 WebSphere MQ系统管理,分为六章,分别介绍安装、配置、管理、控制命令和问题确定;第三部分 WebSphere MQ应用开发,由五章组成,...

    WebSphereMQ_V7.5.0.2_for_Windows.part2.rar

    WebSphere® MQ (也称...IBM 消息中间件MQ以其独特的安全机制、简便快速的编程风格、卓越不凡的稳定性、可扩展性和跨平台性,以及强大的事务处理能力和消息通讯能力,成为业界市场占有率最高的消息中间件产品。

    非常全面的WebsphereMQ的PDF学习资料

    Websphere MQ Programming Guide,Websphere MQ Using C++,WebSphere MQ Using Java,WEBSPHERE MQ6.0 JAVA编程,WebSphere MQ基础教程,IBM WEBSPHERE MQ教程,精通WebSphere MQ,WebSphere MQ开发快速入门,IBM ...

    IBM Websphere MQ 教程之备份与恢复

    IBM Websphere MQ 教程之备份与恢复

    WebSphere MQ 开发培训

    WebSphere MQ 开发培训 IBM软件部 软件部 WebSphere MQ 议程 WebSphere MQ 概览 WebSphere MQ 安装配置 WebSphere MQ 应用开发 WebSphere MQ 参考

    IBM Websphere mq安装

    IBM Websphere mq安装IBM Websphere mq安装

    IBM WebSphere MQ 简介和概述

    WebSphere MQ 所能解决的业务问题的种类,并了解 WebSphere MQ 如何能够帮助您满足业务要求。

    IBM WebSphere MQ消息持久化

    描述MQ消息持久化,在队列管理器发生异常时,传送到队列中的信息怎样通过日志将信息恢复到队列中。

    WebSphere MQ

    WebSphere MQ WebSphere MQWebSphere MQ WebSphere MQ

    IBM WebSphere MQ 8.0windows安装包 part2

    IBM WebSphere MQ 8.0windows安装包 part2

    WebSphere MQ工作原理

    消息中间件及WebSphere MQ入门,介绍WebSphere MQ工作原理,消息队列技术,MQ的基本概念,MQ的通讯模式

Global site tag (gtag.js) - Google Analytics