Python使用Jpype调用Java Spring Fat Jar || 使用maven-shade-plugin插件打Spring Java Fat Jar

Spring Fat Jar 打包方式

mavn打jar包常用3中方式:
方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包
方法二:使用maven-assembly-plugin插件打包
方法三:使用maven-shade-plugin插件打包

如果jar包中用到了Spring,则用maven-shade-plugin插件打包,如果在运行时出现读取XML schema错误,原因是Spring Framework的多个jar包中包含相同的文件spring.handlers和spring.schemas,生成一个jar包会互相覆盖。为了避免互相影响,可以使用AppendingTransformer来对文件内容追加合并。
使用maven-shade-plugin插件打包Spring Fat Jar的方式:

>
                >org.apache.maven.plugins>
                >maven-shade-plugin>
                >3.2.1>
                >
                    >
                        >package>
                        >
                            >shade>
                        >
                        >
                            >
                                >
                                    >*:*>
                                    >
                                        >META-INF/*.SF>
                                        >META-INF/*.DSA>
                                        >META-INF/*.RSA>
                                    >
                                >
                            >
                            >
                                >
                                    >weibo.ml.model.storage.StorageServiceMotanApi>
                                >
                                >
                                    >META-INF/spring.handlers>
                                >
                                >
                                    >META-INF/spring.schemas>
                                >
                                >
                                    >META-INF/spring.tooling>
                                >

                                >
                                    >META-INF/services/com.weibo.api.motan.cluster.Cluster>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.cluster.HaStrategy>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.cluster.LoadBalance>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.codec.Codec>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.codec.Serialization>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.config.handler.ConfigHandler>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.filter.Filter>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.proxy.ProxyFactory>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.registry.Registry>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.registry.RegistryFactory>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.rpc.Protocol>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.switcher.SwitcherService>
                                >
                                >
                                    >META-INF/services/com.weibo.api.motan.transport.HeartbeatFactory>
                                >
                                >
                                    >META-INF/services/javax.annotation.processing.Processor>
                                >

                            >
                        >
                    >
                >
            >

Java代码示例

public class StorageServiceMotanApi {
    private static StorageServiceMotan client = null;
	public static Map<String, String> hgetall(String key) throws Exception {
        return getClient().hgetAll(key);
    }
    private static StorageServiceMotan getClient() throws Exception {
        try {
            if (client == null) {
                synchronized (StorageServiceMotan.class) {
                    if (client == null) {
                        System.out.println("==== init motan client====");
                        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:storage_service_client.xml"});
                        client = (StorageServiceMotan) ctx.getBean("storageServiceMotan");
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return client;

    }
    public static void main(String[] args) throws Exception {
        System.out.println(hgetall("k_7_days_001_285986445"));
    }
}

Jpype安装

pip3 install Jpype1

Python Jpype调用示例

import jpype
import os
jarpath = os.path.join(os.path.abspath('.'),
                      	'xxx.jar')  # 第二个参数是jar包的路径
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % (jarpath))  # 启动jvm
try:
    JDClass = jpype.JClass("weibo.ml.model.storage.StorageServiceMotanApi")
    for i in range(1, 10):
        print(JDClass.hgetall("k_7_days_001_285986445"))
except Exception as e:
    print(str(e))
    jpype.shutdownJVM()  # 异常关闭jvm
jpype.shutdownJVM()  # 最后关闭jvm

python运行结果如下:
Python使用Jpype调用Java Spring Fat Jar || 使用maven-shade-plugin插件打Spring Java Fat Jar_第1张图片

你可能感兴趣的:(开发经验分享,maven,java,jar,python)