标签:des style blog java color 使用
分享一段代码,很实用。
下面这段java代码是我在国外一个论坛上发现的,源地址已经忘了。代码的作用是可以删除正在使用的mq的队列消息,管理mq的人一定知道它的美妙了吧,哈哈。
我拿来改了下,增加了2个参数支持:ccsid和channel。上代码:
1 import java.util.Hashtable; 2 import com.ibm.mq.*; 3 4 /** 5 * A simply Java class to destructively read (delete) all message on a queue. 6 * 7 * @author Roger Lacroix, Capitalware Inc. 8 * @return 0 for ok or 1 for failed. 9 * @version 1.0.0 10 * @license Apache 2 License 11 */ 12 13 /** 14 * lichmama reedited 15 * change: add parameters to set ccsid and channel 16 */ 17 18 public class EmptyQ 19 { 20 private Hashtable params = null; 21 public int port = 1414; 22 public String hostname; 23 public String channel; 24 public String qManager; 25 public String inputQName; 26 public int ccsid = 1208; 27 28 /** 29 * The constructor 30 */ 31 public EmptyQ() 32 { 33 super(); 34 } 35 36 /** 37 * Check if all of the parameters were passed to the class file. 38 * @return true/false 39 */ 40 private boolean allParamsPresent() 41 { 42 boolean b = params.containsKey("-s") && params.containsKey("-h") && params.containsKey("-p") && params.containsKey("-c") && params.containsKey("-m") && params.containsKey("-q"); 43 if (b) 44 { 45 try 46 { 47 port = Integer.parseInt((String) params.get("-p")); 48 ccsid = Integer.parseInt((String) params.get("-s")); 49 } 50 catch (NumberFormatException e) 51 { 52 b = false; 53 } 54 // Set up MQ environment 55 hostname = (String) params.get("-h"); 56 channel = (String) params.get("-c"); 57 qManager = (String) params.get("-m"); 58 inputQName = (String) params.get("-q"); 59 60 } 61 return b; 62 } 63 64 /** 65 * Initialize MQ environment variables 66 * @param args 67 * @throws IllegalArgumentException 68 */ 69 private void init(String[] args) throws IllegalArgumentException 70 { 71 params = new Hashtable(5); 72 if (args.length > 0 && (args.length % 2) == 0) 73 { 74 for (int i = 0; i < args.length; i += 2) 75 params.put(args[i], args[i + 1]); 76 } 77 else 78 { 79 throw new IllegalArgumentException(); 80 } 81 if (allParamsPresent()) 82 { 83 // Set up MQ environment 84 MQEnvironment.hostname = hostname; 85 MQEnvironment.channel = channel; 86 MQEnvironment.port = port; 87 //CCSID: DIS QMGR CCSID 88 MQEnvironment.CCSID = ccsid; 89 MQException.log = null; /* Tell MQ client library not to output anything. */ 90 } 91 else 92 { 93 throw new IllegalArgumentException(); 94 } 95 } 96 97 /** 98 * Main entry point. 99 * @param args 100 */ 101 public static void main(String[] args) 102 { 103 104 EmptyQ readQ = new EmptyQ(); 105 106 try 107 { 108 readQ.init(args); 109 readQ.emptyIt(); 110 } 111 catch (IllegalArgumentException e) 112 { 113 System.err.println("Usage: java EmptyQ <-s ccsid> <-h host> <-p port> <-c channel> <-m QueueManagerName> <-q QueueName>"); 114 System.exit(1); 115 } 116 } 117 118 /** 119 * Connect to a queue manager, open a queue then destructively get (delete) 120 * all messages on the queue. 121 */ 122 private void emptyIt() 123 { 124 boolean loopAgain = true; 125 MQQueueManager _queueManager = null; 126 MQQueue queue = null; 127 int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED; 128 System.out.println("EmptyQ v1.0.0 by Capitalware Inc."); 129 try 130 { 131 _queueManager = new MQQueueManager(qManager); 132 System.out.println("EmptyQ: Connected to queue manager "+qManager); 133 134 try 135 { 136 queue = _queueManager.accessQueue(inputQName, openOptions, null, null, null); 137 System.out.println("EmptyQ: Opened queue "+inputQName); 138 139 int depth = queue.getCurrentDepth(); 140 System.out.println("EmptyQ: Current depth: " + depth); 141 142 MQGetMessageOptions getOptions = new MQGetMessageOptions(); 143 getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_ACCEPT_TRUNCATED_MSG; 144 145 MQMessage message; 146 while (loopAgain) 147 { 148 message = new MQMessage(); 149 try 150 { 151 queue.get(message, getOptions, 1); 152 } 153 catch (MQException e) 154 { 155 if (e.completionCode == 1 && e.reasonCode == MQException.MQRC_TRUNCATED_MSG_ACCEPTED) 156 { 157 // Just what we expected!! 158 } 159 else 160 { 161 loopAgain = false; 162 if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) 163 { 164 // Good, we are now done - no error!! 165 } 166 else 167 { 168 System.err.println("EmptyQ: MQException: " + e.getLocalizedMessage()); 169 } 170 } 171 } 172 } 173 System.out.println("EmptyQ: Queue emptied."); 174 } 175 catch (MQException e1) 176 { 177 System.err.println("EmptyQ: MQException: " + e1.getLocalizedMessage()); 178 } 179 finally 180 { 181 if (queue != null) 182 { 183 queue.close(); 184 System.out.println("EmptyQ: Closed queue "+inputQName); 185 } 186 187 if (_queueManager != null) 188 { 189 _queueManager.disconnect(); 190 System.out.println("EmptyQ: Disconnect from "+qManager); 191 } 192 } 193 } 194 catch (MQException e1) 195 { 196 System.err.println("EmptyQ: MQException: " + e1.getLocalizedMessage()); 197 } 198 } 199 }
下面,再贴一个我用shell写的壳:
1 #!/bin/bash 2 # File: clearMQ.sh 3 # Whatfor:empty the queue‘s curdepth WHEN IT OVERSTOCKS, 4 # and all the performance depends on EmptyQ.class, 5 # which [@author Roger Lacroix, Capitalware Inc.] made it.thx this guy:) 6 # Auth: kongdl@eastcom-sw.com 7 # Usage: ./clearMQ.sh <QueueManagerName> <QueueName> [CHANNEL_NAME] 8 # 9 10 #set user env. 11 #. ~/.bash_profile 12 13 if [ $# -lt 2 ]; then 14 echo "Usage: $0 <QueueManagerName> <QueueName> [CHANNEL_NAME]" 15 exit 1 16 fi 17 18 QMGR=$1 19 QNME=$2 20 CHNN=$3 21 22 if [ -z "`dspmq|grep "$QMGR"`" ]; then 23 echo "Error: $QMGR not running." 24 exit 2 25 fi 26 27 if [ -z "`echo "DIS QUEUE(*)"|runmqsc $QMGR|grep "($QNME)"`" ]; then 28 echo "Error: $QNME not exists in $QMGR." 29 exit 3 30 fi 31 32 if [ -z "$CHNN" ]; then 33 CHNN="SYSTEM.ADMIN.SVRCONN" 34 fi 35 36 CCSID=`echo "DIS QMGR CCSID"|runmqsc $QMGR|grep -oE "CCSID\([0-9]+\)"|awk -F‘(‘ ‘{printf "%d",$2}‘` 37 PORT=`ps -fumqm|grep runmqlsr|grep " $QMGR "|awk ‘{print $NF}‘` 38 39 java -cp .:$CLASSPATH EmptyQ -s $CCSID -h localhost -c $CHNN -p $PORT -m $QMGR -q $QNME
使用起来很方便:
1 ./clearMQ.sh mq_xxx queue_yyy
标签:des style blog java color 使用
原文地址:http://www.cnblogs.com/lichmama/p/3835341.html