java ibm mq 通讯,JAVA连接IBM MQ

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++);

}

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值