Thursday, 25 May 2017

NodeJS - Call soap webservice in loop

I've work with nodejs for get n millions data from database(sql server) and process on every record by a method of webservice.
I using "mssql" module for connect to my database on SqlServer and "soap" module for connect to Soap WebService.

var express = require('express');
var router = express.Router();
const sql = require('mssql');
var soap = require('soap');

const config = {
    user: '...',
    password: '...',
    server: '.', 
    database: 'sample',
};

var url = 'http://ip:port/sample.asmx?WSDL';

router.use('/startDoing', function (req, res, next) {
    doing();
});

function doing() {
    try {
        soap.createClient(url, function (err, client) {
            sql.connect(config, err => {
                // ... error checks

                const request = new sql.Request()
                request.stream = true
                request.query("select top(100) * from sampleTable"); 

                request.on('recordset', columns => {
                    // Emitted once for each recordset in a query
                })

                request.on('row', row => {
                    // Emitted for each row in a recordset
                    processOnRecord(client, row)
                })

                request.on('error', err => {
                    // May be emitted multiple times
                })

                request.on('done', result => {
                    // Always emitted as the last one
                })
            })

            sql.on('error', err => {
                // ... error handler
            })
        })
    }
    catch (cth) {
        //...
    }
}

function processOnRecord(client, row){
    client.doProcess(args, function (err, result) {
       //connect to this service after n minutes
       //...do process
      //end process after n minutes
    })
}

This code call "processOnRecord" 100 times in first time and go inside into callback of webservice's method after that. I want every time that call this function and in continuedoProcess method, Nodejs go come back into webservice's methodand and do process until end process if connected to webservice.



via Hossein Ganjyar

No comments:

Post a Comment