简体   繁体   English

关闭/销毁JIRA连接JAVA

[英]Close/Destroy JIRA connection JAVA

I'm a beginner in Java coding.我是 Java 编码的初学者。 Below is the code下面是代码

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;
}

However, when I ran the sonarqube.但是,当我运行声纳时。 I received this error.我收到了这个错误。

Use try-with-resources or close this "JiraRestClient" in a "finally" clause.使用 try-with-resources 或在“finally”子句中关闭此“JiraRestClient”。

在此处输入图片说明

My understanding is to close the connection once done.我的理解是一旦完成就关闭连接。 But, I'm unsure on how to do that.但是,我不确定如何做到这一点。

I tried to implement finally with close().我尝试最终用 close() 实现。 But the results is still showing the same error.但结果仍然显示相同的错误。

在此处输入图片说明

Try with resources:尝试使用资源:

    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();

    }
}

try finally:最后尝试:

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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