Websphere Message Broker – Code samples

ESQL Samples

Creating content of a message from a text / chars

The following code creates new elements of an XML message.

For more details please view the official IBM documentation:
https://www.ibm.com/support/knowledgecenter/en/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ak04950_.htm

Thanks to FJ for finding an issue with the InputProperties.CodedCharSetId

DECLARE testXML CHAR;
SET testXML = ‘Hawker‘;

DECLARE xmlBlob BLOB;
DECLARE options INTEGER BITOR(FolderBitStream, ValidateContent, ValidateValue);
SET xmlBlob = CAST(testXML as BLOB CCSID 1208);
CREATE LASTCHILD OF inputReference DOMAIN(‘XMLNSC’) PARSE (xmlBlob, InputProperties.Encoding, 1208);

Mapping from XML to JSON

The following code maps data from XML to JSON.

This sample came from something I found on GIT hub from Sriharsha :
https://github.com/sriharsha-at-git/Student2Employee_APP

CREATE COMPUTE MODULE CalifornianCompute_Compute
  CREATE FUNCTION Main() RETURNS BOOLEAN
  BEGIN
    CALL CopyMessageHeaders();

    CREATE LASTCHILD OF OutputRoot DOMAIN ‘JSON’;

    SET OutputRoot.JSON.Data.CaliforniaIT.Name.FirstName = InputRoot.XMLNSC.Applicants.Student[vCurrentStudents].Firstname;
    SET OutputRoot.JSON.Data.CaliforniaIT.Name.LastName = InputRoot.XMLNSC.Applicants.Student[vCurrentStudents].Lastname;
    SET OutputRoot.JSON.Data.CaliforniaIT.Age = Environment.Variables.Age

    SET OutputLocalEnvironment.Destination.MQ.DestinationData[1].queueName = CALIFORNIA_LABEL||’.EDU’;
    RETURN TRUE;
  END;
END MODULE;

Throwing a user defined exception

The following code throws a user defined exception.

The original sample comes from this project:
https://github.com/sriharsha-at-git/Generic-ESQL-Utilities/blob/master/exceptions.esql

CREATE PROCEDURE throw(IN exceptionText CHAR) — Throws an exception
BEGIN
  THROW USER EXCEPTION Message 3012 VALUES( exceptionText );
END;