Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 76ACD200C6C for ; Fri, 5 May 2017 18:05:20 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 75264160BAF; Fri, 5 May 2017 16:05:20 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BBAA1160B97 for ; Fri, 5 May 2017 18:05:19 +0200 (CEST) Received: (qmail 18993 invoked by uid 500); 5 May 2017 16:05:19 -0000 Mailing-List: contact dev-help@gearpump.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@gearpump.incubator.apache.org Delivered-To: mailing list dev@gearpump.incubator.apache.org Delivered-To: moderator for dev@gearpump.incubator.apache.org Received: (qmail 63267 invoked by uid 99); 5 May 2017 09:46:39 -0000 X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.021 X-Spam-Level: X-Spam-Status: No, score=-4.021 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001] autolearn=disabled From: huafengw To: dev@gearpump.incubator.apache.org Reply-To: dev@gearpump.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-gearpump pull request #178: [GEARPUMP-303] add a RabbitMQ sink to ... Content-Type: text/plain Message-Id: <20170505094636.680FADF989@git1-us-west.apache.org> Date: Fri, 5 May 2017 09:46:36 +0000 (UTC) archived-at: Fri, 05 May 2017 16:05:20 -0000 Github user huafengw commented on a diff in the pull request: https://github.com/apache/incubator-gearpump/pull/178#discussion_r114957864 --- Diff: external/rabbitmq/src/main/scala/org/apache/gearpump/external/rabbitmq/RMQSink.scala --- @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.gearpump.external.rabbitmq + +import org.apache.gearpump.Message +import org.apache.gearpump.cluster.UserConfig +import org.apache.gearpump.streaming.sink.DataSink +import org.apache.gearpump.streaming.task.TaskContext +import com.rabbitmq.client.Channel +import com.rabbitmq.client.{Connection, ConnectionFactory} + +class RMQSink(userConfig: UserConfig) extends DataSink{ + + var connection: Connection = null + var channel: Channel = null + var queueName: String = null + + override def open(context: TaskContext): Unit = { + val factory : ConnectionFactory = RMQSink.getConnectionFactory(userConfig) + connection = factory.newConnection + channel = connection.createChannel + if (channel == null) { + throw new RuntimeException("None of RabbitMQ channels are available.") + } + setupQueue() + } + + override def write(message: Message): Unit = { + publish(message.msg) + } + + override def close(): Unit = { + channel.close() + connection.close() + } + + protected def setupQueue(): Unit = { + val queue = RMQSink.getQueueName(userConfig) + if (!queue.nonEmpty) { + throw new RuntimeException("can not get a RabbitMQ queue name") + } + + queueName = queue.get + channel.queueDeclare(queue.get, false, false, false, null) + } + + def publish(msg: Any): Unit = { + msg match { + case seq: Seq[Any] => + seq.foreach(publish) + case str: String => { + channel.basicPublish("", queueName, null, msg.asInstanceOf[String].getBytes) + } + case byteArray: Array[Byte@unchecked] => { --- End diff -- is `unchecked` annotation needed here? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---