Use with IBM MQ classes for JMS

To use the IBM MQ Headers with the IBM MQ classes for JMS, you carry out the same essential steps as for IBM MQ classes for Java. The PCF message can be created and the response parsed in exactly the same way by using the IBM MQ Headers package and the same sample code as for IBM MQ classes for Java.


To send a PCF message using the IBM MQ API, the message payload must be written into a JMS Bytes Message, and sent using the standard JMS APIs. The only consideration is that the message must not contain a JMS RFH2 or any other headers with specific values in the MQMD.

To send a PCF message, complete the following steps. The way in which the PCF message is created, and information is extracted from the response message is the same as for IBM MQ classes for Java (see Use with IBM MQ classes for Java).


Procedure

  1. Create a JMS Queue Destination that represents the SYSTEM.ADMIN.COMMAND.QUEUE. IBM MQ JMS applications send the PCF messages to the SYSTEM.ADMIN.COMMAND.QUEUE, and need access to a JMS Destination object that represents this queue. The Destination must have the following properties set:
    WMQ_MQMD_WRITE_ENABLED = YES
    WMQ_MESSAGE_BODY = MQ
    If we are using WebSphere Application Server, we must define these properties as custom properties on the Destination.To create the destination programmatically from within an application, use the following code:
    Queue q1 = session.createQueue("SYSTEM.ADMIN.COMMAND.QUEUE");   
    ((MQQueue) q1).setIntProperty(WMQConstants.WMQ_MESSAGE_BODY,            
        WMQConstants.WMQ_MESSAGE_BODY_MQ);    
    ((MQQueue) q1).setMQMDWriteEnabled(true);
  2. Convert a PCF message into a JMS Bytes message containing the correct MQMD values. A JMS Bytes message needs to be created, and the PCF Message written to it. A response queue needs to be created, but this needs to have no specific settings.

    The following sample code snippet shows how to create a JMS Bytes Message and write a com.ibm.mq.headers,pcf.PCFMessage object into it. The PCFMessage object (pcfCmd) has previously been built using the IBM MQ Headers package. (Note the package to load the PCFMessage is com.ibm.mq.headers.pcf.PCFMessage).

     // create the JMS Bytes Message    
    final BytesMessage msg = session.createBytesMessage();    
    
    // Create the wrapping streams to put the bytes into the message payload    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();    
    DataOutput dataOutput = new DataOutputStream(baos);
        
    // Set the JMSReplyTo so the answer comes back    
    msg.setJMSReplyTo(new MQQueue("adminResp"));    
    
    // write the pcf into the stream    
    pcfCmd.write(dataOutput);    
    baos.flush();   
    msg.writeBytes(baos.toByteArray());    
    
    // we have taken control of the MD, so need to set all    
    // flags in the MD that we require - main one is the format    
    msg.setJMSPriority(4);   
    msg.setIntProperty(WMQConstants.JMS_IBM_MQMD_PERSISTENCE,       
      CMQC.MQPER_NOT_PERSISTENT);    
    msg.setIntProperty(WMQConstants.JMS_IBM_MQMD_EXPIRY, 300);   
    msg.setIntProperty(WMQConstants.JMS_IBM_MQMD_REPORT,           
        CMQC.MQRO_PASS_CORREL_ID);    
    msg.setStringProperty(WMQConstants.JMS_IBM_MQMD_FORMAT, "MQADMIN");    
    
    // and send the message   
    sender.send(msg);
  3. Send the message, and receive the response using the standard JMS APIs.
  4. Convert the response message into a PCF message for processing. To retrieve the response message and process it as a PCF message, use the following code:
     // Get the message back    
    BytesMessage msg = (BytesMessage) consumer.receive();    
    
    // get the size of the bytes message & read into an array    
    int bodySize = (int) msg.getBodyLength();    
    byte[] data = new byte[bodySize];    
    msg.readBytes(data);    
    
    // Read into Stream and DataInput Stream    
    ByteArrayInputStream bais = new ByteArrayInputStream(data);    
    DataInput dataInput = new DataInputStream(bais);    
    
    // Pass to PCF Message to process   
    PCFMessage response = new PCFMessage(dataInput);

Parent topic: Use the IBM MQ Headers package


Related concepts