def main(opts): # set up our channel conn_factory = ConnectionFactory() conn_factory.setUri(config['RABBITMQ_URI']) conn = conn_factory.newConnection() channel = conn.createChannel() channel.queueDeclare(opts.queue_name, False, False, False, None) channel.basicQos(1) # tells the channel we're only going to deliver one response before req acknowledgement workers = [PdfExtractor(channel, opts) for i in xrange(opts.workers)] log.info("creating pool with %d threads" % opts.workers) tpool = Executors.newFixedThreadPool(opts.workers) log.info("executing threads") futures = tpool.invokeAll(workers) log.info("shutting down thread pool") tpool.shutdown() try: if not tpool.awaitTermination(50, TimeUnit.SECONDS): log.info("thread pool not shutting down; trying again") tpool.shutdownNow() if not tpool.awaitTermination(50, TimeUnit.SECONDS): log.error("Pool did not terminate") except InterruptedException: log.info("exception during thread pool shutdown; trying again") tpool.shutdownNow() Thread.currentThread().interrupt()
def createRabbitConnectionFactory(host): rabbitConnectionFactory = ConnectionFactory() rabbitConnectionFactory.setUsername( grinder.properties.getProperty("rabbit.username")) rabbitConnectionFactory.setPassword( grinder.properties.getProperty("rabbit.password")) rabbitConnectionFactory.setVirtualHost( grinder.properties.getProperty("rabbit.virtualHost")) rabbitConnectionFactory.setHost(host) rabbitConnectionFactory.setPort(5672) return rabbitConnectionFactory
def initialize(self): self.factory = ConnectionFactory() if self.networkRecoveryInterval != None: self.factory.setNetworkRecoveryInterval(self.networkRecoveryInterval) if self.automaticRecoveryEnabled != None: self.factory.setAutomaticRecoveryEnabled(self.automaticRecoveryEnabled) if self.topologyRecoveryEnabled != None: self.factory.setTopologyRecoveryEnabled(self.topologyRecoveryEnabled) if self.uri != None: logger.info("Initializing RabbitMQ with uri: %s", self.uri) self.factory.setUri(self.uri) self.connection = self.factory.newConnection() else: logger.info("Initializing RabbitMQ") self.addresses.append(Address(self.host, self.port)) if (self.username != None): self.factory.setUsername(self.username) if (self.password != None): self.factory.setPassword(self.password) if (self.virtualhost != None): self.factory.setVirtualHost(self.virtualhost) self.connection = self.factory.newConnection(self.addresses) self.channel = self.connection.createChannel() if (self.queue_name != None): self.passive = False self.durable = False self.exclusive = False self.autodelete = False self.declareQueue = False if "passive" in self.config: self.passive = self.config["passive"] if "durable" in self.config: self.durable = self.config["durable"] if "exclusive" in self.config: self.exclusive = self.config["exclusive"] if "declareQueue" in self.config: self.declareQueue = self.config["declareQueue"] if self.declareQueue: logger.info("Declaring queue %s", self.queue_name) self.channel.queueDeclare(self.queue_name, self.passive, self.durable, self.exclusive, None) else: logger.info("Binding to queue %s, with exchange '%s' and routingKey '%s'", self.queue_name, self.exchange, self.routingKey) self.channel.queueBind(self.queue_name, self.exchange, self.routingKey)