本文整理汇总了Scala中com.rabbitmq.client.ConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:Scala ConnectionFactory类的具体用法?Scala ConnectionFactory怎么用?Scala ConnectionFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConnectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MQProducer
//设置package包名称以及导入依赖的类
package mq
import java.io.{ByteArrayOutputStream, ObjectOutputStream}
import com.fasterxml.jackson.databind.ObjectMapper
import twitter4j.Status
import com.rabbitmq.client.{Channel, ConnectionFactory}
object MQProducer {
private val EXCHANGE_TWITTER = "twitter_exchange"
def initiateRabbitMQProducer:Channel = {
val factory = new ConnectionFactory
factory.setHost("localhost")
val connection = factory.newConnection
val channel = connection.createChannel()
channel.exchangeDeclare(EXCHANGE_TWITTER, "topic")
channel
}
def produce(channel: Channel,topic:String ,status:Status): Unit ={
println(status.getText)
channel.basicPublish(EXCHANGE_TWITTER,topic,null,serialise(status))
}
def serialise(value: Any): Array[Byte] = {
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(stream)
oos.writeObject(value)
oos.close
stream.toByteArray
}
}
示例2: MQAgent
//设置package包名称以及导入依赖的类
package com.asto.dmp.mgtevl.mq
import com.asto.dmp.mgtevl.base.Props
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory, MessageProperties}
import org.apache.spark.Logging
object MQAgent extends Logging {
private val connection: Connection = getConnection
private val channel: Channel = connection.createChannel
def getConnection = {
val connectionFactory = new ConnectionFactory
connectionFactory.setHost(Props.get("rabbit_mq_host"))
connectionFactory.setPort(Props.get("rabbit_mq_port").toInt)
connectionFactory.setUsername(Props.get("rabbit_mq_username"))
connectionFactory.setPassword(Props.get("rabbit_mq_password"))
connectionFactory.newConnection
}
def send(message: String) {
val queueName = Props.get("queue_name_online")
//???????????????????????
channel.queueDeclare(queueName, true, false, false, null)
channel.exchangeDeclare(queueName, "direct", true)
channel.queueBind(queueName, queueName, queueName)
channel.basicPublish(queueName, queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"))
}
def close() {
if (Option(channel).isDefined) channel.close()
if (Option(connection).isDefined) connection.close()
}
}
示例3: Hello
//设置package包名称以及导入依赖的类
package com.example
import akka.actor.ActorSystem
import com.rabbitmq.client.ConnectionFactory
import com.github.sstone.amqp.{ChannelOwner, ConnectionOwner, Amqp}
import com.rabbitmq.client.AMQP.Queue
import com.github.sstone.amqp.Amqp._
import scala.concurrent.duration._
object Hello {
def main(args: Array[String]): Unit = {
val system = ActorSystem("mySystem")
val connFactory = new ConnectionFactory()
connFactory.setUri("amqp://guest:[email protected]/%2F")
val conn = system.actorOf(ConnectionOwner.props(connFactory, 1 second))
val channel = ConnectionOwner.createChildActor(conn, ChannelOwner.props())
channel ! DeclareQueue(QueueParameters("my_queue", passive = false, durable = false, exclusive = false, autodelete = true))
}
}
示例4: RabbitQueue
//设置package包名称以及导入依赖的类
package com.fustigatedcat.heystk.engine.queue
import com.fustigatedcat.heystk.engine.EngineAPI
import com.fustigatedcat.heystk.common.normalization.Normalization
import com.rabbitmq.client.{MessageProperties, BuiltinExchangeType, ConnectionFactory}
import org.json4s.native.Serialization.write
import org.slf4j.LoggerFactory
object RabbitQueue {
implicit val formats = org.json4s.DefaultFormats
val logger = LoggerFactory.getLogger(this.getClass)
val config = EngineAPI.config.getConfig("engine.queue.amqp")
val apiExchangeName = config.getString("api.exchange-name")
val toProcessRoutingKey = config.getString("api.routing-key")
val factory = {
val f = new ConnectionFactory()
f.setHost(config.getString("host"))
f.setPort(config.getInt("port"))
f.setVirtualHost(config.getString("vhost"))
f.setUsername(config.getString("user"))
f.setPassword(config.getString("password"))
f
}
val connection = factory.newConnection()
val channel = connection.createChannel()
def postToProcess(normalization : Normalization) = {
val norm = write(normalization)
logger.debug("Publishing normalization {}", norm)
channel.basicPublish(
apiExchangeName,
toProcessRoutingKey,
MessageProperties.PERSISTENT_TEXT_PLAIN,
norm.getBytes
)
}
}
示例5: AMQPMndSetup
//设置package包名称以及导入依赖的类
package com.biosimilarity.lift.lib
import com.rabbitmq.client.AMQP.{Exchange, Queue}
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory}
import scala.collection.mutable
object AMQPMndSetup {
// ht 2016.07.15
// It would be better to use a fixture for setting up the AMQP connection,
// but we'll need to wait until the AMQPMnd implementation doesn't rely on Path-dependent Types
def roundTrip[A](src: String, trgt: String, qName: String, produced: Set[A], consumed: mutable.Set[A]): () => Unit = {
val srcScope: AMQPStdScope[A] = new AMQPStdScope[A]()
val trgtScope: AMQPStdScope[A] = new AMQPStdScope[A]()
val srcQM: srcScope.AMQPQueueHostExchangeM[A] = new srcScope.AMQPQueueHostExchangeM[A](src, qName)
val trgtQM: trgtScope.AMQPQueueHostExchangeM[A] = new trgtScope.AMQPQueueHostExchangeM[A](trgt, qName)
val srcQ: srcScope.AMQPQueue[A] = srcQM.zero[A]
val trgtQ: trgtScope.AMQPQueue[A] = trgtQM.zero[A]
val cleanup: () => Unit = { () =>
val factory: ConnectionFactory = srcScope.factory
val conn: Connection = factory.newConnection()
val channel: Channel = conn.createChannel()
val exchangeDeleteOk: Exchange.DeleteOk = channel.exchangeDelete(qName)
val queueDeleteOk: Queue.DeleteOk = channel.queueDelete(qName + "_queue")
channel.close()
conn.close()
}
trgtQM(trgtQ).foreach { (msg: A) =>
consumed += msg
}
produced.foreach { (v: A) =>
srcQ ! v
}
cleanup
}
}
示例6: AMQPTwistedPairMndSetup
//设置package包名称以及导入依赖的类
package com.biosimilarity.lift.lib
import java.net.URI
import com.rabbitmq.client.AMQP.{Exchange, Queue}
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory}
import scala.collection.mutable
object AMQPTwistedPairMndSetup {
def roundTrip[A](src: URI, trgt: URI, qName: String, produced: Set[A], consumed: mutable.Set[A]): () => Unit = {
val scope: AMQPTwistedPairScope[A] with AMQPBrokerScope[A] with MonadicDispatcherScope[A] = AMQPStdTPS[A](src, trgt)
val qpM: scope.AMQPQueueMQT[A, scope.AMQPAbstractQueue] =
new scope.TwistedQueuePairM[A](qName, "routeroute").asInstanceOf[scope.AMQPQueueMQT[A, scope.AMQPAbstractQueue]]
val qtp: scope.AMQPAbstractQueue[A] = qpM.zero[A]
val cleanup: () => Unit = { () =>
val factory: ConnectionFactory = scope.factory
val conn: Connection = factory.newConnection()
val channel: Channel = conn.createChannel()
val exchangeDeleteOk: Exchange.DeleteOk = channel.exchangeDelete(qName)
val queueDeleteOk: Queue.DeleteOk = channel.queueDelete(qName + "_queue")
channel.close()
conn.close()
}
qpM(qtp).foreach { (msg: A) =>
consumed += msg
}
produced.foreach { (v: A) =>
qtp ! v
}
cleanup
}
val localhost: URI = new URI("amqp", null, "localhost", 5672, "/mult", "routingKey=routeroute", null)
}
示例7: resetMongo
//设置package包名称以及导入依赖的类
package com.biosimilarity.evaluator
import com.biosimilarity.evaluator.api.VersionInfoResponse
import com.biosimilarity.evaluator.distribution.EvalConfigWrapper
import com.mongodb.casbah.Imports.MongoClient
import com.mongodb.casbah.MongoClientURI
import com.rabbitmq.client.{Connection, ConnectionFactory}
import scala.collection.mutable
import scala.sys.process._
import scala.util.Try
package object util extends Helpers with FileHelpers {
def resetMongo(): Unit = {
val dbHost = EvalConfigWrapper.readStringOrElse("dbHost", "localhost")
val dbPort = EvalConfigWrapper.readStringOrElse("dbPort", "27017")
val uri = MongoClientURI(s"mongodb://$dbHost:$dbPort/")
val mongoClient: MongoClient = MongoClient(uri)
val dbNames: mutable.Buffer[String] = mongoClient.databaseNames
dbNames.foreach { (name: String) =>
mongoClient(name).dropDatabase()
}
}
def mongoVersion(): Option[String] =
Try("mongod --version".!!).toOption.map { (s: String) =>
val pattern = """(?m)^db version v(\S+)$""".r
pattern.findFirstIn(s) match {
case Some(pattern(version)) => version
case None => "unknown"
}
}
def rabbitMQVersion(): Option[String] =
Try {
val factory: ConnectionFactory = new ConnectionFactory()
val conn: Connection = factory.newConnection()
val version: String = conn.getServerProperties.get("version").toString
conn.close()
version
}.toOption
def versionInfoResponse: VersionInfoResponse =
VersionInfoResponse(BuildInfo.version, BuildInfo.scalaVersion, mongoVersion().getOrElse("n/a"), rabbitMQVersion().getOrElse("n/a"))
def getOS: String = System.getProperty("os.name")
}
示例8: ProducerActorRabbit
//设置package包名称以及导入依赖的类
package com.bbva.actor
import akka.actor.{Actor, ActorRef, ActorSystem}
import com.bbva.App
import com.bbva.service.{Message, SyncMessage}
import com.github.sstone.amqp.Amqp.{Ok, Publish}
import com.github.sstone.amqp.{ChannelOwner, ConnectionOwner}
import com.rabbitmq.client.ConnectionFactory
import com.typesafe.config.ConfigFactory
import org.slf4j.LoggerFactory
import scala.concurrent.duration._
import scala.util.Failure
class ProducerActorRabbit(connFactory: ConnectionFactory, system: ActorSystem) extends Actor {
val config = ConfigFactory.load()
private def logger = LoggerFactory.getLogger(this.getClass)
createRabbitProducer
def createRabbitProducer: ActorRef = {
val conn = system.actorOf(ConnectionOwner.props(connFactory, 1 second))
ConnectionOwner.createChildActor(conn, ChannelOwner.props())
}
var senderDataService:ActorRef = null
val producer = createRabbitProducer
override def receive: Receive = {
case Message(topic, id, message) =>
App.sumsend()
producer ! Publish(topic,
id,
message.getBytes,
properties = None,
mandatory = true,
immediate = true)
case SyncMessage(topic, id, message) =>
val response = producer ! Publish(topic,
id, message.getBytes,
properties = None,
mandatory = true,
immediate = false)
logger.debug("response: " + response)
case Ok(request, result) => {
App.sum()
if(result != None){
logger.info("ok request " + result)
}
}
case Failure(cause) => {
logger.error("error " + cause)
}
}
}
示例9: MQAgent
//设置package包名称以及导入依赖的类
package com.asto.dmp.alipay.mq
import com.asto.dmp.alipay.base.Props
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory, MessageProperties}
import org.apache.spark.Logging
object MQAgent extends Logging {
private val connection: Connection = getConnection
private val channel: Channel = connection.createChannel
def getConnection = {
val connectionFactory = new ConnectionFactory
connectionFactory.setHost(Props.get("rabbit_mq_host"))
connectionFactory.setPort(Props.get("rabbit_mq_port").toInt)
connectionFactory.setUsername(Props.get("rabbit_mq_username"))
connectionFactory.setPassword(Props.get("rabbit_mq_password"))
connectionFactory.newConnection
}
def send(message: String) {
val queueName = Props.get("queue_name_online")
//???????????????????????
channel.queueDeclare(queueName, true, false, false, null)
channel.exchangeDeclare(queueName, "direct", true)
channel.queueBind(queueName, queueName, queueName)
channel.basicPublish(queueName, queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"))
}
def close() {
if (Option(channel).isDefined) channel.close()
if (Option(connection).isDefined) connection.close()
}
}
示例10: props
//设置package包名称以及导入依赖的类
package com.eigenroute.plumbing
import akka.actor._
import akka.routing.SmallestMailboxPool
import com.rabbitmq.client.{Channel, ConnectionFactory}
import com.spingo.op_rabbit.PlayJsonSupport._
import com.spingo.op_rabbit.{Message, RabbitControl}
import com.thenewmotion.akka.rabbitmq._
import com.typesafe.config.ConfigFactory
import play.api.libs.json._
trait RabbitMQPublisherSubscriber extends PublisherSubscriber {
val actorSystem: ActorSystem
val exchange: String
def props: Props
val nrOfInstances = 10000
val convert: (String) => Option[MessageBrokerMessageType]
val rabbitControl = actorSystem.actorOf(Props[RabbitControl])
override def publish(message: JsValue, routingKey: String): Unit = {
rabbitControl ! Message.exchange(message, exchange, routingKey)
}
val conf = ConfigFactory.load()
val queueName: String = conf.getString("eigenroute-publish-subscribe.queueName")
val factory = new ConnectionFactory()
factory.setHost(conf.getString("op-rabbit.connection.host"))
factory.setPort(conf.getInt("op-rabbit.connection.port"))
factory.setUsername(conf.getString("op-rabbit.connection.username"))
factory.setPassword(conf.getString("op-rabbit.connection.password"))
factory.setVirtualHost(conf.getString("op-rabbit.connection.virtual-host"))
val useSSL = conf.getBoolean("op-rabbit.connection.ssl")
if (useSSL) factory.useSslProtocol()
import concurrent.duration._
val connectionActor: ActorRef =
actorSystem.actorOf(ConnectionActor.props(factory, 3.seconds), "subscriber-connection")
def setupSubscriber(channel: Channel, self: ActorRef) {
val queue = channel.queueDeclare(queueName, true, false, false, new java.util.HashMap()).getQueue
val incomingMessageHandler = actorSystem.actorOf(props.withRouter(SmallestMailboxPool(nrOfInstances = nrOfInstances)))
val consumer = new DefaultConsumer(channel) {
override def handleDelivery(consumerTag: String, envelope: Envelope, properties: BasicProperties, body: Array[Byte]) {
val incomingMessageJson = Json.parse(new String(body))
val maybeMessage = convert(envelope.getRoutingKey).flatMap(_.toMessageBrokerMessage(incomingMessageJson))
maybeMessage.foreach { message => incomingMessageHandler ! message }
}
}
channel.basicConsume(queue, true, consumer)
}
connectionActor ! CreateChannel(ChannelActor.props(setupSubscriber), Some("subscriber"))
def fromBytes(x: Array[Byte]) = new String(x, "UTF-8")
}
开发者ID:shafiquejamal,项目名称:eigenroute-publish-subscribe-minimal-scala,代码行数:60,代码来源:RabbitMQPublisherSubscriber.scala
示例11: getConnection
//设置package包名称以及导入依赖的类
package models
import com.rabbitmq.client.{Connection, ConnectionFactory}
import models.RabbitConfig
def getConnection(): Connection = {
connection match {
case null => {
val factory = new ConnectionFactory()
factory.setHost(RabbitConfig.RABBITMQ_HOST)
factory.setUsername(RabbitConfig.USER_NAME)
factory.setPassword(RabbitConfig.USER_PASSWORD)
factory.newConnection()
}
case _ => connection
}
}
}
示例12: MQAgent
//设置package包名称以及导入依赖的类
package com.asto.dmp.relcir.mq
import com.asto.dmp.relcir.base.Props
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory, MessageProperties}
import org.apache.spark.Logging
object MQAgent extends Logging {
private val connection: Connection = getConnection
private val channel: Channel = connection.createChannel
def getConnection = {
val connectionFactory = new ConnectionFactory
connectionFactory.setHost(Props.get("rabbit_mq_host"))
connectionFactory.setPort(Props.get("rabbit_mq_port").toInt)
connectionFactory.setUsername(Props.get("rabbit_mq_username"))
connectionFactory.setPassword(Props.get("rabbit_mq_password"))
connectionFactory.newConnection
}
def send(queueName: String, message: String) {
//???????????????????????
channel.queueDeclare(queueName, true, false, false, null)
channel.exchangeDeclare(queueName, "direct", true)
channel.queueBind(queueName, queueName, queueName)
channel.basicPublish(queueName, queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"))
}
def close() {
if (Option(channel).isDefined) channel.close()
if (Option(connection).isDefined) connection.close()
}
}
示例13: Messenger
//设置package包名称以及导入依赖的类
package com.example.services
import com.example.models.SystemMessage
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory}
import org.json4s.NoTypeHints
import org.json4s.native.Serialization
import org.json4s.native.Serialization.write
class Messenger {
private val QUEUE_NAME: String = "places"
implicit val formats = Serialization.formats(NoTypeHints)
def send(message: String) : Unit = {
val factory: ConnectionFactory = new ConnectionFactory
factory.setHost("localhost")
val connection: Connection = factory.newConnection
val channel: Channel = connection.createChannel
channel.queueDeclare(QUEUE_NAME, false, false, false, null)
channel.basicPublish("", QUEUE_NAME, null, message.getBytes())
channel.close()
connection.close()
}
def send(message: SystemMessage): Unit = {
send(write(message))
}
}
object Messenger {
def apply() = new Messenger()
}
示例14: MQAgent
//设置package包名称以及导入依赖的类
package com.asto.dmp.xxx.mq
import com.asto.dmp.xxx.base.Props
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory, MessageProperties}
import org.apache.spark.Logging
object MQAgent extends Logging {
private val connection: Connection = getConnection
private val channel: Channel = connection.createChannel
def getConnection = {
val connectionFactory = new ConnectionFactory
connectionFactory.setHost(Props.get("rabbit_mq_host"))
connectionFactory.setPort(Props.get("rabbit_mq_port").toInt)
connectionFactory.setUsername(Props.get("rabbit_mq_username"))
connectionFactory.setPassword(Props.get("rabbit_mq_password"))
connectionFactory.newConnection
}
def send(message: String) {
val queueName = Props.get("queue_name_online")
//???????????????????????
channel.queueDeclare(queueName, true, false, false, null)
channel.exchangeDeclare(queueName, "direct", true)
channel.queueBind(queueName, queueName, queueName)
channel.basicPublish(queueName, queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"))
}
def close() {
if (Option(channel).isDefined) channel.close()
if (Option(connection).isDefined) connection.close()
}
}
示例15: AMQPUtil
//设置package包名称以及导入依赖的类
package com.biosimilarity.lift.lib.amqp
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory}
import scala.util.Try
object AMQPUtil {
def rabbitIsRunning(host: String = "localhost", port: Int = 5672): Boolean =
Try {
val factory: ConnectionFactory = new ConnectionFactory
factory.setHost(host)
factory.setPort(port)
val conn: Connection = factory.newConnection
val channel: Channel = conn.createChannel
channel.close()
conn.close()
}.isSuccess
}