Stress test reading properties with getPropertie() return null in java application

106 views Asked by At

I have a jar that contains a class that, among other things, reads a properties and extracts some properties with getProperty in the constructor.

When I test the jar and I stress it with jmeter by raising parallels threads, only in some cases it returns null for some properties. This doesn't happen when I don't stress it out.

I'm using jdk-14 over windows 10.

This is my source code with some modifications due to copyright.

Grateful in advance for your help !!


public class Connector {

    private static properties;
    private static int port = 0;
    private static String host = null;
    private static int timeOutRead = 0;
    private static int timeOutConnect = 0;
    private static final Logger log = Logger.getLogger(Connector.class);

    public Connector() {
        try {
            Logg logg = new Logg();
            logg.initLoggerProperties();
            
            log.info("Constructor of Connector class");

            properties = readProperties("configurations/conector.properties");
            port = Integer.parseInt(properties.getProperty("port"));
            host = properties.getProperty("host");
            timeOutRead = Integer.parseInt(properties.getProperty("timeOutRead"));
            timeOutConnect = Integer.parseInt(properties.getProperty("timeOutConnect"));
            properties.clear();

            log.info(metodo + "port[" + port + "].");
            log.info(metodo + "host[" + host + "].");
            log.info(metodo + "timeOutRead[" + timeOutRead+ "].");
            log.info(metodo + "timeOutConnect["+ timeOutConnect + "].");
        } catch (Exception ex) {
            log.error(metodo
                    + "[ConectorCIO]::[ConectorCIO]::Se ha producido una exception del tipo:"
                    + ex.getMessage(), ex);
        }
    }
    
    
    private Properties readProperties(String pathAndName)
            throws FileNotFoundException, IOException {

        InputStream is = getClass().getClassLoader().getResourceAsStream(pathAndName);
        Properties properties = new Properties();
        properties.load(is);
        is.close();
        return properties;
    }
    
    
    //others functions
    
}
1

There are 1 answers

1
Bogdan Mart On BEST ANSWER

You need to make your fields non-static. That would solve your problem.

The problem is in following two lines:

properties = readProperties("configurations/conector.properties");
properties.clear();

While first thread is filling properties, other thread could clear it before it's being read.

Also here could other artifacts arise as you are accessing same field from different threads without marking them volatile (this is just estimated description, more could be googled as Java Memory Model (JMM)).

Also, you could consider putting all code of your method in synchronized block. But be aware that no thread would be able to execute code in parallel, and other threads would wait.

P.S. It looks like that Singleton is what you need here.