通过websphere mq 发送消息:
public class MQChanal { public static void main(String args[]){ MQChanal first = new MQChanal(); first.test(); } public void test(){ String qManager = "QMGR"; String qName = "SendJMSQueue"; try { MQEnvironment.hostname="172.16.40.216"; //MQEnvironment.port=1414; MQEnvironment.channel="Chanal1"; //通道的名字 MQEnvironment.CCSID =1381; // 资源管理器的编码字符集标识 MQQueueManager qMgr = new MQQueueManager(qManager); //定义并初始化队列管理器对象并连接 int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; // 设置将要连接的队列属性 MQQueue queue = qMgr.accessQueue(qName, openOptions); //连接队列 MQMessage msg = new MQMessage(); //定义一个简单的消息 msg.writeUTF("Hello, World!"); MQPutMessageOptions pmo = new MQPutMessageOptions(); //设置写入消息的属性(默认属性) System.out.println("Sending a message..."); /* for(int j=0;j<5;j++){ String str ="test11111111111"; str = str+j; msg.writeUTF(str); queue.put(msg, pmo); }*/ queue.put(msg, pmo); //将消息写入队列 queue.close(); //关闭队列 qMgr.disconnect(); //从队列管理器断开 } catch (MQException ex) { System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } catch (java.io.IOException ex) { System.out.println("An IOException occured whilst writing to the message buffer: " + ex); } } } 接收消息: public class MQRec { public static void main(String args[]){ MQRec first = new MQRec(); first.test(); } public void test(){ String qManager = "QMGR"; String qName = "SendJMSQueue"; try { MQEnvironment.hostname="172.16.40.216"; MQEnvironment.channel="Chanal1"; MQEnvironment.CCSID =1381; MQQueueManager qMgr = new MQQueueManager(qManager); int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; MQQueue queue = qMgr.accessQueue(qName, openOptions); MQMessage rcvMessage = new MQMessage(); MQGetMessageOptions gmo = new MQGetMessageOptions(); queue.get(rcvMessage, gmo); String msgText = rcvMessage.readUTF(); System.out.println("The message is: " + msgText); queue.close(); qMgr.disconnect(); } catch (MQException ex) { System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } catch (java.io.IOException ex) { System.out.println("An IOException occured whilst writing to the message buffer: " + ex); } } } 发送文件: package mq; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.ibm.mq.*; public class MQFileSend { public static void main(String args[]) { MQFileSend first = new MQFileSend(); first.test(); } public void test() { String qManager = "QMGR"; String qName = "SendJMSQueue"; try { MQEnvironment.hostname = "172.16.40.216"; MQEnvironment.channel = "Chanal1"; MQEnvironment.CCSID = 1381; MQQueueManager qMgr = new MQQueueManager(qManager); int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; MQQueue queue = qMgr.accessQueue(qName, openOptions); MQMessage msg = new MQMessage(); File file = new File("d://人员联系表.xls"); msg.writeInt8(file.length()); msg.writeUTF(file.getName()); msg.write(getBytesFromFile(file)); MQPutMessageOptions pmo = new MQPutMessageOptions(); System.out.println("Sending a file..."); queue.put(msg, pmo); queue.close(); qMgr.disconnect(); } catch (MQException ex) { System.out .println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } catch (java.io.IOException ex) { System.out .println("An IOException occured whilst writing to the message buffer: " + ex); } } public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } // Close the input stream and return bytes is.close(); return bytes; } } 接收文件: public class MQFileRec { public static void main(String args[]) throws ClassNotFoundException{ MQFileRec first = new MQFileRec(); first.test(); } public void test() throws ClassNotFoundException{ String qManager = "QMGR"; String qName = "SendJMSQueue"; try { MQEnvironment.hostname="172.16.40.216"; MQEnvironment.channel="Chanal1"; MQEnvironment.CCSID =1381; MQQueueManager qMgr = new MQQueueManager(qManager); int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; MQQueue queue = qMgr.accessQueue(qName, openOptions); MQMessage rcvMessage = new MQMessage(); MQGetMessageOptions gmo = new MQGetMessageOptions(); queue.get(rcvMessage, gmo); Long length = rcvMessage.readInt8(); byte[] bytes = new byte[(int)length.intValue()]; String filename = rcvMessage.readUTF(); rcvMessage.readFully(bytes); //ByteArrayInputStream ba = new ByteArrayInputStream(bytes); FileOutputStream fos = new FileOutputStream(new File("C:/"+filename)); fos.write(bytes,0,length.intValue()); System.out.println("接受文件结束....."); fos.close(); queue.close(); qMgr.disconnect(); } catch (MQException ex) { System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } catch (java.io.IOException ex) { System.out.println("An IOException occured whilst writing to the message buffer: " + ex); } } } |