import java.text.SimpleDateFormat;
import java.util.Date;
import sms.util.UuidUtil;
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.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
public class TestMQ {
static MQQueueManager qMgr;
static int CCSID = 1381;
static String MqSendQueueName = "SMSSYSTEM_REQUEST_IN";
static String MqReceviceQueueName = "SMSSYSTEM_REQUEST_YWPT_OUT";
public static void connect() throws MQException {
MQEnvironment.hostname = "10.107.0.1";
//通道
MQEnvironment.channel = "SMS.DV.SVRCONN";
MQEnvironment.port = 4000;
MQEnvironment.CCSID = CCSID;
//队列管理器
qMgr = new MQQueueManager("mq_sms_server");
}
public static void sendMsg(String msgStr) {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT
| MQC.MQOO_INQUIRE;
MQQueue queue = null;
try {
// 建立Q1通道的连接
queue = qMgr.accessQueue(MqSendQueueName, openOptions, null, null,
null);
MQMessage msg = new MQMessage();// 要写入队列的消息
msg.format = MQC.MQFMT_STRING;
msg.characterSet = CCSID;
msg.encoding = CCSID;
// msg.writeObject(msgStr.getBytes("utf-8")); //将消息写入消息对象中
// msg.writeString(msgStr);
msg.write(msgStr.getBytes("UTF-8"));
MQPutMessageOptions pmo = new MQPutMessageOptions();
msg.expiry = -1; // 设置消息用不过期
queue.put(msg, pmo);// 将消息放入队列
System.out.println("发送成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (queue != null) {
try {
queue.close();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void receiveMsg() {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT
| MQC.MQOO_INQUIRE;
MQQueue queue = null;
try {
// queue = qMgr.accessQueue(queueString, openOptions, null,
// null,null);
queue = qMgr.accessQueue(MqReceviceQueueName, openOptions, null,
null, null);
System.out.println("该队列当前的深度为:" + queue.getCurrentDepth());
System.out.println("===========================");
int depth = queue.getCurrentDepth();
// 将队列的里的消息读出来
while (depth-- > 0) {
MQMessage msg = new MQMessage();// 要读的队列的消息
MQGetMessageOptions gmo = new MQGetMessageOptions();
queue.get(msg, gmo);
System.out.println("消息的大小为:" + msg.getDataLength());
// System.out.println("消息的内容:\n"+msg.readStringOfByteLength(msg.getDataLength()));
byte[] messageBody = new byte[msg.getDataLength()];
msg.readFully(messageBody);
String receiveMsg = new String(messageBody, "UTF-8");
System.out.println("返回报文:" + receiveMsg);
System.out.println("---------------------------");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (queue != null) {
try {
queue.close();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws MQException {
String phone = "18888888888";
String sysId="asf25vf03927418a828b28d8a84ec71b";
connect();
int i = 10000;
// while(i<12){
String uuid = UuidUtil.get32UUID();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String dateString = dateFormater.format(date);
String smscontent = "测试循环发送" + i;
String xml = ""
+ sysId
+ ""
+ dateString
+ ""
+ uuid
+ "36561"
+ phone
+ ""
+ smscontent
+ "0";
System.out.println("发送报文:" + xml);
sendMsg(xml);
System.out.println("发送次数:" + i);
i++;
// }
/*try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
receiveMsg();
System.out.println("计时:"+i++);
}
}
}