Spark Streaming 读取RabbitMQ
https://github.com/zlaidandan/java/blob/master/RMQReceiver.cala
private class RMQReceiver[T: ClassTag](
amqpHost: String,
amqpPort: Int,
amqpUsername: String,
amqpPassword: String,
amqpVhost: String,
queueName: String,
storageLevel: StorageLevel) extends Receiver[T](storageLevel) with Logging {
val cf = new ConnectionFactory
cf.setHost(amqpHost)
cf.setPort(amqpPort)
cf.setUsername(amqpUsername)
cf.setPassword(amqpPassword)
cf.setVirtualHost(amqpVhost)
val amqpConnection = cf.newConnection();
val amqpChannel = amqpConnection.createChannel();
val queueingConsumer = new QueueingConsumer(amqpChannel)
val amqpConsumerTag = amqpChannel.basicConsume(queueName, true, queueingConsumer)
var blockPushingThread: Thread = null
def onStart() {
val queue = new ArrayBlockingQueue[ByteBuffer](300)
blockPushingThread = new Thread {
setDaemon(true)
override def run() {
while (true) {
val buffer = queue.take()
store(buffer)
}
}
}
blockPushingThread.start()
while (true) {
val delivery = queueingConsumer.nextDelivery(1L)
if (delivery.getEnvelope().getRoutingKey == "realtime.request") {
val message = delivery.getBody
queue.put(ByteBuffer.allocate(message.length).put(message))
}
}
}
def onStop() {
if (blockPushingThread != null) blockPushingThread.interrupt()
if (amqpChannel != null) {
if (amqpConsumerTag != null) {
amqpChannel.basicCancel(amqpConsumerTag)
}
amqpChannel.close()
}
if (amqpConnection != null) {
amqpConnection.close()
}
}
} 相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
ChaITSimpleLove 2020-10-06
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25