简体   繁体   中英

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.

b> Jar file: These are placed in a file system which will be consumed by the war file above.

My concern is that the jar file will be invoked a lot per day. 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 :

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.

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. Maybe it would be possible to design the Controller class a singleton .

by using the basic constructor without any parameter then it is sufficient to just use pluinClass.newInstance() without the constructor. Note that you do not need to use setAccessible(true) if the method called is defined as public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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