IBM MQ Series发送消息和读取消息(use JAVA)

MQSender .java
向IBM MQ发送消息
import java.io.IOException;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class MQSender implements Runnable {
	private static final String MQ_MANAGER = "MQ_TEST";
	private static final String MQ_HOST_NAME = "192.168.0.1";
	private static final String MQ_CHANNEL = "TEST";
	private static final String MQ_QUEUE_NAME = "TEST_QUEUE";
	private static final int MQ_PROT = 8100;
	private static final int MQ_CCSID = 819;

	public void run() {
		MQQueueManager mqQueueManager = null;
		MQQueue mqQueue = null;
		try {
			MQEnvironment.addConnectionPoolToken();
			MQEnvironment.hostname = MQ_HOST_NAME;
			MQEnvironment.channel = MQ_CHANNEL;
			MQEnvironment.port = MQ_PROT;
			MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
			MQEnvironment.CCSID = MQ_CCSID;
			int sendOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
			mqQueueManager = new MQQueueManager(MQ_MANAGER);
			mqQueue = mqQueueManager.accessQueue(MQ_QUEUE_NAME, sendOptions, null, null, null);
			MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
			MQMessage mqMessage = null;
			String putMessage = "test";
			mqMessage = new MQMessage();
			mqMessage.format = MQC.MQFMT_STRING;
			mqMessage.write(putMessage.getBytes());
			mqQueue.put(mqMessage, mqPutMessageOptions);
		} catch (MQException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if (mqQueue != null) {
				try {
					mqQueue.close();
				} catch (MQException e) {
					e.printStackTrace();
				}
			}
			if (mqQueueManager != null) {
				try {
					mqQueueManager.close();
				} catch (MQException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String arg[]) {
		MQSender client = new MQSender();
		Thread mqClientThread = new Thread(client);
		mqClientThread.start();
	}
}


MQReceiver.java 监听IBM MQ取回消息
import java.io.IOException;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

public class MQSender implements Runnable {
	private static final String MQ_MANAGER = "MQ_TEST";
	private static final String MQ_HOST_NAME = "192.168.0.1";
	private static final String MQ_CHANNEL = "TEST";
	private static final String MQ_QUEUE_NAME = "TEST_QUEUE";
	private static final int MQ_PROT = 8100;
	private static final int MQ_CCSID = 819;

	public void run() {
		MQQueueManager mqQueueManager = null;
		MQQueue mqQueue = null;
		try {
			MQEnvironment.addConnectionPoolToken();
			MQEnvironment.hostname = MQ_HOST_NAME;
			MQEnvironment.channel = MQ_CHANNEL;
			MQEnvironment.port = MQ_PROT;
			MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
			MQEnvironment.CCSID = MQ_CCSID;
			int sendOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
			mqQueueManager = new MQQueueManager(MQ_MANAGER);
			mqQueue = mqQueueManager.accessQueue(MQ_QUEUE_NAME, sendOptions, null, null, null);
			MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
			MQMessage mqMessage = null;
			String putMessage = "test";
			mqMessage = new MQMessage();
			mqMessage.format = MQC.MQFMT_STRING;
			mqMessage.write(putMessage.getBytes());
			mqQueue.put(mqMessage, mqPutMessageOptions);
		} catch (MQException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if (mqQueue != null) {
				try {
					mqQueue.close();
				} catch (MQException e) {
					e.printStackTrace();
				}
			}
			if (mqQueueManager != null) {
				try {
					mqQueueManager.close();
				} catch (MQException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String arg[]) {
		MQSender client = new MQSender();
		Thread mqClientThread = new Thread(client);
		mqClientThread.start();
	}
}

你可能感兴趣的:(java,thread,IBM)