Nodejs calling the jar package

Source: Internet
Author: User
Tags base64

In actual production and development practice, we actually have to deal with many third-party service providers, which include the use of the bulk and online services they provide, but unlike other internet companies, the bank as a traditional enterprise, it has always attached importance to security, many things in the Internet enterprises seem to take for granted, Practice in the bank is not so smooth. For example, recently, because of the third-party system upgrade (the reason for the upgrade is naturally due to national regulatory, policy requirements, confidentiality requirements, otherwise, such as the first big XXX business, strong is his consistent style), resulting in our call service also need to cooperate with the transformation, which involves a request for a field of the addition and decryption, The other side provides a jar package that clearly indicates that subsequent upgrades may only need to update the jar package. And our whole system is. NET implementation, but because of the online field conversion, this seemingly simple, in fact, it is necessary to think about how to achieve and subsequent convenience upgrade. Three options:

A. By converting the jar package to a DLL by IKVM, and then conveniently referencing it, the problem is that subsequent upgrades can be relatively cumbersome.
B. On-Premises server deployment was or tomcat, you only need to develop a simple HTTP interface based on this jar package, the problem is that the cost is too high to increase the operational burden.
C. Develop a simple HTTP interface based on node. js.

I'm going to emulate a jar package, and the interface calls me to use the System.out output, which makes it easier to call in node. js.

 import java.io.unsupportedencodingexception;import java.util.base64;public Class Test {public static  void   main (string[ ] args) throws unsupportedencodingexception { if  (Args!=null  &&args.length = = 1) {System.out.print (Toparse (args[         0 else  {System.out.print ( "Wrong A        RGS "); }} static string Toparse (String str) { return   Base64.geturlencoder (). Encodetostring (Str.getbytes ()); }}

Then write the JS code here, that is, in fact, node. JS in the child_process call to execute the JAR package, note just said the jar package in the method output with System.out, see the following EXEC callback function stdout can access:

functionParserequest (info,res) {logger.debug (info); varexec = require (' child_process '). exec, child; varExec_path = "Java-jar Test.jar" +info.id; vardata; Child= EXEC (Exec_path,function(Error, stdout, stderr) {logger.debug (stdout); Data= "{errcode:0,errmsg: '" + stdout + "'}"Res.writehead (' Content-type ': ' Text/plain;charset=utf-8; '});        Res.end (json.stringify (data)); if(Error!==NULL) {Logger.debug (' stderr: ' +stderr); Logger.debug (' EXEC error: ' +error); Data= "{errcode:500,errmsg: '" + Error + "'}"Res.writehead (' Content-type ': ' Text/plain;charset=utf-8; '});        Res.end (json.stringify (data)); }    });}

After the deployment, we directly through the page access, preliminary see whether we want the results, and then in our system direct HTTP calls, parse JSON field can be.

Borrow "Groove Assembly" pool, the knowledge point is not: node. js in the log log4js,npm after the installation of the sample code, is the following:

var log4js = require (' Log4js '); Log4js.configure ({    appenders: [        {            "file",            "Test.txt",            ' test ', ' Console '        },        {            "console"        }    ],     false }); var logger = Log4js.getlogger (' test '); Logger.setlevel (' Debug ');

JSON processing after JSON2,NPM in node. js

node. JS calls the jar, which involves the Child_process interface in node. js, like here

Logger.debug (info); var exec = require (' child_process '). exec, child; var exec_path = "Java-jar Test.jar" + info.id;

It's such a happy end!

Article Source: 69274818

Nodejs calling the jar package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.