简体   繁体   English

带有jar文件的URLCLassLoader的性能

[英]Performance with URLCLassLoader with jar files

I have implemented a plugin infrastructure where I have: 我已经实现了一个插件基础架构,其中:

a> war file : This war file has the class which loads the jar file dynamically from file system. a> war file:此war文件具有从文件系统动态加载jar文件的类。

b> Jar file: These are placed in a file system which will be consumed by the war file above. b> Jar文件:这些文件放置在文件系统中,上面的war文件将使用这些文件系统。

My concern is that the jar file will be invoked a lot per day. 我担心的是,jar文件每天都会被大量调用。 Is my design correct or there is a scope for improvement? 我的设计正确还是有改进的余地? How will it hit the performance of the war file? 它将如何影响战争档案的性能?

War file controller class : War文件控制器类:

public class PluginController {

/**
 * This method is used to invoke plugin class dynamically
 * @param requestBean : This object contains all parameter to
 * complete the request
 * @return responseBean : This object contains the response from vendor
 */
public ResponseBean invokePlugin(RequestBean requestBean){
    URLClassLoader urlClassLoader = null;
    ResponseBean responseBean = null; 
    Map<String, String> parameters = marshalRequest(requestBean);
    try {
        urlClassLoader = new URLClassLoader(new URL[]{new URL("file:///C:/Users/jamju02/Desktop/today/otp.jar")});
        if(urlClassLoader != null){
             Class pluiginClass = urlClassLoader.loadClass("com.ca.pas.plugin.bean.PluginTest");

             if(pluiginClass != null){
                // Create a new instance from the loaded class
                 Constructor<?> constructor = pluiginClass.getConstructor();

                Object classobject = constructor.newInstance();
                    Method method = pluiginClass.getDeclaredMethod("sendOTP", Map.class);
                    method.setAccessible(true);
                    responseBean = (ResponseBean) method.invoke(classobject, parameters);
             }else{
                 System.out.println("class file name not found in jar file");
             }

        }else{

        }



    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }finally{
        try {
            if(urlClassLoader != null){
                urlClassLoader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return responseBean;
}


public Map<String, String> marshalRequest(RequestBean requestBean){

    Map<String, String> parameters = new HashMap<>();
    parameters.put("TXN_ID", requestBean.getTxnId());
    parameters.put("BANK_ID", requestBean.getBankId());
    parameters.put("RANGE_ID", requestBean.getRangeId());
    parameters.put("CARD_NUMBER", requestBean.getCardNumber());
    parameters.put("CARDHOLDER_NAME", requestBean.getCardholderName());
    parameters.put("OTP", requestBean.getOrb().getOtp());
    parameters.put("MOBILE_NUMBER", requestBean.getOrb().getMobileNumber());
    parameters.put("EMAIL", requestBean.getOrb().getEmailAddress());
    return parameters;
}

}

================================================================================ ================================================== =============================

Jar file class has some content to invoke a web service. Jar文件类具有一些可以调用Web服务的内容。

looks good for me. 对我来说很好

I don't see the whole system design but it would be faster if you can save the classloader instance in a object/class variable. 我看不到整个系统的设计,但是如果可以将classloader实例保存在object / class变量中,它将更快。 Maybe it would be possible to design the Controller class a singleton . 也许可以将Controller类设计为单例

by using the basic constructor without any parameter then it is sufficient to just use pluinClass.newInstance() without the constructor. 通过使用不带任何参数的基本构造函数,仅使用不带构造函数的pluinClass.newInstance()就足够了。 Note that you do not need to use setAccessible(true) if the method called is defined as public. 请注意,如果调用的方法定义为public,则无需使用setAccessible(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM