Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fail to put message with express #123

Open
ginlo0628 opened this issue Jun 1, 2021 · 0 comments
Open

fail to put message with express #123

ginlo0628 opened this issue Jun 1, 2021 · 0 comments

Comments

@ginlo0628
Copy link

Hi, I'm trying use node js and express to implement a restful service to put message to IBM MQ,
here is my code:

const express = require( "express" );
const app = express();
const port = 8050; // default port to listen
const mq = require( "ibmmq" );


function formatErr(err) {
    return  "MQ call failed in " + err.message;
  }
  
  function sleep(ms) {
    return new Promise(resolve=![image](https://user-images.githubusercontent.com/11471535/120274900-19bb4480-c2e3-11eb-9646-13fc345fdbd5.png){
      setTimeout(resolve,ms);
    });
  }
  
  function toHexString(byteArray) {
      return byteArray.reduce((output, elem) =>
        (output + ('0' + elem.toString(16)).slice(-2)),
        '');
    }
  function putMessage(hObj) {
  
      var msg = "Hello from Node at " + new Date();
      console.log("putMessage start!");

      var mqmd = new mq.MQMD(); // Defaults are fine.
      var pmo = new mq.MQPMO();
    
      // Describe how the Put should behave
      pmo.Options = MQC.MQPMO_NO_SYNCPOINT |
                    MQC.MQPMO_NEW_MSG_ID |
                    MQC.MQPMO_NEW_CORREL_ID;
    
      mq.Put(hObj,mqmd,pmo,msg,function(err) {
        if (err) {
          console.log(formatErr(err));
        } else {
          console.log("MsgId: " + toHexString(mqmd.MsgId));
          console.log("MQPUT successful");
        }
      });
    }
  function cleanup(hConn,hObj) {
      mq.Close(hObj, 0, function(err) {
        if (err) {
          console.log(formatErr(err));
        } else {
          console.log("MQCLOSE successful");
        }
        mq.Disc(hConn, function(err) {
          if (err) {
            console.log(formatErr(err));
          } else {
            console.log("MQDISC successful");
          }
        });
      });
    }


// define a route handler for the default home page
app.get( "/", ( req, res ) => {
    var MQC = mq.MQC; 
    var qMgr = "QM1";
    var qName = "DEV.QUEUE.1";
    var hConn;
    var cno = new mq.MQCNO();
    cno.Options |= MQC.MQCNO_CLIENT_BINDING;
    var cd = new mq.MQCD();
    cd.ConnectionName = "localhost(1414)";
    cd.ChannelName = "DEV.APP.SVRCONN";
    cno.ClientConn = cd;
    
    var csp = new mq.MQCSP();
    csp.UserId = "app";
    csp.Password = "ssssssss";
    cno.SecurityParms = csp;
    
    
    mq.Connx(qMgr, cno, function(err,hConn) {
       if (err) {
         console.log(formatErr(err));
       } else {
         console.log("MQCONN to %s successful ", qMgr);
    
         // Define what we want to open, and how we want to open it.
         var od = new mq.MQOD();
         od.ObjectName = qName;
         od.ObjectType = MQC.MQOT_Q;
         var openOptions = MQC.MQOO_OUTPUT;
         mq.Open(hConn,od,openOptions,function(err,hObj) {
           if (err) {
             console.log(formatErr(err));
           } else {
             console.log("MQOPEN of %s successful",hObj);
              
              putMessage(hObj);
                        
           }
           cleanup(hConn,hObj);
         });
       }
    });

   // res.send( "Hello world!" );
} );

// start the Express server
app.listen( port, () => {
    console.log( `server started at http://localhost:${ port }` );
} );

The server started ok:
image

but will get error code and exit with put function.
image

Here is the server debug.log:
image

Is anything wrong with this case?

@ginlo0628 ginlo0628 changed the title fail to put message fail to put message with express Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant