分享一段代码,很实用。

下面这段java代码是我在国外一个论坛上发现的,源地址已经忘了。代码的作用是可以删除正在使用的mq的队列消息,管理mq的人一定知道它的美妙了吧,哈哈。

我拿来改了下,增加了2个参数支持:ccsid和channel。上代码:

 import java.util.Hashtable;
import com.ibm.mq.*; /**
* A simply Java class to destructively read (delete) all message on a queue.
*
* @author Roger Lacroix, Capitalware Inc.
* @return 0 for ok or 1 for failed.
* @version 1.0.0
* @license Apache 2 License
*/ /**
* lichmama reedited
* change: add parameters to set ccsid and channel
*/ public class EmptyQ
{
private Hashtable params = null;
public int port = 1414;
public String hostname;
public String channel;
public String qManager;
public String inputQName;
public int ccsid = 1208; /**
* The constructor
*/
public EmptyQ()
{
super();
} /**
* Check if all of the parameters were passed to the class file.
* @return true/false
*/
private boolean allParamsPresent()
{
boolean b = params.containsKey("-s") && params.containsKey("-h") && params.containsKey("-p") && params.containsKey("-c") && params.containsKey("-m") && params.containsKey("-q");
if (b)
{
try
{
port = Integer.parseInt((String) params.get("-p"));
ccsid = Integer.parseInt((String) params.get("-s"));
}
catch (NumberFormatException e)
{
b = false;
}
// Set up MQ environment
hostname = (String) params.get("-h");
channel = (String) params.get("-c");
qManager = (String) params.get("-m");
inputQName = (String) params.get("-q"); }
return b;
} /**
* Initialize MQ environment variables
* @param args
* @throws IllegalArgumentException
*/
private void init(String[] args) throws IllegalArgumentException
{
params = new Hashtable(5);
if (args.length > 0 && (args.length % 2) == 0)
{
for (int i = 0; i < args.length; i += 2)
params.put(args[i], args[i + 1]);
}
else
{
throw new IllegalArgumentException();
}
if (allParamsPresent())
{
// Set up MQ environment
MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
//CCSID: DIS QMGR CCSID
MQEnvironment.CCSID = ccsid;
MQException.log = null; /* Tell MQ client library not to output anything. */
}
else
{
throw new IllegalArgumentException();
}
} /**
* Main entry point.
* @param args
*/
public static void main(String[] args)
{ EmptyQ readQ = new EmptyQ(); try
{
readQ.init(args);
readQ.emptyIt();
}
catch (IllegalArgumentException e)
{
System.err.println("Usage: java EmptyQ <-s ccsid> <-h host> <-p port> <-c channel> <-m QueueManagerName> <-q QueueName>");
System.exit(1);
}
} /**
* Connect to a queue manager, open a queue then destructively get (delete)
* all messages on the queue.
*/
private void emptyIt()
{
boolean loopAgain = true;
MQQueueManager _queueManager = null;
MQQueue queue = null;
int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED;
System.out.println("EmptyQ v1.0.0 by Capitalware Inc.");
try
{
_queueManager = new MQQueueManager(qManager);
System.out.println("EmptyQ: Connected to queue manager "+qManager); try
{
queue = _queueManager.accessQueue(inputQName, openOptions, null, null, null);
System.out.println("EmptyQ: Opened queue "+inputQName); int depth = queue.getCurrentDepth();
System.out.println("EmptyQ: Current depth: " + depth); MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_ACCEPT_TRUNCATED_MSG; MQMessage message;
while (loopAgain)
{
message = new MQMessage();
try
{
queue.get(message, getOptions, 1);
}
catch (MQException e)
{
if (e.completionCode == 1 && e.reasonCode == MQException.MQRC_TRUNCATED_MSG_ACCEPTED)
{
// Just what we expected!!
}
else
{
loopAgain = false;
if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE)
{
// Good, we are now done - no error!!
}
else
{
System.err.println("EmptyQ: MQException: " + e.getLocalizedMessage());
}
}
}
}
System.out.println("EmptyQ: Queue emptied.");
}
catch (MQException e1)
{
System.err.println("EmptyQ: MQException: " + e1.getLocalizedMessage());
}
finally
{
if (queue != null)
{
queue.close();
System.out.println("EmptyQ: Closed queue "+inputQName);
} if (_queueManager != null)
{
_queueManager.disconnect();
System.out.println("EmptyQ: Disconnect from "+qManager);
}
}
}
catch (MQException e1)
{
System.err.println("EmptyQ: MQException: " + e1.getLocalizedMessage());
}
}
}

下面,再贴一个我用shell写的壳:

 #!/bin/bash
# File: clearMQ.sh
# Whatfor:empty the queue's curdepth WHEN IT OVERSTOCKS,
# and all the performance depends on EmptyQ.class,
# which [@author Roger Lacroix, Capitalware Inc.] made it.thx this guy:)
# Auth: nextgodhand@163.com
# Usage: ./clearMQ.sh <QueueManagerName> <QueueName> [CHANNEL_NAME]
# #set user env.
#. ~/.bash_profile if [ $# -lt ]; then
echo "Usage: $0 <QueueManagerName> <QueueName> [CHANNEL_NAME]"
exit
fi QMGR=$
QNME=$
CHNN=$ if [ -z "`dspmq|grep "$QMGR"`" ]; then
echo "Error: $QMGR not running."
exit
fi if [ -z "`echo "DIS QUEUE(*)"|runmqsc $QMGR|grep "($QNME)"`" ]; then
echo "Error: $QNME not exists in $QMGR."
exit
fi if [ -z "$CHNN" ]; then
CHNN="SYSTEM.ADMIN.SVRCONN"
fi CCSID=`echo "DIS QMGR CCSID"|runmqsc $QMGR|grep -oE "CCSID\([0-9]+\)"|awk -F'(' '{printf "%d",$2}'`
PORT=`ps -fumqm|grep runmqlsr|grep " $QMGR "|awk '{print $NF}'` java -cp .:$CLASSPATH EmptyQ -s $CCSID -h localhost -c $CHNN -p $PORT -m $QMGR -q $QNME

使用起来很方便:

./clearMQ.sh mq_xxx queue_yyy

MQ队列管理的更多相关文章

  1. MQ队列管理器搭建(三)

    MQ集群及网关队列管理器的搭建 描述:     如上图所示,为MQ的集群搭建部署图.CLUSTERA.CLUSTERB分别是两个集群,其中Qm1-Qm3.GateWayA为CLUSTERA集群中的队列 ...

  2. MQ队列管理器搭建(二)

    MQ级联方式使用场景 使用场景:     如上图所示,Application1与Application2要进行通信或者消息互换,使用MQ中间件作为中介.上图中,Application1与Applica ...

  3. MQ队列管理器搭建(一)

    多应用单MQ使用场景 如上图所示,MQ独立安装,或者与其中一个应用同处一机.Application1与Application2要进行通信,但因为跨系统,所以引入中间件来实现需求.   Applicat ...

  4. mq队列管理器命令

    dspmq: 队列管理器显示 QMCIPSA-------队列管理器 runmqsc QMSAA  运行查找Q队列名 运行MQ命令 runmqsc QmgrName 如果是默认队列管理器,可以不带其名 ...

  5. MQ队列及常见操作

    一. 创建MQ队列管理器 1.1准备工作 到所安装websphere mq的机子上,进入/opt/mm/bin目录下,查询相关mq的情况,通过命令行./dspmq. 创建mq队列管理器的的时候要用mq ...

  6. com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: 为队列管理器提供的安全性认证无效

    com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: 为队列管理器“zm_queue_manager”提供的安全性认证无效, ...

  7. Java调用MQ队列

    IBM MQ 6.0中设置两个队列,(远程队列.通道之类都不设置). 队列管理器是XIR_QM_1502 队列名称是ESBREQ IP地址是10.23.117.134(远程的一台电脑,跟我的电脑不在一 ...

  8. postfix队列管理

    队列管理单元的服务器程序--qmgr,是整个postfix系统的中心枢纽.所有邮件,包括等待送出与从外界收进来的,都必须通过队列.了解队列的运行原理以及postfix如何处理队列,有助于你解决问题.  ...

  9. hadoop队列管理(指定queue跑程序)

    hadoop 升级到cdh5后,队列管理被取消,而是统一用资源池分配. hadoop2.0版本,Hadoop采用了平级队列组织方式,,管理员可将用户分到若干个扁平队列中,在每个队列中,可指定一个或几个 ...

随机推荐

  1. Handling Class Imbalance with R and Caret - An Introduction

    When faced with classification tasks in the real world, it can be challenging to deal with an outcom ...

  2. js常用的4种截取字符串方法

    平常经常把这几个api的参数记混了,于是打算记录下来,当不确定的时候在拿出来翻翻: 在做项目的时候,经常会需要截取字符串,所以常用的方法有slice().substr().substring().ma ...

  3. Notification的基本用法以及使用RemoteView实现自定义布局

    Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) ...

  4. Java泛型学习

    1.泛型的概念 泛型即"参数化类型",就比如我们定义方法的时候,定义一个变量,称为形参,变量值根据传进去的实参的值不同而改变.而泛型的出现,就是为了解决类型也能根据传进去的类型改变 ...

  5. javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印

    欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置. ...

  6. 可视化之Earth NullSchool

    上两篇我们分别介绍了<Berkeley Earth>和<AQICN>两个网站,今天来看一下Earth NullSchool. 这个网站的特色是风向图,之前有一篇可视化之风向图, ...

  7. 移动平台Unity3D 应用性能优化

    WeTest 导读 做了大概半年多VR应用了,VR由于双眼double渲染的原因,对性能的优化要求比较高,在项目的进展过程中,总结了一些关于移动平台上Unity3D的性能优化经验,供分享. 一.移动平 ...

  8. Android 创建虚拟机时“提示no system images installed for this target”

    经上网查证,发现原因在于CPU/ABI选项无法选择,并显示“No system images installed for this target”,也就是没有适合的系统镜像,通过与安装好了的ADT-b ...

  9. scrapy抓取淘宝女郎

    scrapy抓取淘宝女郎 准备工作 首先在淘宝女郎的首页这里查看,当然想要爬取更多的话,当然这里要查看翻页的url,不过这操蛋的地方就是这里的翻页是使用javascript加载的,这个就有点尴尬了,找 ...

  10. openfire极限优化

    日志优化   默认是 用info 级别,最好不用openfire原生的打日志方式.   离线消息用存储不打回方式,不要用打回方式   xmpp.offline.type=store_and_drop ...