Close/Destroy 有联系 JAVA

Close/Destroy JIRA connection JAVA

我是 Java 编码的初学者。 下面是代码

public class AddJIRATicketWatcherCommandHandler {

private final JiraFactory jiraFactory;

public void handle(String jiraIssueKey, String watcher) {
    
        log.debug("Adding {} watcher to JIRA issue: {}", watcher, jiraIssueKey);

        final Issue issue = jiraFactory.createClient().getIssueClient().getIssue(jiraIssueKey).claim();
        log.debug("Found JIRA issue: {}", issue.getKey());

        Promise<Void> addWatcherPromise = jiraFactory.createClient().getIssueClient().addWatcher(issue.getWatchers().getSelf() , watcher);
        addWatcherPromise.claim();
 }}




public JiraRestClient createClient() {
    log.debug("Creating JIRA rest client for remote environment");

    URI jiraServerUri = URI.create("");

    jiraServerUri = new URI(StringUtils.removeEnd(jiraConfig.getJiraURI(), "/rest"));

    JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri,
            jiraConfig.getJiraUsername(),
            jiraConfig.getJiraPassword());

    JIRA_LOGGER.info("url=[{}], username=[{}], password=[{}]", jiraServerUri.toString(), jiraConfig.getJiraUsername(), jiraConfig.getJiraPassword());
    log.debug("JIRA rest client created successfully for remote environment");
    return restClient;
}

然而,当我运行声纳曲时。我收到此错误。

使用 try-with-resources 或在“finally”子句中关闭此“JiraRestClient”。

我的理解是完成后关闭连接。但是,我不确定该怎么做。

我尝试用 close() 实现 finally。但是结果还是一样的错误。

尝试使用资源:

    public void handle(String jiraIssueKey, String watcher) {
    try (JiraRestClient restClient = jiraFactory.createClient()) {
        log.debug("Adding {} watcher to JIRA issue: {}", watcher, jiraIssueKey);

        final Issue issue = restClient.getIssue(jiraIssueKey).claim();
        log.debug("Found JIRA issue: {}", issue.getKey());

        Promise<Void> addWatcherPromise = restClient.getIssueClient().addWatcher(issue.getWatchers().getSelf() , watcher);
        addWatcherPromise.claim();

    }
}

最后试试:

public void handle(String jiraIssueKey, String watcher) {
    JiraRestClient restClient = jiraFactory.createClient();
    try {
        log.debug("Adding {} watcher to JIRA issue: {}", watcher, jiraIssueKey);

        final Issue issue = restClient.getIssue(jiraIssueKey).claim();
        log.debug("Found JIRA issue: {}", issue.getKey());

        Promise<Void> addWatcherPromise = restClient.getIssueClient().addWatcher(issue.getWatchers().getSelf() , watcher);
        addWatcherPromise.claim();

    } finally {
        restClient.close();
    }
}