Search code examples
javajmsibm-mqspring-jms

Send JMS message to IBM-MQ with MQIIH header


Can someone help me to send a JMS message to IBM-MQ with IIH (MQIIH) headers?

I am using the Spring Boot application with mq-jms-spring-boot-starter version 2.4.5.

When I send a message it always goes with the RFH2 header although it sets StrucId as IIH in the message header.

public void send(Request mqRequest) throws JMSException {
    log.info("sending with message [{}] to queue [{}].", mqRequest.getMessage(), QUEUE_NAME);
    MQQueue destinationQueue = new MQQueue(QUEUE_NAME);
    jmsTemplate.convertAndSend(destinationQueue, mqRequest.getMessage(), message -> {
        log.info("setting standard JMS headers before sending");
       message.setJMSCorrelationID(UUID.randomUUID().toString());
        message.setJMSDestination(new MQQueue(QUEUE_NAME));
        message.setJMSTimestamp(System.nanoTime());
        message.setJMSType("MQIIH");
        message.setStringProperty("Authenticator", "********");
        message.setStringProperty("Format", "********");
        message.setStringProperty("ReplyToFormat", "********");
        message.setIntProperty("CommitMode", '0');
        message.setStringProperty("LTermOverride", "********");
        message.setStringProperty("MFSMapName", "********");
        message.setIntProperty("SecurityScope", 'C');
        message.setStringProperty("StrucId", "IIH ");
        message.setIntProperty("StrucLength", 84);
        message.setIntProperty("Version", 1);
        return message;
    });
}

Solution

  • You can not do this with pure JMS, you have to use the MQIIH helper class:

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import javax.jms.BytesMessage;
    import com.ibm.mq.headers.MQIIH;
    import com.ibm.msg.client.jms.JmsConstants;
    ...
    
    public void send(Request mqRequest) throws JMSException {
        log.info("sending with message [{}] to queue [{}].", mqRequest.getMessage(), QUEUE_NAME);
        MQIIH mqiih = new MQIIH();
        mqiih.setAuthenticator("***");
        // set other MQIIH header values
        ByteArrayOutputStream mqiihBytes = new ByteArrayOutputStream();
        mqiih.write(new DataOutputStream(mqiihBytes));
        String destinationQueue = "queue:///" + QUEUE_NAME + "?targetClient=1";
        jmsTemplate.send(destinationQueue, (MessageCreator) session -> {
            BytesMessage message = session.createBytesMessage();
            message.setStringProperty(JmsConstants.JMS_IBM_FORMAT, "MQIMS");
            message.writeBytes(mqiihBytes.toByteArray());
            message.writeBytes("This Is The Body".getBytes());
            return message;
    
        });
    }
    

    With the queue option targetClient=1 the usual RFH2 header with JMS meta information gets stripped. The message will look like this:

    5724-H72 (C) Copyright IBM Corp. 1994, 2022.
    IBM MQ Queue Load/Unload Utility
    * DMPMQMSG Version:9.3.1.0 Created:Wed Jan 11 08:41:37 2023
    * Qmgr  = QM1
    * Queue = Q1
    
    A VER 2
    A RPT 0
    A MST 8
    A EXP -1
    A FDB 0
    A ENC 273
    A CCS 819
    A FMT MQIMS   
    A PRI 4
    A PER 1
    A MSI 414D5120514D312020202020202020204337BB6301ED0140
    A COI 000000000000000000000000000000000000000000000000
    A BOC 0
    A RTQ                                                 
    A RTM QM1                                             
    A USR mqm         
    A ACC 0000000000000000000000000000000000000000000000000000000000000000
    A AIX 2020202020202020202020202020202020202020202020202020202020202020
    A PAT 28
    A PAN runner.RemoteTestRunner     
    A PTD 20230111
    A PTT 08413426
    A AOX 20202020
    A GRP 000000000000000000000000000000000000000000000000
    A MSQ 1
    A OFF 0
    A MSF 0
    A ORL -1
    T IIH 
    X 00000001000000
    T T
    X 0000011100000000
    T         
    X 00000000
    T                         ***     
    X 00000000000000000000000000000000
    T     This Is The Body
    
    Read    - Files:   0  Messages:1  Bytes:100
    Written - Files:   0  Messages:1  Bytes:100