我有一个项目,给我一个ID,然后使用该ID查找文件路径并对其进行处理...这些文件位于各种装入的驱动器上,因此我正在使用SMBJ Java库来访问它们。

我遇到的问题是某些文件(大多数)正在使用DFS挂载点...现在,这本身本身就不是问题,但显然SMBJ库似乎为每个不同的DFS创建嵌套会话。位置。因此,即使我在读取完文件后关闭了实际的FILE,DiskSession对象也将保留所有这些嵌套的会话……最终还是通过DFS配置设置或通过这些库,我碰到了一些问题并停止允许创建更多会话。

我正在处理数十万条记录,而“崩溃”似乎发生在正在处理的500条记录(会话)附近。我看不到任何明显可见的代码来显式关闭这些嵌套会话。.实际上,我从DiskShare对象的外部完全看不到它们的外部访问。

我缺少某种设置来最大化其保持的会话吗?除了我自己管理某种计数器,关闭和重新打开会话/连接之外,我无所适从。

有人知道我在这里想念的吗?

代码如下:

public class Smb {

private static SMBClient client;
private static String[] DFSMounts = {"DFS1","dfs1"};
private static final Logger Log = LoggerFactory.getLogger(Smb.class);
private static HashMap<String,DiskShare> shares = new HashMap<>();
private static HashMap<String,Connection> connections = new HashMap<>();
private static HashMap<Connection,Session> sessions = new HashMap<>();

private synchronized static SMBClient getClient(){
    if (client == null){
        SmbConfig cfg = SmbConfig.builder().withDfsEnabled(true).build();
        client = new SMBClient(cfg);
    }
    return client;
}

private synchronized static Connection getConnection(String realDomainName) throws IOException{

    Log.info("DOMAIN NAME "+realDomainName);
    Connection connection = (connections.get(realDomainName) == null) ? client.connect(realDomainName) : connections.get(realDomainName);
    if(!connection.isConnected()) {
        connection.close();
        sessions.remove(connection);
        connection = client.connect(realDomainName);

    }
    // connection = client.connect(realDomainName);
    connections.put(realDomainName,connection);
    return connection;


}

private synchronized static Session getSession(Connection connection,SMBClient client){

    Session session = sessions.get(connection);
    if(session==null) {
        PropertiesCache props = PropertiesCache.getInstance();
        String sambaUsername = props.getProperty("smb.user");
        String sambaPass = props.getProperty("smb.password");
        String sambaDomain = props.getProperty("smb.domain");
        Log.info("CLIENT " + client);

        session = (sessions.get(connection) != null) ? sessions.get(connection) : connection.authenticate(new AuthenticationContext(sambaUsername, sambaPass.toCharArray(), sambaDomain));

        sessions.put(connection, session);
    }
    return session;
}

@SuppressWarnings("UnusedReturnValue")
public synchronized static DiskShare getShare(String domainName, String shareName) throws SmbException
{
    DiskShare share = shares.get(domainName+"/"+shareName);
    if((share!=null)&&(!share.isConnected())) share=null;
    if(share == null){
        try {


            PropertiesCache props = PropertiesCache.getInstance();
            String sambaUsername = props.getProperty("smb.user");
            String sambaPass = props.getProperty("smb.password");
            String sambaDomain = props.getProperty("smb.domain");
            String dfsIP = props.getProperty("smb.sambaIP");

            SMBClient client = getClient();

            String realDomainName = (Arrays.stream(DFSMounts).anyMatch(domainName::equals)) ? dfsIP: domainName;
            Connection connection = getConnection(realDomainName);
            Session session = getSession(connection,client);

            share = (DiskShare) session.connectShare(shareName);
            shares.put(domainName+"/"+shareName,share);
        }
        catch (Exception e){
            Log.info("EXCEPTION E "+e);
            Log.info("EX "+e.getMessage());

            throw new SmbException();
        }

    }
    return(share);

}

public static String fixFilename(String filename){
    String[] parts = filename.split("\\\\");
    ArrayList<String> partsList = new ArrayList<>(Arrays.asList(parts));
    partsList.remove(0);
    partsList.remove(0);
    partsList.remove(0);
    partsList.remove(0);
    return String.join("/",partsList);

}


public static File open(String filename) throws SmbException {
    String[] parts = filename.split("\\\\");
    String domainName = parts[2];
    String shareName = parts[3];
    DiskShare share = getShare(domainName,shareName);
    Set<SMB2ShareAccess> s = new HashSet<>();
    s.add(SMB2ShareAccess.ALL.iterator().next());
    filename = fixFilename(filename);
    return(share.openFile(filename, EnumSet.of(AccessMask.GENERIC_READ), null, s,  SMB2CreateDisposition.FILE_OPEN, null));
}


}

这是使用OPEN的方式(显示使用后将关闭文件):

String filename = documents.get(0).getUNCPath();
            try (File f = Smb.open(filename)){

               Process the file code...

                f.closeSilently();
            }


和:

    while(i.hasNext()){
        String filename =  (String)i.next();
        Log.info("FILENAME "+filename);
        try(File f = Smb.open(filename)){

           Process the file stuff here


        }
    }

最佳答案

我为SMBJ创建了PR,从而改变了这种情况。它将嵌套会话重用于同一主机。我本人已成功使用它来避免出现完全相同的问题。 https://github.com/hierynomus/smbj/pull/489

07-27 21:14