我正在试验Akka流和twitter4j-stream。有一连串的推文,它可以正常工作,直到它失败NPE
,这对我来说没有意义。
最初我正在创建client
:
val client: TwitterStream = {
using(TwitterClient.clientFactory.getInstance()) { inst =>
inst.setOAuthConsumer(auth.consumerKey, auth.consumerSecret)
inst.setOAuthAccessToken(new AccessToken(auth.accessTokenKey, auth.accessTokenSecret))
}
}
最后,我希望获得具有以下签名的内容:val stream: Source[TwitterEvent, ShutdownHandler]
因此我创建了一个ReactiveStream Publisher
,它会使用来自Source
的{{1}}客户:
val pub = new Publisher[TwitterEvent] {
override def subscribe(s: Subscriber[_ >: TwitterEvent]): Unit = {
client.addListener(new TwitterListener(s))
client.filter(filter)
}
}
val stream: Source[TwitterEvent, ShutdownHandler] = {
Source.fromPublisher(pub).mapMaterializedValue { nu =>
new TwitterClient.ShutdownHandler(() => client.shutdown())
}
}
所以.filter()
引导这个东西,我收到了一些推文,然后就失败了:
[ERROR] [04/08/2016 15:18:19.301] [System-akka.actor.default-dispatcher-4] [akka://System/user/StreamSupervisor-0/flow-0-0-foreachSink-foreachSink-map] It is illegal to throw exceptions from request(), rule 3.16
akka.stream.impl.ReactiveStreamsCompliance$SignalThrewException: It is illegal to throw exceptions from request(), rule 3.16
at akka.stream.impl.ReactiveStreamsCompliance$.tryRequest(ReactiveStreamsCompliance.scala:111)
...
Caused by: java.lang.NullPointerException
at akka.stream.impl.ReactiveStreamsCompliance$.tryRequest(ReactiveStreamsCompliance.scala:110)
如果重要的是TwitterListener
:
final class TwitterListener(s: Subscriber[_ >: TwitterEvent]) extends StatusAdapter {
override def onStatus(status: Status): Unit = {
if (status != null) {
val message = new TwitterEvent(status)
s.onNext(message)
}
}
override def onException(ex: Exception): Unit = {
s.onError(ex)
}
}