Kafka high-level consumer multithreaded Access exception

Source: Internet
Author: User

In the use of Kafka high-level consumer, the use of multi-threaded consumption data times wrong, simple analysis of the reason for download , Consumeriterator will not be blocked when the message and set the internal state to failed, which throws an exception when other threads access it.

Java code 650) this.width=650; "class=" star "src=" Http://woodding2008.iteye.com/images/icon_star.png "alt=" Favorites Code "style= "border:0px;"/>

  1. Def hasnext (): Boolean = {

  2. if         (state = = FAILED) //In failed state, additional thread access will be directly abnormal

  3. Throw New IllegalStateException ("Iteratoris in failed state")

  4. State Match {

  5. Case Done = false

  6. Case Ready = true

  7. Case _ = Maybecomputenext ()

  8. }

  9. }

  10. Def maybecomputenext (): Boolean = {

  11. state = FAILED //Reset Status

  12. Nextitem = Some (Makenext ())

  13. if (state = = done) {

  14. false

  15. } Else {

  16. State = Ready

  17. true

  18. }

  19. }

  20. Download

  21. protected def makenext (): Messageandmetadata[k, V] = {

  22. var currentdatachunk:fetcheddatachunk = null

  23. //If we don ' t has an iterator, get one

  24. var localcurrent = Current.get ()

  25. if (localcurrent = = Null | |!localcurrent.hasnext) {

  26. if (Consumertimeoutms < 0)

  27. Currentdatachunk = Channel.take //channel is blockingqueue here will block

  28. Else {

  29. Currentdatachunk = Channel.poll (Consumertimeoutms, Timeunit.milliseconds)

  30. if (Currentdatachunk = = null) {

  31. //Reset State to make the iterator re-iterable

  32. ResetState ()

  33. Throw New Consumertimeoutexception

  34. }

  35. }

  36. //Omit part of the code

  37. }


Kafka high-level consumer multithreaded Access exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.