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;"/>
Def hasnext (): Boolean = {
if (state = = FAILED) //In failed state, additional thread access will be directly abnormal
Throw New IllegalStateException ("Iteratoris in failed state")
State Match {
Case Done = false
Case Ready = true
Case _ = Maybecomputenext ()
}
}
Def maybecomputenext (): Boolean = {
state = FAILED //Reset Status
Nextitem = Some (Makenext ())
if (state = = done) {
false
} Else {
State = Ready
true
}
}
Download
protected def makenext (): Messageandmetadata[k, V] = {
var currentdatachunk:fetcheddatachunk = null
//If we don ' t has an iterator, get one
var localcurrent = Current.get ()
if (localcurrent = = Null | |!localcurrent.hasnext) {
if (Consumertimeoutms < 0)
Currentdatachunk = Channel.take //channel is blockingqueue here will block
Else {
Currentdatachunk = Channel.poll (Consumertimeoutms, Timeunit.milliseconds)
if (Currentdatachunk = = null) {
//Reset State to make the iterator re-iterable
ResetState ()
Throw New Consumertimeoutexception
}
}
//Omit part of the code
}
Kafka high-level consumer multithreaded Access exception