PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-jira/src/main/java/org/apache/camel/component/jira/consumer/NewIssueConsumer.java

https://gitlab.com/matticala/apache-camel
Java | 82 lines | 42 code | 11 blank | 29 comment | 4 complexity | be6ba799c25051af427a52491de62f93 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.jira.consumer;
  18. import java.util.List;
  19. import java.util.Stack;
  20. import com.atlassian.jira.rest.client.domain.BasicIssue;
  21. import org.apache.camel.Exchange;
  22. import org.apache.camel.Processor;
  23. import org.apache.camel.component.jira.JIRAEndpoint;
  24. /**
  25. * Consumes new JIRA issues.
  26. *
  27. * NOTE: We manually add "ORDER BY key desc" to the JQL in order to optimize startup (the latest issues one at a time),
  28. * rather than having to index everything.
  29. */
  30. public class NewIssueConsumer extends AbstractJIRAConsumer {
  31. private final String jql;
  32. private long latestIssueId = -1;
  33. public NewIssueConsumer(JIRAEndpoint endpoint, Processor processor) {
  34. super(endpoint, processor);
  35. jql = endpoint.getJql() + " ORDER BY key desc";
  36. // grab only the top
  37. List<BasicIssue> issues = getIssues(jql, 0, 1, 1);
  38. // in case there aren't any issues...
  39. if (issues.size() >= 1) {
  40. latestIssueId = issues.get(0).getId();
  41. }
  42. }
  43. @Override
  44. protected int poll() throws Exception {
  45. Stack<BasicIssue> newIssues = new Stack<>();
  46. getNewIssues(0, newIssues);
  47. while (!newIssues.empty()) {
  48. BasicIssue newIssue = newIssues.pop();
  49. Exchange e = getEndpoint().createExchange();
  50. e.getIn().setBody(newIssue);
  51. getProcessor().process(e);
  52. }
  53. return newIssues.size();
  54. }
  55. // In the end, we want *new* issues oldest to newest.
  56. private void getNewIssues(int start, Stack<BasicIssue> stack) {
  57. // grab only the top
  58. List<BasicIssue> issues = getIssues(jql, start, 1, 1);
  59. // in case there aren't any issues...
  60. if (issues.size() >= 1) {
  61. long id = issues.get(0).getId();
  62. if (id > latestIssueId) {
  63. stack.push(issues.get(0));
  64. // try again in case multiple new issues exist
  65. getNewIssues(start + 1, stack);
  66. // make sure this happens now, rather than before calling #getNewIssues
  67. latestIssueId = id;
  68. }
  69. }
  70. }
  71. }