Search code examples
javasonarqube

Close/Destroy JIRA connection JAVA


I'm a beginner in Java coding. 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.

enter image description here

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(). But the results is still showing the same error.

enter image description here


Solution

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