Spark Streaming 读取RabbitMQ

[url]https://github.com/zlaidandan/java/blob/master/RMQReceiver.cala[/url]

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()
}

}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值