Java implements the receive queue file function in conjunction with WebSphere MQ
- 2020-04-01 04:19:57
- OfStack
Let's start with a brief introduction to websphere mq and installation usage
Websphere mq : for the transmission of information has a cross-platform function.
1 install websphere mq and start
2 websphere mq establishes the queue Manager (e.g. MQSI_SAMPLE_QM)
3 establish the queue type by selecting the Local type (e.g. Lq ; )
4 Establish channels type select Server Connection (such as BridgeChannel)
Next, let's look at the example code:
MQFileReceiver.java
package com.mq.dpca.file;
import java.io.File;
import java.io.FileOutputStream;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
import com.mq.dpca.msg.MQConfig;
import com.mq.dpca.util.ReadCmdLine;
import com.mq.dpca.util.RenameUtil;
public class MQFileReceiver {
private MQQueueManager qmgr; //Connect to the queue manager
private MQQueue inQueue; //A transmission queue
private String queueName = ""; //The name of the queue
private String host = ""; //
private int port = 1414; //The port number of the listener
private String channel = ""; //The name of the channel
private String qmgrName = ""; //Queue manager
private MQMessage inMsg; //Creating message buffers
private MQGetMessageOptions gmo; //Set the get message option
private static String fileName = null; //Receive the message on the queue and store it in a file
private int ccsid = 0;
private static String file_dir = null;
public static void main(String args[]) {
MQFileReceiver mfs = new MQFileReceiver();
//Initialize connection
mfs.initproperty();
//Receive the file
mfs.runGoupReceiver();
//Gets the shell script name
// String shellname = MQConfig.getValueByKey(fileName);
// if(shellname!=null&&!"".equals(shellname)){
// // call shell
// ReadCmdLine.callShell(shellname);
// }else{
// System.out.println("have no shell name,Only receive files.");
// }
}
public void runGoupReceiver() {
try {
init();
getGroupMessages();
qmgr.commit();
System.out.println("n Messages successfully Receive ");
} catch (MQException mqe) {
mqe.printStackTrace();
try {
System.out.println("n Backing out Transaction ");
qmgr.backout();
System.exit(2);
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
private void init() throws Exception {
MQEnvironment.hostname = host;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
qmgr = new MQQueueManager(qmgrName);
int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF
| MQConstants.MQOO_FAIL_IF_QUIESCING;
inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
}
public void getGroupMessages() {
gmo = new MQGetMessageOptions();
gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING;
gmo.options = gmo.options + MQConstants.MQGMO_SYNCPOINT;
gmo.options = gmo.options + MQConstants.MQGMO_WAIT;
gmo.waitInterval = 5000;
gmo.options = gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE;
gmo.options = gmo.options + MQConstants.MQGMO_LOGICAL_ORDER;
gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID;
inMsg = new MQMessage();
try {
FileOutputStream fos = null;
while (true) {
try {
inQueue.get(inMsg, gmo);
if (fos == null) {
try {
fileName = inMsg.getStringProperty("fileName");
String fileName_full = null;
fileName_full = file_dir + RenameUtil.rename(fileName);
fos = new FileOutputStream(new File(fileName_full));
int msgLength = inMsg.getMessageLength();
byte[] buffer = new byte[msgLength];
inMsg.readFully(buffer);
fos.write(buffer, 0, msgLength);
char x = gmo.groupStatus;
if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
System.out.println("Last Msg in Group");
break;
}
inMsg.clearMessage();
} catch (Exception e) {
System.out
.println("Receiver the message without property,do nothing!");
inMsg.clearMessage();
}
} else {
int msgLength = inMsg.getMessageLength();
byte[] buffer = new byte[msgLength];
inMsg.readFully(buffer);
fos.write(buffer, 0, msgLength);
char x = gmo.groupStatus;
if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
System.out.println("Last Msg in Group");
break;
}
inMsg.clearMessage();
}
} catch (Exception e) {
char x = gmo.groupStatus;
if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
System.out.println("Last Msg in Group");
}
break;
}
}
if (fos != null)
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void initproperty() {
MQConfig config = new MQConfig().getInstance();
if (config.getMQ_MANAGER() != null) {
qmgrName = config.getMQ_MANAGER();
queueName = config.getMQ_QUEUE_NAME();
channel = config.getMQ_CHANNEL();
host = config.getMQ_HOST_NAME();
port = Integer.valueOf(config.getMQ_PROT());
ccsid = Integer.valueOf(config.getMQ_CCSID());
file_dir = config.getFILE_DIR();
}
}
}