MQ client

public class mqSendSample { @SuppressWarnings
("rawtypes")
private static Hashtable<String, Comparable> env = new Hashtable<String, Comparable>();

// queue manager name
private static String queueManagerName;
// queue manager reference
private static MQQueueManager queueManager;
// queue name
private static String queueName;
// queue reference
private MQQueue queue;

/**
* <initialize the queue manager connection when the application starts
* Since the connection queue manager is like connection data, it requires more resources when establishing , The connection time is long, so do not create a close every time, it is recommended that the application keep
* or multiple queue manager connections but pay attention to closing the connection and releasing resources when the application closes!
*
* @throws Exception
*/
@BeforeClass
public static void initEnvironment() throws Exception {
// server id, name
env.put(MQConstants.HOST_NAME_PROPERTY, "192.168.102.29");
// Connection channel
env.put(MQConstants.CHANNEL_PROPERTY, "ESB_TEST");
// The code used by the server MQ service is 1381 for GBK, 1208 for UTF(Coded Character Set Identifier:CCSID)
env.put(MQConstants.CCSID_PROPERTY, 1208);
// port number
env.put(MQConstants.PORT_PROPERTY, 1414);
// transport type
env.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);
MQEnvironment. properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
// set the target queue manager
queueManagerName = "AIRP_QMGR_002";
// set the target queue
queueName = "topic_in3";

// establish a queue manager connection
connectQM();
}

/ **
* Release the queue management connection resources when the program ends
*
* @throws Exception
*/
@AfterClass
public static void destroyEnvironment() throws Exception {
disconnectQM();
}

@Test
public void testSend() throws Exception {
// Queue open parameters
int openOptions = MQConstants.MQOO_BIND_AS_Q_DEF
| MQConstants.MQOO_OUTPUT;
// Open the queue (within the same thread, the queue can only be opened once at the same time)
queue = queueManager.accessQueue(queueName, openOptions);
// Set the sending message parameters as: synchronization, and support Transaction
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = MQConstants.MQPMO_SYNCPOINT;
try {
// send a message (this is for multiple simultaneous messages)
for (int i = 0; i < 10; i++) {
// Set message format to string type
MQMessage msg = new MQMessage();
msg.format = MQConstants.MQFMT_STRING;
/*
* Set custom message header
*/
msg.setStringProperty("sys", "md"); // service id
msg.characterSet = 1208;
// message content
String message = "<msg>Hello<msg>";
// set message content
msg.writeString(message);
// send ?message
queue.put(msg, pmo);

}
// commit the transaction
queueManager.commit();

} catch (MQException e) {
// transaction rollback
queueManager.backout();
e.printStackTrace();
} finally {
// close the queue
if (queue != null) {
queue.close();
}
}

}

private static void connectQM() throws Exception {
queueManager = new MQQueueManager(queueManagerName, env);
}

private static void disconnectQM() throws Exception {
if (queueManager != null) {
queueManager.disconnect();
}
}

public static void main (String[] args) throws Exception {
mqSendSample queue1 = new mqSendSample();
queue1.initEnvironment();
// queue open parameters
int openOptions = MQConstants.MQOO_BIND_AS_Q_DEF
| MQConstants.MQOO_OUTPUT;
// open the queue (within the same thread, only The queue can be opened once)
queue1.queue = queue1.queueManager.accessQueue(queueName, openOptions);
// Set the sending message parameters as: synchronization and support for transactions
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = MQConstants.MQPMO_SYNCPOINT;
try {
// 设置消息格式为字符串类型
MQMessage msg = new MQMessage();
msg.format = MQConstants.MQFMT_STRING;
// 消息内容编码(1208:utf-8)
msg.characterSet = 1208;
msg.setStringProperty("sys", "md");
String message = "<root><RequestHead><RequestID>123</RequestID><SourceSystem>VMSCODE</SourceSystem>" +
"<TargetSystem>DCECODE</TargetSystem><ServiceName>S0802001A</ServiceName>" +
"<ServiceOperation>TO_ZF</ServiceOperation>" +
"<ServiceVersion></ServiceVersion></RequestHead><RequestBody><updateProductReq><product><action>2</action><prod_id>1010002956</prod_id><prod_code>1010002956</prod_code><prod_simple_code></prod_simple_code><prod_name>测试产品00013</prod_name><unit_id>118</unit_id><prod_memo2k></prod_memo2k><prod_list_price>200.0000</prod_list_price></product></updateProductReq></RequestBody></root>";
// 设置消息内容
msg.writeString(message);
// 发�?消息
queue1.queue.put(msg, pmo);

// 提交事务
queue1.queueManager.commit();

} catch (MQException e) {
// 事务回滚
queue1.queueManager.backout(); if (queue1.queue != null) { // close the queue } finally {
e.printStackTrace();



queue1.queue.close();
}
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326220539&siteId=291194637