围炉网

一行代码,一篇日志,一个梦想,一个世界

NodeJS 调用 Jar包的简单试用

  • 本次实验代码https://github.com/AdamsLee/node-java-demo

  • 基础环境

    • jdk8

    • python 2.7

    • vs code

  • 安装

    1. npm i -g node-gyp

    2. npm i –g –production windows-build-tools 

      • 本身安装了vs code,可能不用安装

    3. 安装node-java模块

  • 测试用的java 代码,redis地址和密码根据需要进行相应修改

package com.example.demo;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
public class JedisTool {
    private Pipeline pl = null;
    private JedisPoolConfig poolConfig = null;
    private Jedis jr = null;
    private JedisPool jedisPool = null;
    public void initJedis() {
        this.poolConfig = new JedisPoolConfig();
        this.poolConfig.setMaxTotal(4);
        this.poolConfig.setMaxIdle(1);
        this.poolConfig.setMaxWaitMillis(6000);
        this.jedisPool = new JedisPool(poolConfig,"127.0.0.1", 6379, 60, "password");
        this.jr = this.jedisPool.getResource();
        this.pl = this.jr.pipelined();
        System.out.println("initialzing");
    }
    public void usePipeline(int count) {
        if(this.pl == null) {
            System.out.println("Warning: not initialized");
        } else {
            System.out.println("initialized");
        }
        try{
            for(int i=0;i<count;i++) {
                this.pl.set("key" + Integer.toString(i), "abcdef" + Integer.toString(i));
            }
            this.pl.sync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        JedisTool jedisTool = new JedisTool();
        jedisTool.initJedis();
        jedisTool.usePipeline(1);
    }
}

  • 为了方便,把所有的依赖都打包在一个jar包(另外也可以参考https://github.com/joeferner/node-java 在nodejs代码中通过代码逐步把依赖包加入),pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.2.0</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.8.0</version>
        </dependency>           
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/resources/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

  • assembly.xml

<assembly>
    <id>with-dependence</id> <!-- 配置文件的标识,同时生成的jar包名字会把这个文本包含进去 -->
    <formats>
        <format>jar</format> <!-- 打包类型,此处为jar -->
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack> <!-- 是否解压 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

  • mvn assembly:assembly编译jar包

  • 测试用的nodejs代码

let java = require("java"); //引入nodejs的java模块
java.classpath.push("./target/demo-0.0.1-SNAPSHOT-with-dependence.jar"); //导入编写的jar包
let JedisTool = java.import('com.example.demo.JedisTool'); //package.class
let jedisTool = java.newInstance("JedisTool");
jedisTool.initJedis();  //方法调用
setTimeout(function(){ jedisTool.usePipeline(1); }, 2000); 
  • 最后一行js代码使用了setTimeout,因为发现如果直接调用jedisTool.usePipeline(1)时,jedisTool.initJedis()实际上还没有初始化执行完成。因此这里等待两秒,后续还得研究一下其原理,改善一下调用方法,否则其可用性大大降低。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

沪ICP备15009335号-2