Search code examples
javanode.jsthrift

How should I use nodejs visit thrift TNonblockingServer or to communicate with TNonblockingSocket


I'm trying to use thrift to relalize communication between nodejs client and Java server

Thrift offer different kinds of java server which have been implemented · TSimpleServer · TNonblockingServer · THsHaServer · TThreadedSelectorServer · TThreadPoolServer

I have successfully used nodejs client to call the function in TTSimpleServer and TThreadPoolServer which both use TServerSocket to initialize

TServerSocket serverTransport = new TServerSocket(9090);

CalculatorService.Processor<CalculatorImpl> processor = new CalculatorService.Processor<CalculatorImpl>(
                    new CalculatorImpl());

            TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport).processor(processor);
            args.maxWorkerThreads(100);

            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(
                    serverTransport).processor(processor));

            System.out.println("Starting server on port 9090 ...");
            server.serve();

but when I try to use TNonblockingServer,TThreadedSelectorServer and THaHsServer , I came acrross following error in nodejs client

{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

I realized that this may be caused by TNonblockingSocket, is there any method to use nodejs communicate with TNonblockingSocket

try
        {
            TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(
                    9090);

            CalculatorService.Processor<CalculatorImpl> processor = new CalculatorService.Processor<CalculatorImpl>(
                    new CalculatorImpl());

            TServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(
                    serverTransport).processor(processor));
            System.out.println("Starting server on port 9090 ...");
            server.serve();
        } catch (TTransportException e)
        {
            e.printStackTrace();
        }

my nodejs client code is as follow

 var thrift = require('thrift');

var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var Calculator = require('./gen-nodejs/CalculatorService.js');
var ttypes = require('./gen-nodejs/tutorial_types');

transport = ThriftTransports.TFramedTransport();
protocol = ThriftProtocols.TBinaryProtocol();

var connection = thrift.createConnection("localhost", 9090, {
  transport : transport,
  protocol : protocol
});

connection.on('error', function(err) {
  console.log(err)
});

// Create a Calculator client with the connection
var client = thrift.createClient(Calculator, connection);

client.send_print(1,1, function(err, response) {
      console.log("send_print result:" + response);
    });

Solution

  • Well , I just found the answer This problem is truly caused by TTFramedTransport i found even when I use this in my code

    transport = ThriftTransports.TFramedTransport();
    

    the transport seems not successfully been assigned TFramedTransport

    I just try this way

    transport = require('./node_modules/thrift/framed_transport.js')
    

    it succeeded

    I am not familiar with node.js , maybe I figure out why this happen later