Example #1
0
File: app.js Project: gatinul/blog
module.exports = app => {
  // 在这里写你的初始化逻辑
  app.beforeStart(() => {
    const directory = path.join(app.config.baseDir, 'app/lib');
    app.loader.loadToApp(directory, 'lib');
  });
  amqp.connect('amqp://localhost')
    .then(function(conn) {
      return conn.createChannel();
    }).then(function(ch) {
      return ch.assertQueue(q, { durable: false }).then(function(ok) {
        console.log(' [*] Waiting for messages in %s. To exit press CTRL+C', q);
        return ch.consume(q, async function(msg) {
          if (msg !== null) {
            console.log(' [x] Received %s', msg.content.toString('utf8'));
            const es = app.lib.elasticsearch;
            const result = await new es().analysis(msg.content.toString('utf8').replace(/{/g, '').replace(/}/g, ''));
            if (!result.success) {
              app.logger.error(`报错信息:${result.message}\n 消息队列:${msg.content}`);
            }
            ch.ack(msg);
          }
        });
      });
    });
};
function connectAndSend(msg){

  //For testing, connecting to a real IP address
  //amqp.connect('amqp://*****:*****@46.101.46.152:5672').then(function(conn) {


    // FOR DOCKER WHEN A LINK IS CALLED rabbit
  amqp.connect('amqp://' + 'rabbit' + ':' + process.env.RABBIT_PORT_5672_TCP_PORT).then(function(conn) {

    return when(conn.createChannel().then(function(ch) {
      var q = 'hello';
      //var msg = 'Hello World!';

      var ok = ch.assertQueue(q, {durable: false});
      
      return ok.then(function(_qok) {

            ch.sendToQueue(q, new Buffer(msg));
            console.log(" [x] Sent '%s'", msg);
    
        return ch.close();
      });
    })).ensure(function() { conn.close(); });;
  }).then(null, console.warn);

  
}
Example #3
0
    app.get("/fractals", function(req, res) {
        amqp.connect(app.get("amqp_connection")).then(function(conn) {
            return when(conn.createChannel().then(function(ch) {
                var answer = defer();
                var corrId = uuid.v4();

                function maybeAnswer(msg) {
                    if (msg.properties.correlationId === corrId) {
                        answer.resolve(msg.content.toString());
                    }
                }

                var ok = ch.assertQueue('', {exclusive: true}).then(function(qok) { return qok.queue; });

                ok = ok.then(function(queue) {
                    return ch.consume(queue, maybeAnswer, {noAck: true}).then(function() { return queue; });
                });

                ok = ok.then(function(queue) {
                    ch.sendToQueue(app.get("amqp_queue_list"), new Buffer("list"), { correlationId: corrId, replyTo: queue });
                    return answer.promise;
                });

                return ok.then(function(message) {
                    return res.send(JSON.parse(message));
                });
            })).ensure(function() { conn.close(); });
        }).then(null, console.warn);
    });
Example #4
0
export default async function publsiher(input) {
  let validate = Joi.validate(
    input,
    Joi.object().keys({
      title: Joi.string().required().
        description(`publisher group title`),
      description: Joi.string().required().
        description(`Publisher group description`),
      connectionString: Joi.string().required().
        description(`amqp connection string`),
      exchangePrefix: Joi.string().default('').
        description(`default exchange prefix to use`)
    })
  );

  if (validate.error) throw error;
  let opts = validate.value;

  let conn = await amqplib.connect(opts.connectionString);
  let channel = await conn.createConfirmChannel();

  return new Publisher({
    title: opts.title,
    description: opts.description,
    exchangePrefix: opts.exchangePrefix,
    conn,
    channel
  });
}
 createTransport(args) {
   return Amqp.connect(args.url, args.socketOptions)
     .then((conn) => conn.createChannel())
     .then((channel) => {
       var ex = args.exchange;
       channel.prefetch(args.channels.prefetch);
       return Promise.all([
         channel,
         channel.assertExchange(ex.name, ex.type, ex.options),
         this.utils.resolve_pins(args)
       ]);
     })
     .spread((channel, exchange, pins) => {
       var topics = Amqputil.resolveListenTopics(pins);
       var qact = args.queues.action;
       var queue = _.trim(args.name) || Amqputil.resolveListenQueue(pins, qact);
       return channel.assertQueue(queue, qact.options)
         .then((q) => Promise.map(topics,
           (topic) => channel.bindQueue(q.queue, exchange.exchange, topic))
         )
         .then(() => {
           return {
             channel,
             exchange: exchange.exchange,
             queue
           };
         });
     });
 }
Example #6
0
export function applyAmqpCtl(io, { amqpUrl, amqpQueue }) {

    amqp.connect(amqpUrl)
        .then(conn => conn.createChannel())
        .then(ch => {
            ch.assertQueue(amqpQueue, { durable: false });

            console.log(
                chalk.yellow(
                    `Waiting for messages in ${amqpQueue}`
                )
            );

            ch.consume(
                amqpQueue,
                msg => {
                    try {
                        const { event, model, data } = JSON.parse(msg.content.toString());
                        if (appEvents[event]) {
                            io.sockets.emit(event, {
                                model,
                                data
                            });
                        }
                    } catch (e) {
                        console.error(
                            chalk.red('corrupt message', e)
                        );
                    }
                },
                { noAck: true }
            );

        }).catch(console.warn);
}
Example #7
0
    app.post("/fractals", function(req, res) {
        var fractal = req.body.fractal;
        if (!req.body.fractal) {
            return res.send(409,  { error: "Unable to parse fractal object" });
        } else if (!req.body.fractal.name) {
            return res.send(409,  { error: "Fractal name property is required" });
        } else if (!req.body.fractal.settings) {
            return res.send(409,  { error: "Fractal settings property is required" });
        }

        var fractal = {
            type: "default",
            created: new Date(),
            state: "saving",
            status: "queued for fractal generation",
            name: req.body.fractal.name,
            notes: req.body.fractal.notes,
            settings: req.body.fractal.settings,
            id: uuid.v4()
        }

        amqp.connect(app.get("amqp_connection")).then(function(conn) {
            var ok = conn.createChannel().then(function(ch) {
                ch.assertQueue(app.get("amqp_queue_save"));
                ch.assertQueue(app.get("amqp_queue_generate"));

                var fractal_buffer = new Buffer(JSON.stringify(fractal));
                ch.sendToQueue(app.get("amqp_queue_save"), fractal_buffer);
                ch.sendToQueue(app.get("amqp_queue_generate"), fractal_buffer);
            });
            return ok;
        }).then(null, console.warn);
        res.send(202, { fractal: fractal });
    });
Example #8
0
 this.getEndpointConfigs().then((endpointConfigs) => {
   const connectionParams = endpointConfigs.connection;
   if (this._amqpConnection !== undefined) {
     resolve(this._amqpConnection);
   } else {
     // TODO if ssl change connections string and connection parameters
     let connectionString = '';
     if (this._ssl) {
       connectionString = `${this._sslProtocol}://${connectionParams.deviceId || connectionParams.client}:` +
         `${connectionParams.secret}@${connectionParams.host}:` +
         `${connectionParams.protocols.amqp.sslPort}/${connectionParams.vhost.replace('/', '%2f')}`;
       connectionOpts = merge(connectionOpts, this._sslOpts);
     } else {
       connectionString = `${this._protocol}://${connectionParams.deviceId || connectionParams.client}:` +
         `${connectionParams.secret}@${connectionParams.host}:` +
         `${connectionParams.protocols.amqp.port}/${connectionParams.vhost.replace('/', '%2f')}`;
     }
     amqp.connect(connectionString, connectionOpts).then((conn) => {
       conn.on('error', (err) => {
         reject(err);
       });
       conn.on('blocked', (reason) => {
         console.warn(reason); // eslint-disable-line no-console
       });
       conn.on('unblocked', (reason) => {
         console.warn(reason); // eslint-disable-line no-console
       });
       this._amqpConnection = conn;
       resolve(this._amqpConnection);
     }).catch((reason) => {
       reject(reason);
     });
   }
 }).catch((reason) => {
Example #9
0
    return new Promise(function (resolve, reject) {

        amqp.connect(configuration.RabbitMQ[process.env.NODE_ENV].host).then(function (conn) {
            conn.createConfirmChannel().then(function (ch) {
                channel = ch;

                var q = configuration.RabbitMQ.jobsQueue;
                var ok = channel.assertQueue(q, {durable: true, arguments: {"x-max-priority": 10}});
                ok = ok.then(function () {
                    channel.prefetch(1);
                });
                ok = ok.then(function () {
                    channel.consume(q, rabbitMQ.onJob, {noAck: false});
                    console.log(" [*] Jobs queue created");


                    var q = configuration.RabbitMQ.resultQueue;
                    var ok = channel.assertQueue(q, {durable: true, arguments: {"x-max-priority": 10}});

                    ok.then(function () {
                        console.log(" [*] Result queue created");
                        resolve();
                    });

                });

            });
        });
    });
Example #10
0
 function init() {
   var d = when.defer();
   amqp.connect(connectionString)
     .then(function(con) {
       connection = con;
       return when.all([con.createChannel(), con.createChannel()]);
     })
     .then(function(channels) {
       publisher = channels[0];
       consumer = channels[1];
       return when.all([
         publisher.assertQueue(service.in_queue),
         publisher.assertQueue(service.out_queue)
       ]);
     })
     .then(function() {
       if (ENV == 'test') {
         return when.all([
           publisher.purgeQueue(service.in_queue),
           publisher.purgeQueue(service.out_queue)
         ]);
       }
     })
     .catch(console.error)
     .done(function() {
       d.resolve();
     });
   return d.promise;
 }
Exchanges.prototype.connect = function(options) {
  options = _.defaults(options || {}, this._options);

  // Check we have a connection string
  assert(options.connectionString, "ConnectionString must be provided");
  assert(options.validator instanceof Validator,
         "An instance of base.validator.Validator must be given");

  // Clone entries for consistency
  var entries = _.cloneDeep(this._entries);

  // Create connection
  var conn = null;
  var channel = null;
  return amqplib.connect(options.connectionString).then(function(conn_) {
    conn = conn_;
    return conn.createConfirmChannel();
  }).then(function(channel_) {
    channel = channel_;

    return Promise.all(entries.map(function(entry) {
      var name = options.exchangePrefix + entry.exchange;
      return channel.assertExchange(name, 'topic', {
        durable:      options.durableExchanges,
        internal:     false,
        autoDelete:   false
      });
    }));
  }).then(function() {
    return new Publisher(conn, channel, entries, options);
  });
};
Example #12
0
  load: function(Options, PluginContext) {
    return rabbit.connect(Options.url)
      .then(function(connection){
        return connection
      })

  },
Example #13
0
function *initCallback(rmq){
  let connString = rmq.config.CONNECTION ? rmq.config.CONNECTION.url : '';
  let opts = rmq.config.CONNECTION && rmq.config.CONNECTION.ssl && rmq.config.CONNECTION.ssl.caFile ?
              {ca:[fs.readFileSync(rmq.config.CONNECTION.ssl.caFile)]} :
                {};
  rmq.connection = yield amqp.connect(connString, opts);
  rmq.configBase.connection = rmq.connection;

  if(rmq.config.CONSUME_QUEUE){
    yield _.map(rmq.config.CONSUME_QUEUE, function(data){
      return consumeQueue(rmq, data);
    });
  }

  if(rmq.config.BIND_EXCHANGE){
    yield _.map(rmq.config.BIND_EXCHANGE, function(data){
      return bindExchange(rmq, data);
    });
  }

  if(rmq.config.BIND_QUEUE){
    yield _.map(rmq.config.BIND_QUEUE, function(data){
      return bindQueue(rmq, data);
    });
  }
}
Example #14
0
(function() {
  'use strict';

  let open = amqp.connect('amqp://192.168.99.100:32773');
  class Receiver extends EventEmitter {};
  let receiver = new Receiver();

  open.then((conn) => {
    conn.createChannel().then((channel) => {
      channel.assertQueue(EXCHANGE);
      channel.consume(EXCHANGE, (msg) => {
        if (msg) {
          let text = msg.content.toString();
          let json = JSON.parse(text);
          // console.log(text);
          channel.ack(msg);
          receiver.emit('msg', json);
        }
      });
    });
  });

  module.exports = receiver;

}());
Example #15
0
co(function* () {
  // connection errors are handled in the co .catch handler
  const conn = yield amqp.connect('amqp://localhost');

  // try catch will throw any errors from the yielding the following promises to the co .catch handler
  try {
    const q = 'hello';
    const msg = 'Hello World!';

    // use a confirm channel so we can check the message is sent OK.
    const channel = yield conn.createConfirmChannel();

    yield channel.assertQueue(q);

    channel.sendToQueue(q, Buffer.from(msg));

    // if message has been nacked, this will result in an error (rejected promise);
    yield channel.waitForConfirms();

    console.log(" [x] Sent '%s'", msg);

    channel.close();
  }
  catch (e) {
    throw e;
  }
  finally {
    conn.close();
  }

}).catch(err => {
Example #16
0
        function sendCallBack(callBackMsg,msgRecv) {
            amqpSend.connect(rmqURI).then(function (connSend) {
                return when(connSend.createChannel().then(function (chSend) {
                    var ok = when.all([
                        chSend.assertQueue(sendQ, {durable: false}),
                        chSend.assertExchange(rmqExch),
                        chSend.bindQueue(sendQ, rmqExch, sendRouterKey)
                    ]);
                    return ok.then(function () {
                        console.log(" [x] connected to sendQ")
                        var msgSend = JSON.stringify(callBackMsg);
                        console.log(" [x] msgSend is '%s'", msgSend)

                        var corrId = msgRecv.properties.correlationId;
                        console.log(" [x] corrId is '%s'", corrId)
                        chSend.sendToQueue(sendQ, new Buffer(msgSend), {correlationId: corrId});
                        console.log(" [x] Sent to new Q");
                        return chSend.close();
                    });
                })).ensure(function () {
                    connSend.close();
                    console.log(" [x] Done");
                    chRecv.ack(msgRecv);
                });
            }).then(null, console.warn);
        }
.then(function () {
	
	// Connect AMQP:
	var amqpServer = amqp.connect('amqps://' + cdxgc_gen_args.amqp_host + ':' + cdxgc_gen_args.amqp_port, AMQP_OPTS);
	
	amqpServer.then(function (amqpConn) {
		// Setup signals:
		process.on('SIGINT', function () {
			logger.info('SIGNAL: SIGINT caught: Closing connection.');
			clearInterval(CYCLE_TIMER);
			amqpConn.close();
			printCounters(COUNTERS);
			process.exit(1); // May need to kick out.
		});
		process.on('SIGTERM', function () {
			logger.info('SIGNAL: SIGTERM caught: Closing connection.');
			clearInterval(CYCLE_TIMER);
			amqpConn.close();
			printCounters(COUNTERS);
			process.exit(1); // May need to kick out.
		});
		
		return amqpConn.createChannel().then(function(ch) {
			coreChannel = ch;
			var ok = ch.assertExchange(AMQP_EXCHANGE, 'topic', {durable: false});
			return ok.then(function() {
				CYCLE_TIMER = setInterval(mainTaskExecutor,cdxgc_gen_args.cycleTime);
			});
		});
		
	}).then(null,function (err) {
		logger.error('AMQP Error :: '+ err);
	});
});
Example #18
0
AmqpPublisher.prototype.connect = function AmqpPubConnect(client) {
  var self = this;

  this.connectionP = amqp.connect(this.config.url).
    then(function (connection) {
      console.log('AmqpPubConnect: Got connection!');
      self.connection = connection;
      return connection.createChannel()
    }).
    then(function (channel) {
      console.log('AmqpPubConnect: Got channel!');

      self.channel = channel;

      var exchange = self.config.exchange;
      console.log('AmqpPubConnect: Connecting to exchange "%s"', exchange);

      return self.channel.assertExchange(exchange, 'topic', {
        passive: false,
        durable: true,
        autoDelete: false
      });
    }).
    then(function () {
      console.log('AmqpPubConnect: Exchange open!');
      self.exchangeOpen = true;
    }).
    catch(function (wut) {
      console.log('Halp! Something failed: %s', util.inspect(wut));
    });
};
Example #19
0
function rabbitReconnect() {
    amqplib.connect(config.parameters.rabbitmq_url, {heartbeat: 60}).then(function (conn) {
        rabbitConnection = conn;

        conn.on("error", function (err) {
            console.log("[rabbit]\tconnection error, reconnecting (%s)", JSON.stringify(err));
            rabbitConnection = false;
            rabbitChannel = false;
            rabbitReconnect();
        });

        conn.createChannel().then(function (channel) {
            console.log("[rabbit]\tconnected");
            rabbitChannel = channel;

            channel.on("error", function (err) {
                console.log("[rabbit]\tchannel error, reconnecting (%s)", JSON.stringify(err));
                rabbitConnection = false;
                rabbitChannel = false;
                rabbitReconnect();
            });

            channel.assertQueue("twitter.tweet", { exclusive: true }).then(function () {
                channel.bindQueue("twitter.tweet", "twitter", "twitter.tweet").then(function () {
                    channel.consume("twitter.tweet", handleReceivedData, { noAck: false });
                });
            });
        });
    }, function (err) {
        console.error("[rabbit]\t%s", JSON.stringify(err));

        setTimeout(rabbitReconnect, 60000);
    });
}
Example #20
0
		this.isReady = (async () => {
			const connect = await amqp.connect(this.options.url)
			const channel = await connect.createChannel()

			this.connect = connect
			this.channel = channel
		})()
    async start(onIncomingMessage) {
        log.info('start');

        let options = this._options;
        let connection = await amqp.connect(options.url);
        log.info('createChannel');
        this._channel = await connection.createChannel();
        log.info('assertExchange ', options.exchange);
        await this._channel.assertExchange(options.exchange, options.type, { durable: true });
        log.info('assertQueue name: ', options.queueName);
        let result = await this._channel.assertQueue(options.queueName, { exclusive: false });

        this._queue = result.queue;
        let routingKeys = options.routingKeys || [options.queueName];
        log.info('assertQueue keys ', routingKeys);
        for(let routingKey of routingKeys){
            log.info('bindQueue routingKey ', routingKey);
            await this._channel.bindQueue(this._queue, options.exchange, routingKey);
        }

        log.info('prefetch and consume');
        this._channel.prefetch(1);
        await this._channel.consume(this._queue, onIncomingMessage.bind(this));
        log.info('started');
    }
Example #22
0
AmqpConnection.prototype.connect = function connect (callback) {
    console.log('[messaging:messaging] Connecting to AMQP server at', this.url);
    
    var self = this;
    amqplib.connect(this.url).then(function onConnected (connection) {
        self.connection = connection;

        connection.on('close', function onAmqpConnectionClose () {
            console.log('[messaging:messaging] AMQP connection closed...');
            
            self.closed = true;
        });
        
        connection.on('error', function onAmqpConnectionError (error) {
            console.log('[messaging:messaging] AMQP connection closed...');
            
            if (error && amqplib.isFatal(error)) {
                console.error('[messaging:messaging] Connection shutdown by server');
            }
            
            self.closed = true;
        });
        
        process.on('exit', function () {
            console.log('[messaging:messaging] Process exiting normally...');
            
            connection.close(); 
        });
        
        callback();
    }, callback);
};
Example #23
0
// Post a status message
function publish_status_update(fractal) {
    amqp.connect(amqp_connection).then(function(conn) {
        var ok = conn.createChannel().then(function(ch) {
            ch.assertQueue(amqp_queue_update);
            ch.sendToQueue(amqp_queue_update, new Buffer(JSON.stringify(fractal)));
        });
        return ok;
    }).then(null, console.warn);
}
Example #24
0
File: test.js Project: hongkou/va
 before(function (done) {
     var open = amqp.connect(rbqUri);
     brokerFactory.create(open, {nm: true})
         .then(function (brokerOrigin) {
             nodeManagerBroker = brokerOrigin.getNodeManager();
             broker = brokerOrigin;
             done();
         })
 });
 before(function () {
   return amqp.connect('amqp://' + db_host)
     .then(function (conn) {
       client = conn
       return client.createChannel()
     })
     .then(function (ch) {
       context.channel = ch
     })
 })
Example #26
0
exports.start = function(conf) {
   var queueUrl = conf.get("pigeon.queue.broker");
   log.info("connecting to message queue broker at %s", queueUrl);

   amqplib.connect(queueUrl)
          .then(function(conn) {
             queueConnection = conn;
             return listenOnQueue(conf);
          });
};
Example #27
0
 before(function(done){
   amqplib.connect('amqp://*****:*****@localhost:5672').then(function(conn){
     conn.createChannel().then(function(ch){
       ch.assertQueue('/queue/sometestpub2', {durable: true, autoDelete: false, messageTtl: 30000, expires: 3600000});
       ch.assertQueue('/queue/sometestget', {durable: true, autoDelete: false, messageTtl: 30000, expires: 3600000});
       log('queues', 'created');
       done();
     });
   });
 });
Example #28
0
 constructor(url, name){
   if(typeof url !== "string" || typeof name !== "string") {
     throw Error(`Invalid arguments passed to PubSub constructor, url and queueName need to be string, was ${url} and ${name}`)
   }
   this._name = name;
   this._connection = amqplib.connect(url).then((connection) => {
     Log.info("[PubSub] connection established");
     return connection;
   });
 }
Example #29
0
 constructor(name, queue, brokerURL) {
   this.name = name;
   this.queue = queue;
   this.brokerURL = brokerURL;
   this.promise = new Promise().then();
   this.connection = amqp.connect(brokerURL).then(connection =>
   connection.createChannel()).catch(console.warn.bind(console));
   this.connection.then(channel => this.channel = channel);
   console.log('Created Producer', name, 'for queue', queue);
 }
Example #30
0
export function toTwitter(contentId) {
	const queueName  = "twitter";
	const connection = amqp.connect(config.rabbitMq.url);
	const channel    = connection.then((conn) =>conn.createChannel());
	return promise.join(connection, channel, (con, ch) =>
			ch.assertQueue(queueName, {durable: true})
					.then((qok) => ch.sendToQueue(queueName, new Buffer(contentId.toString())))
					.finally(() => ch.close()
							.finally(()=> con.close())));
}