MQファイル転送


Websphere MQは、IBMのメッセージミドルウェアについてあまり紹介していません.
初心者は初めてMQを学んで、達人は笑ってはいけません.恥をかいた.
MQファイルの転送
MQ転送ファイルについて、私が考えているのは:
A.まず、fileBeanという名前のシーケンス化クラスを定義します.クラスの属性はfileNameとfileContentの2つです.
B.BASE 64 Encoderに合わせて入力ストリームでファイルをBASE 64 EncoderベースのString符号化してファイルの内容とする.
C.ファイル名とファイル内容をfile Beanのfile Content属性にセットする.
D.MQを呼び出してこのObjectをリモートキューに書き込む.
E.受信者がメッセージを受信するとreadObject()メソッドで読み出し、file Beanに強く移行する
F.file Beanからファイル名とファイル内容を取り出し、ファイル内容をBASE 64 Decoderで復号する
G.ファイル出力ストリームでファイルを指定された位置に書き込み、これで大成功です.
------------------------くだらないことは言わないで、コードを見て------------------------
ファイルシーケンス化クラス


package com.test.mq;

import java.io.Serializable;
/**
 * 
 * <p>
 * Title: FileBean.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: shunde
 * </p>
 * 
 * @author: listening
 * @create date Nov 8, 2009
 */
public class FileBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String fileName = "";//    

	private String fileContent = "";//     (BASE64Encoder     )

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileContent() {
		return fileContent;
	}

	public void setFileContent(String fileContent) {
		this.fileContent = fileContent;
	}

}

ファイル送受信クラス

package com.test.mq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

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;

/**
 * 
 * <p>
 * Title: MQSendAndReceiveUtil.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: shunde
 * </p>
 * 
 * @author: listening
 * @create date Nov 8, 2009
 */
public class MQSendAndReceiveUtil {
	private MQQueueManager qManager;
	private MQQueue queue;

	private static String qmManager = "QM_00000000";//        
	private static String remoteQName = "RQ_88888888";//       
	private static String localQName = "LQ_00000000";//     
	private static String hostname = "192.168.1.66";//     
	private static String channel = "DC.SVRCONN";//        
	private static int ccsid = 1381;
	private static int port = 1414;

	@SuppressWarnings("unchecked")
	private MQSendAndReceiveUtil() {
		MQEnvironment.hostname = hostname;
		MQEnvironment.channel = channel;
		MQEnvironment.CCSID = ccsid;
		MQEnvironment.port = port;
		MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
				MQC.TRANSPORT_MQSERIES);

		try {
			qManager = new MQQueueManager(qmManager);
		} catch (MQException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * Description:         ,  
	 * 
	 * @param:
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	private void createConnection() {
		if (qManager == null) {
			new MQSendAndReceiveUtil();
		}
	}

	
	/**
	 * 
	 * Description:    
	 * 
	 * @param:String fileName -   
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public void sendFileMessage(String fileName) {
		this.createConnection();
		try {
			int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;//       

			queue = qManager.accessQueue(remoteQName, openOptions, null, null,
					null);//     (               )

			MQPutMessageOptions pmo = new MQPutMessageOptions();//           

			MQMessage message = new MQMessage();//   MQ    

			FileBean file = new FileBean();//   FileBean       

			file.setFileName(fileName);

			InputStream in = new FileInputStream("D:\\" + fileName); //            

			BASE64Encoder encoder = new BASE64Encoder();//   BASE64Encoder    

			byte[] data = new byte[in.available()];

			in.read(data);

			String content = encoder.encode(data);//        String

			file.setFileContent(content);

			message.writeObject(file);//  FileBean   file       

			queue.put(message, pmo);

			qManager.commit();
			this.logInfo("      ");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAction();
		}

	}

	public void receiveFileMessage() {
		try {
			int openOptions = MQC.MQOO_INPUT_SHARED
					| MQC.MQOO_FAIL_IF_QUIESCING;//         

			queue = qManager.accessQueue(localQName, openOptions, null, null,
					null);//     (               )

			MQGetMessageOptions gmo = new MQGetMessageOptions();

			gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;//     

			gmo.options = gmo.options + MQC.MQGMO_WAIT;//      

			gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;//      

			gmo.waitInterval = 100;//     

			MQMessage inMsg = new MQMessage();//       

			queue.get(inMsg, gmo);//         

			FileBean fileBean = new FileBean();

			fileBean = (FileBean) inMsg.readObject(); //        FileBean  

			String content = fileBean.getFileContent();//      

			BASE64Decoder decoder = new BASE64Decoder();//        

			byte[] contentArray = decoder.decodeBuffer(content);//     byte  

			String path = "E:\\" + fileBean.getFileName();

			FileOutputStream out = new FileOutputStream(new File(path));//                

			out.write(contentArray, 0, contentArray.length);

			// System.out.print(fileBean.getFileName());

			qManager.commit();//     

			this.logInfo("      ,     ");//     
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * Description:     
	 * 
	 * @param:
	 * @return:
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public void closeAction() {
		try {
			if (queue != null) {
				queue.close();
				queue = null;
			} else if (qManager != null) {
				qManager.close();
				qManager = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * Description:        
	 * 
	 * @param:String message-    
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public void logInfo(String message) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		System.out.println(format.format(new Date()) + "-------" + message
				+ "+-------------");
	}

	/**
	 * 
	 * Description:main    
	 * 
	 * @param:
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public static void main(String[] args) {
		new MQSendAndReceiveUtil().sendFileMessage("test.xml");
		//new MQSendAndReceiveUtil().receiveFileMessage();
	}

}

これで、ファイル送信受信が完了します.