2013-09-27 27 views

回答

2

每當你問一個問題,你應該先告訴你已經嘗試了你的問題的方法。現在你聽起來好像你在問某人爲你寫代碼而不是SO的動機。 請記住您的下一個問題。

現在,我們來您的問題。你試圖實現的其實很簡單。

你必須:

  1. 閱讀您的Java程序的文件轉換成字符串(希望你要傳輸純文本文件)。
  2. 創建一個MQMessage。
  3. 將字符串寫入MQMessage。
  4. 將MQMessage發佈到隊列中。

發送方

代碼讀取文件到字符串變量:

static String readFile(String path, Charset encoding) 
    throws IOException 
{ 
    byte[] encoded = Files.readAllBytes(Paths.get(path)); 
    return encoding.decode(ByteBuffer.wrap(encoded)).toString(); 
} 

參考:here

代碼從這個字符串創建MQMessage和p osting到隊列:

 MQQueueManager qMgr = new MQQueueManager(YourQueueManagerName); 
    // Set up the options on the queue we wish to open... 
    int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | 
    MQC.MQOO_OUTPUT ; 

    // Now specifythe queue that we wish to open, and the open options... 
    MQQueue inputQ = 
    qMgr.accessQueue(Inputqueue,openOptions); 
    Charset charset = Charset.forName("UTF-8"); 
    String messg=(readFile("C:\test.txt", charset)); 

    MQMessage InputMsg1 = new MQMessage(); 
    InputMsg1.writeString(messg); 

    MQPutMessageOptions pmo = new MQPutMessageOptions(); 
    inputQ.put(InputMsg1,pmo); 

    inputQ.close(); 
    qMgr.disconnect(); 

接收側

代碼從隊列中讀取消息:

 MQQueueManager qMgr2 = new MQQueueManager(OutQM); 
    // Set up the options on the queue we wish to open... 

    int openOptions2 = MQC.MQOO_INPUT_AS_Q_DEF | 
    MQC.MQOO_OUTPUT ; 
    // Now specifythe queue that we wish to open, and the open options... 
    MQQueue Output = 
    qMgr2.accessQueue(OutQ,openOptions2); 

    MQMessage retrievedMessage = new MQMessage(); 

    MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the defaults 
    //  // same as 
    //  // MQGMO_DEFAULT 
    //  // get the message off the queue.. 
    Output.get(retrievedMessage, gmo); 
    String msgText = retrievedMessage.readString(retrievedMessage.getDataLength()); 

在一個字符串變量接收消息可以使用後數據是你想要的方式,或者你可以將它保存在一個文件中。

0

1)首先,您需要編寫一些代碼來從您要發送的文件中讀取數據。
2)接下來,請參閱發送/接收隊列消息的MQ Java樣本。 MQSample.java就是一個很好的例子。您需要修改示例以設置從文件中讀取的數據。喜歡的東西:

// Define a simple WebSphere MQ Message ... 
    MQMessage msg = new MQMessage(); 
    // ... and write some text in UTF8 format 
    msg.write(fileData); 
    queue.put(msg); 

3)在接收端做的正是這麼做的反向

queue.get(msg); 
    msg.readFully(byte[]); 
1

沒有用Java編寫的一個免費的開源項目叫做通用文件捷運(UFM)。你可以找到UFM在http://www.capitalware.biz/ufm_overview.html

去下載的源代碼,看看如何做到這一點或簡單地使用UFM。

+0

+1寫得很好的代碼。但是最初,爲了學習的目的,編寫自己的簡單代碼會很好。其他實用工具也很有用,寫得很好,但僅僅爲了好奇,有人真的會爲他們購買999美元的支持嗎? – nitgeek