簡體   English   中英

使用 JMS 發送和接收 SWIFT 消息

[英]Sending and Receiving SWIFT-messages with JMS

我目前正在嘗試在 SWIFT FileAct 上發送和接收消息。 當前的實現基於 JMS。 要求是,我需要發送一個 xml 消息作為有效負載,一個 XML 標頭,根據我得到的信息應該是一個 RFH2 標頭和可能的附件文件(因此是 FileAct)。

我與隊列管理器建立了有效連接,並且能夠發送和讀取一般文本或字節消息。 問題是,就 RFH2 標頭和有效負載(以及以后可能的附件)而言,格式似乎不正確。 因此,當我閱讀消息(我可以自己發送)時,我正在處理 stream 字節。 問題是,當消息的哪一部分是什么(標頭/有效負載/附件)時,如何說,因為它只是一個 stream 字節。

我可以添加一些代碼塊。 以及操作完成后來自控制台的兩個屏幕截圖。 也許你可以看到一些我想念的東西?

public void sendAByteMessage(JmsConnectionFactory connectionFactory, Destination destination) {
  String ifCobaString = "IF_COBA";
  String filename;
  FileInputStream inStream;
  BytesMessage bytesMessage;
  int bytesRead;
  int bytesTotalSize = 0;
  byte[] buf1 = new byte[64];

    // write RFH2-Header
    MQRFH2 mqrfh2 = new MQRFH2();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    mqrfh2.setEncoding(CMQC.MQENC_NATIVE);
    mqrfh2.setCodedCharSetId(CMQC.MQCCSI_INHERIT);
    mqrfh2.setFormat(CMQC.MQFMT_NONE);
    mqrfh2.setFlags(0);
    mqrfh2.setNameValueCCSID(1208);

    mqrfh2.setFieldValue(ifCobaString, "OriginatorApplication", "data");
    mqrfh2.setFieldValue(ifCobaString, "Requestor", "ou=data,o=data,o=swift");
    mqrfh2.setFieldValue(ifCobaString, "Responder", "ou=data,o=data,o=swift");
    mqrfh2.setFieldValue(ifCobaString, "Service", "swift.data");
    mqrfh2.setFieldValue(ifCobaString, "RequestType", "data");
    mqrfh2.setFieldValue(ifCobaString, "Compression", "Zip");
    mqrfh2.setFieldValue(ifCobaString, "FileName", "data.zip");
    mqrfh2.setFieldValue(ifCobaString, "FileReference", "data.zip");

    mqrfh2.write(new DataOutputStream(out), CMQC.MQENC_NATIVE, 819);

    byte[] bytesHeader = out.toByteArray();

    // set the input file to be send
    filename = "/opt/mq/TC.SB13.0015484631.zip"; // just a random file that I am trying to send over the message
    inStream = new FileInputStream(filename);

    JMSContext producerContext = connectionFactory.createContext();
    JMSProducer producer = producerContext.createProducer();
    bytesMessage = producerContext.createBytesMessage();

    // add RHF2-header to the message
    bytesMessage.writeBytes(bytesHeader);

    // add payload to the message
    while ((bytesRead = inStream.read(buf1)) != -1) {
      bytesMessage.writeBytes(buf1, 0, bytesRead);
      bytesTotalSize += bytesRead;
      System.out.println("Writing " + bytesRead + " bytes into message..");
    }

    System.out.println("Finished writing " + bytesTotalSize + " bytes into message!");

    producer.send(destination, bytesMessage);
    producerContext.close();
    inStream.close();

}

這是閱讀它的代碼。 FileOutputStream 也成功創建了 WRITE.zip 文件,但是我們沒有關於 header 的任何信息。這有點像翻譯中的遺漏。

我不確定我們是否需要先讀取 x 個字節,即 header,然后再讀取 rest 文件是什么。 但是后來我很困惑,因為這里我們使用文件的整個消息長度,因為 header 沒有任何額外的內容。有點混亂..


// The message is sent as a Message object, so we must determine its type
if (message instanceof TextMessage) {
  System.out.println("-- reading TEXT-message..");
  TextMessage textMessage = (TextMessage) message;

  try {
    System.out.println("-- MyMessageListener received message with payload: " + textMessage.getText());
  } catch (JMSException jmse) {
    System.out.println("JMS Exception in MyMessageListener class (TextMessage)!");
    System.out.println(jmse.getLinkedException());
  }

} else if (message instanceof BytesMessage) {
  System.out.println("-- reading BYTE-message..");
  BytesMessage bytesMessage = (BytesMessage) message;

  try {
    int textLength = (int) bytesMessage.getBodyLength();
    byte[] textBytes = new byte[textLength];
    bytesMessage.readBytes(textBytes, textLength);

    // Save file
    FileOutputStream fos = new FileOutputStream("/opt/mq/WRITE.zip");
    fos.write(textBytes);
    fos.close();

    // Show content of file
    String content = new String(textBytes);
    System.out.println(content);

我認為有人向您提供了一些有關 MQRFH2 消息的錯誤信息。

如果您的應用程序是 JMS 應用程序,那么您就完全走錯了路。

IBM MQ 將 JMS 消息視為 MQRFH2 消息。 因此,當您發送 JMS 應用程序時,您不會創建 MQRFH2 消息。 IBM MQ 會自動為您執行此操作。

其次,您談論的是 XML 消息,但在您的代碼中我看不到任何有關 XML 或 CDATA 的信息。 如果您想將二進制數據作為 XML 消息的一部分發送,則必須使用 CDATA。

如果您想要消息的屬性,則需要將它們放在 JMS 應用程序的“usr”文件夾中。 如果您想使用不同的文件夾,那么您需要使用常規 Java 應用程序(非 JMS),當然,然后構建 MQRFH2 header。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM