ceid1987
ceid1987

Reputation: 11

How to make SMB connection persistent with jcifs-ng?

I have a Quarkus app that retrieves data from a mysql database.

I am adding a functionality which uses jcifs-ng to read & parse xml files on a shared drive (adding additional data to what is already retrieved from the DB).

I have a SMBConnector.java class defined like this:

@Singleton
public class SMBConnector {

    @ConfigProperty(name = "smb.username")
    private String username;

    @ConfigProperty(name = "smb.password")
    private String password;

    private static final String SERVERNAME = "SD";
    private static final String SHARENAME = "Project";

    private CIFSContext testCtx;

    @PostConstruct
    public void init() {
        SingletonContext baseContext = SingletonContext.getInstance();
        Credentials credentials = new NtlmPasswordAuthenticator(null, username, password);
        this.testCtx = baseContext.withCredentials(credentials);
        try {
            String url = "smb://" + SERVERNAME + "/" + SHARENAME + "/";
            this.rootDir = new SmbFile(url, testCtx);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public InputStream readFileContent(String dirPath) {
        try {
            SmbFile dir = new SmbFile(rootDir, dirPath);
            for (SmbFile file : dir.listFiles()) {
                if (file.getName().startsWith("HPC_SYS") && file.getName().endsWith(".xml")) {
                    return file.getInputStream();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}

This class is injected in the Parser class which itself receives a list of different paths on the drive and calls the readFileContent method for every path.

The problem is that after adding the SMB Connection the entire operation takes about 3 minutes, whereas it took about 20 seconds max before adding this functionality.

I am unsure if I have properly configured the connection so that it persists throughout the application's lifetime or if it is reconnecting every time when the readFileContent method is called.

I am trying to make it so that the application only connects once by accessing the shared root directory between all the paths that we will look for, then read each file by navigating to its specific path. Really any form of optimization would help..

Upvotes: 0

Views: 786

Answers (0)

Related Questions