Exemplo n.º 1
0
def place_order(body):
    conn = get_connection()
    cursor = conn.cursor()

    sql = "Insert into orders (total_amount, created_on, status, email)  values (%s, %s,%s,%s )"
    values = (body['total_amount'], body['created_on'], 'Ordered',
              body['email'])
    rows = cursor.execute(sql, values)

    order_id = cursor.lastrowid

    for row in body['products']:
        sql = "Insert into order_details (product_id, order_id, product_name, quantity, unit_cost)  values (%s, %s, %s, %s, %s)"
        values = (row['product_id'], order_id, row['product_name'],
                  row['quantity'], row['unit_cost'])
        rows = cursor.execute(sql, values)

    conn.commit()

    queue = '/queue/ordered'
    amq_conf = None
    #    if isOpen('activemq-service.default', 61613):
    amq_conf = StompConfig('tcp://activemq-service.default:61613')
    #else:
    #    amq_conf = StompConfig('tcp://localhost:30012')
    try:
        client = Stomp(amq_conf)
        client.connect()
        client.send(queue, str(order_id).encode())
        client.disconnect()
    except:
        print("something went wrong")
    conn.close()
    return {'order_id': order_id}
Exemplo n.º 2
0
def queueFile(parameters):
    bucket = app.config['s3.bucket']
    username = app.config['s3.username']
    password = app.config['s3.password']
    input_folder = app.config['s3.input_folder'] or '/comets/input/'

    if username and password:
        s3 = boto3.resource('s3',
                            aws_access_key_id=username,
                            aws_secret_access_key=password)
    else:
        s3 = boto3.resource('s3')

    s3.meta.client.upload_file(
        parameters['filepath'],
        bucket,
        input_folder + parameters['filename'],
    )

    forQueue = json.dumps(parameters)
    client = Stomp(
        StompConfig('tcp://' + app.config['queue.host'] + ':' +
                    str(app.config['queue.port'])))
    client.connect()
    client.send('/queue/Comets', forQueue,
                {'correlation-id': parameters['filename']})
    client.disconnect()
    def test_4_integration_stomp_1_1(self):
        if StompSpec.VERSION_1_1 not in commands.versions(VERSION):
            print 'This broker does not support STOMP protocol version 1.1'
            return

        client = Stomp(self.getConfig(StompSpec.VERSION_1_1))
        client.connect(host=VIRTUALHOST)

        client.send(self.DESTINATION, 'test message 1')
        client.send(self.DESTINATION, 'test message 2')
        self.assertFalse(client.canRead(self.TIMEOUT))
        token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: 'client-individual'})
        self.assertTrue(client.canRead(self.TIMEOUT))
        client.ack(client.receiveFrame())
        self.assertTrue(client.canRead(self.TIMEOUT))
        client.ack(client.receiveFrame())
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.unsubscribe(token)
        client.send(self.DESTINATION, 'test message 3', receipt='4711')
        self.assertTrue(client.canRead(self.TIMEOUT))
        self.assertEquals(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {'receipt-id': '4711'}))
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: 'client-individual'})
        self.assertTrue(client.canRead(self.TIMEOUT))
        client.ack(client.receiveFrame())
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.disconnect(receipt='4712')
        self.assertEquals(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {'receipt-id': '4712'}))
        self.assertRaises(StompConnectionError, client.receiveFrame)
        client.connect(host=VIRTUALHOST)
        client.disconnect(receipt='4711')
        self.assertEquals(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {'receipt-id': '4711'}))
        client.close()
        self.assertRaises(StompConnectionError, client.canRead, 0)
Exemplo n.º 4
0
def main():
    logging.basicConfig()
    logging.getLogger().setLevel(logging.WARN)

    client = Stomp(stomp_config)
    client.connect()
    client.send(stomp_queue, body=stomp_body)
    client.subscribe(stomp_queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT, 'activemq.prefetchSize': 1})
    if client.canRead(timeout=5):
        frame = client.receiveFrame()
        print 'Got %s' % frame.info()
        client.ack(frame)
        frame_body = str(frame.body)
        if frame_body == stomp_body:
            print "OK: Message received"
            status = 'ok'
        else:
            print "WARNING: Incorrect message body; is %s, should be %s" % (frame_body, stomp_body)
            status = 'warning'
    else:
        print "CRITICAL: Timed out while trying to collect the message"
        status = 'critical'
    client.disconnect()
    client.close(flush=True)
    return exit_codes[status]
Exemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-d", nargs=1, required=True, help="destination", metavar="dest")
    parser.add_argument("-e", action="store_true", default=False, help="persistent msgs flag")
    parser.add_argument("-m", nargs=1, required=True, help="msg", metavar="msg")
    parser.add_argument("-n", nargs=1, default=False, help="number of msgs", metavar="int")
    parser.add_argument("-p", nargs=1, default=False, help="msg prefix", metavar="msg prefix")
    parser.add_argument("-s", nargs=1, required=True, help="broker", metavar="broker")
    parser.add_argument("-t", nargs=1, default=False, help="send msg every sec", metavar="float")
    args = parser.parse_args()

    broker = "tcp://%s:6163" % (args.s[0])
    config = StompConfig(broker)
    client = Stomp(config)
    client.connect()

    rd = "".join(random.sample("abcdefghijklmno", 2))

    try:
        i = 0
        if args.n:
            while i < int(args.n[0]):
                send_msg(args, client, i, rd)
                i += 1
        else:
            while True:
                send_msg(args, client, i, rd)
                i += 1
    except KeyboardInterrupt:
        client.disconnect()
        raise SystemExit(1)
Exemplo n.º 6
0
    def send(self):
        """
            Create a new stomp configuration client, connect and
            then serializes message by message posting
            them to your consumers in TOPIC standard
            and disconnect.
        """

        try:

            configuration = StompConfig(uri=self._broker_uri)
            client = Stomp(configuration)
            client.connect(connectTimeout=self._broker_timeout)

            for message in self._queue:

                serialized_message = json.dumps(message, ensure_ascii=False)
                client.send(self._queue_destination, serialized_message)

            client.disconnect()

        except Exception, e:

            self.log.error(
                u'QueueManagerError - Error on sending objects from queue.')
            self.log.debug(e)
            raise Exception(
                'QueueManagerError - Error on sending objects to queue.')
    def test_6_integration_stomp_1_1_encoding_and_escaping_headers(self):
        if BROKER == 'rabbitmq':
            print 'Broker does not support unicode characters. Skipping this test case.'
            return

        version = StompSpec.VERSION_1_1
        client = Stomp(self.getConfig(version))
        try:
            client.connect(host=VIRTUALHOST, versions=[version])
        except StompProtocolError as e:
            print 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (
                e, version)
            return

        specialCharactersHeader = u'fen\xeatre:\r\n'
        headers = {specialCharactersHeader: u'\xbfqu\xe9 tal?, s\xfc\xdf'}
        client.send(self.DESTINATION, body='test message 1', headers=headers)
        self.assertFalse(client.canRead(self.TIMEOUT))
        token = client.subscribe(
            self.DESTINATION, {
                StompSpec.ID_HEADER: 4711,
                StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL
            })
        self.assertTrue(client.canRead(self.TIMEOUT))
        frame = client.receiveFrame()
        client.ack(frame)
        self.assertEquals(frame.version, version)
        self.assertEquals(frame.headers[specialCharactersHeader],
                          headers[specialCharactersHeader])
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.unsubscribe(token)
        client.disconnect(receipt='4712')
    def test_6_integration_stomp_1_1_encoding_and_escaping_headers(self):
        if BROKER == 'rabbitmq':
            print('Broker does not support unicode characters. Skipping this test case.')
            return

        version = StompSpec.VERSION_1_1
        client = Stomp(self.getConfig(version))
        try:
            client.connect(host=VIRTUALHOST, versions=[version])
        except StompProtocolError as e:
            print('Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version))
            return

        key = b'fen\xc3\xaatre'.decode('utf-8')
        value = b'\xc2\xbfqu\xc3\xa9 tal?'.decode('utf-8')
        headers = {key: value}
        client.send(self.DESTINATION, body=b'test message 1', headers=headers)
        self.assertFalse(client.canRead(self.TIMEOUT))
        token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        self.assertTrue(client.canRead(self.TIMEOUT))
        frame = client.receiveFrame()
        client.ack(frame)
        self.assertEqual(frame.version, version)
        self.assertEqual(frame.headers[key], headers[key])
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.unsubscribe(token)
        client.disconnect(receipt='4712')
    def _test_4_integration_stomp(self, version):
        client = Stomp(self.getConfig(version))
        try:
            client.connect(host=VIRTUALHOST, versions=[version])
        except StompProtocolError as e:
            print('Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version))
            return

        client.send(self.DESTINATION, b'test message 1')
        client.send(self.DESTINATION, b'test message 2')
        self.assertFalse(client.canRead(self.TIMEOUT))
        token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        self.assertTrue(client.canRead(self.TIMEOUT))
        client.ack(client.receiveFrame())
        self.assertTrue(client.canRead(self.TIMEOUT))
        client.ack(client.receiveFrame())
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.unsubscribe(token)
        client.send(self.DESTINATION, b'test message 3', receipt='4711')
        self.assertTrue(client.canRead(self.TIMEOUT))
        self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'}))
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        self.assertTrue(client.canRead(self.TIMEOUT))
        client.ack(client.receiveFrame())
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.disconnect(receipt='4712')
        self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712'}))
        self.assertRaises(StompConnectionError, client.receiveFrame)
        client.connect(host=VIRTUALHOST)
        client.disconnect(receipt='4711')
        self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'}))
        client.close()
        self.assertRaises(StompConnectionError, client.canRead, 0)
Exemplo n.º 10
0
def writeJson(value,*argv):
    '''Writes the specified value to an output file
    Takes:
    value-> List or Dict
    *argv: Available options:
       'stomp'
       'post'
       'outfile'
    Returns:
    none
    '''
    outJson=json.dumps(value)
    print outJson
    if 'stomp' in argv:
        CONFIG = StompConfig(stomp_config.server, stomp_config.login,stomp_config.passcode)
        QUEUE = stomp_config.queue
        client = Stomp(CONFIG)
        client.connect()
        client.send(QUEUE, outJson)
        client.disconnect()
    elif 'outfile' in argv:
        with open(outfile,'w') as jsonFile:
            jsonFile.write(outJson)
    elif 'post' in argv:
        #TODO Post to php server
        pass
Exemplo n.º 11
0
def Producer(account, message):
    def documentCreateNotice():
        result = dict(body=dict(func_name="documentCreateNotice",
                                func_args=[{
                                    "doc_type": "importEmail"
                                }]),
                      recipient="*",
                      profile="user",
                      tag="grendizer")
        return json.dumps(result)

    def Send_Notify(message):
        result = dict(body=dict(func_name="toastr.info", func_args=[message]),
                      recipient="*",
                      profile="user",
                      tag="grendizer")
        return json.dumps(result)

    def clearNotice():
        result = dict(body=dict(func_name="toastr.clear", func_args=[]),
                      recipient="*",
                      profile="user",
                      tag="grendizer")
        return json.dumps(result)

    send_notify = Send_Notify(message)
    clear_notice = clearNotice()
    notice = documentCreateNotice()
    logger.debug(u"Отправляем сообщение: %s" % message)
    client = Stomp(StompConfig(default_uri))
    client.connect()
    client.send(queue, notice)
    client.send(queue, send_notify)
    client.send(queue, clear_notice)
    client.disconnect()
    def test_6_integration_stomp_1_1_encoding_and_escaping_headers(self):
        if BROKER == 'rabbitmq':
            print 'Broker does not support unicode characters. Skipping this test case.'
            return

        version = StompSpec.VERSION_1_1
        client = Stomp(self.getConfig(version))
        try:
            client.connect(host=VIRTUALHOST, versions=[version])
        except StompProtocolError as e:
            print 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version)
            return

        specialCharactersHeader = u'fen\xeatre:\r\n'
        headers = {specialCharactersHeader: u'\xbfqu\xe9 tal?, s\xfc\xdf'}
        client.send(self.DESTINATION, body='test message 1', headers=headers)
        self.assertFalse(client.canRead(self.TIMEOUT))
        token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        self.assertTrue(client.canRead(self.TIMEOUT))
        frame = client.receiveFrame()
        client.ack(frame)
        self.assertEquals(frame.version, version)
        self.assertEquals(frame.headers[specialCharactersHeader], headers[specialCharactersHeader])
        self.assertFalse(client.canRead(self.TIMEOUT))
        client.unsubscribe(token)
        client.disconnect(receipt='4712')
Exemplo n.º 13
0
class PublisherAMQ(object):
    '''
    classdocs
    '''

    CONFIG = StompConfig('tcp://localhost:61613')
    QUEUE = '/queue/revideomessagesBE'

    def __init__(self, selk, selv, config=None, queue=None):
        '''
        Constructor
        '''
        if config is not None:
            self.CONFIG = config
        if queue is not None:
            self.QUEUE = queue
        self.client = Stomp(self.CONFIG)
        self.client.connect()
        self.selk = selk
        self.selv = selv

    def send(self, msg):
        #print "Send::: " + self.selector
        self.client.send(self.QUEUE, msg, headers={self.selk: self.selv})

    def end(self):
        self.client.disconnect()
 def test_3_socket_failure_and_replay(self):
     client = Stomp(self.getConfig(StompSpec.VERSION_1_0))
     client.connect(host=VIRTUALHOST)
     headers = {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}
     token = client.subscribe(self.DESTINATION, headers)
     client.sendFrame(
         StompFrame(StompSpec.DISCONNECT)
     )  # DISCONNECT frame is out-of-band, as far as the session is concerned -> unexpected disconnect
     self.assertRaises(StompConnectionError, client.receiveFrame)
     client.connect(host=VIRTUALHOST)
     client.send(self.DESTINATION, 'test message 1')
     client.ack(client.receiveFrame())
     client.unsubscribe(token)
     headers = {
         StompSpec.ID_HEADER: 'bla',
         StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL
     }
     client.subscribe(self.DESTINATION, headers)
     headers[StompSpec.DESTINATION_HEADER] = self.DESTINATION
     client.sendFrame(
         StompFrame(StompSpec.DISCONNECT)
     )  # DISCONNECT frame is out-of-band, as far as the session is concerned -> unexpected disconnect
     self.assertRaises(StompConnectionError, client.receiveFrame)
     client.connect(host=VIRTUALHOST)
     client.send(self.DESTINATION, 'test message 2')
     client.ack(client.receiveFrame())
     client.unsubscribe((StompSpec.ID_HEADER, 'bla'))
     client.disconnect()
Exemplo n.º 15
0
def send(data):
    mqcfg = settings.MESSAGE_QUEUE
    config = StompConfig(mqcfg['url'],
                         login=mqcfg['username'],
                         passcode=mqcfg['password'])
    stomp = Stomp(config)
    stomp.connect(host=mqcfg['broker'])
    stomp.send(mqcfg['vote_queue'], json.dumps(data))
    stomp.disconnect()
    def test_2_transaction(self):
        config = self.getConfig(StompSpec.VERSION_1_0)
        client = Stomp(config)
        client.connect(host=VIRTUALHOST)
        client.subscribe(
            self.DESTINATION,
            {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        self.assertFalse(client.canRead(self.TIMEOUT))

        with client.transaction(4711) as transaction:
            self.assertEquals(transaction, '4711')
            client.send(self.DESTINATION, 'test message',
                        {StompSpec.TRANSACTION_HEADER: transaction})
            self.assertFalse(client.canRead(0))
        self.assertTrue(client.canRead(self.TIMEOUT))
        frame = client.receiveFrame()
        self.assertEquals(frame.body, 'test message')
        client.ack(frame)

        with client.transaction(4713, receipt='4712') as transaction:
            self.assertEquals(transaction, '4713')
            self.assertEquals(
                client.receiveFrame(),
                StompFrame(StompSpec.RECEIPT,
                           {StompSpec.RECEIPT_ID_HEADER: '4712-begin'}))
            client.send(self.DESTINATION, 'test message',
                        {StompSpec.TRANSACTION_HEADER: transaction})
            client.send(self.DESTINATION, 'test message without transaction')
            self.assertTrue(client.canRead(self.TIMEOUT))
            frame = client.receiveFrame()
            self.assertEquals(frame.body, 'test message without transaction')
            client.ack(frame)
            self.assertFalse(client.canRead(0))
        frames = [client.receiveFrame() for _ in xrange(2)]
        frames = list(sorted(frames, key=lambda f: f.command))
        frame = frames[0]
        client.ack(frame)
        self.assertEquals(frame.body, 'test message')
        frame = frames[1]
        self.assertEquals(
            frame,
            StompFrame(StompSpec.RECEIPT,
                       {StompSpec.RECEIPT_ID_HEADER: '4712-commit'}))

        try:
            with client.transaction(4714) as transaction:
                self.assertEquals(transaction, '4714')
                client.send(self.DESTINATION, 'test message',
                            {StompSpec.TRANSACTION_HEADER: transaction})
                raise RuntimeError('poof')
        except RuntimeError as e:
            self.assertEquals(str(e), 'poof')
        else:
            raise
        self.assertFalse(client.canRead(self.TIMEOUT))

        client.disconnect()
Exemplo n.º 17
0
def recv_stomp ():
    config = StompConfig (stomp_uri)
    client = Stomp (config)
    client.connect ()
    client.subscribe (stomp_source, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
    frame = client.receiveFrame ()
    print "Received: {}".format (frame.info ())
    client.ack (frame)
    client.disconnect ()
Exemplo n.º 18
0
def test(size):
    config = StompConfig("tcp://%s:%d" % (ACTIVEMQ_HOST, ACTIVEMQ_PORT))
    client = Stomp(config)
    client.connect()
    msg = "0" * size
    for i in range(0, COUNT):
        client.send(TOPIC, msg)
    client.send(TOPIC, "quit")
    client.disconnect()
Exemplo n.º 19
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-a', action='store_true', default=False, help='client ack')
    parser.add_argument('-b', action='store_true', default=False, help='write body only')
    parser.add_argument('-d', nargs=1, required=True, help='destination', metavar='dest')
    parser.add_argument('-f', nargs=1, required=True, help='file with recorded msgs', metavar='file')
    parser.add_argument('-n', nargs=1, default=False, help='number of msgs', metavar='int')
    parser.add_argument('-r', action='store_true', default=False, help='reconnect')
    parser.add_argument('-s', nargs=1, required=True, help='broker', metavar='broker')
    parser.add_argument('-t', nargs=1, default=False, help='recv msg every sec', metavar='float')
    args = parser.parse_args()

    broker = 'tcp://%s:6163' % (args.s[0])
    config = StompConfig(broker)

    client = Stomp(config)
    if not args.r:
        client.connect()
        if args.a:
            client.subscribe(args.d[0], {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        else:
            client.subscribe(args.d[0], {StompSpec.ACK_HEADER: StompSpec.ACK_AUTO})
    try:
        consumed = 0
        while True:
            if args.r:
                client.connect()
                if args.a:
                    client.subscribe(args.d[0], {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
                else:
                    client.subscribe(args.d[0], {StompSpec.ACK_HEADER: StompSpec.ACK_AUTO})
            if args.t:
                time.sleep(float(args.t[0]))
            else:
                time.sleep(1)
            fo = open(args.f[0], 'a+')
            frame = client.receiveFrame()
            consumed += 1
            if args.b:
                fo.write(frame.body+'\n')
            else:
                fo.write(frame.info()+'\n')
            fo.close()
            if args.a:
                client.ack(frame)
            if args.r:
                client.disconnect()
            if args.n:
                if consumed == int(args.n[0]):
                    raise KeyboardInterrupt
    except KeyboardInterrupt:
        client.stop()
        client.disconnect()
        raise SystemExit(1)
    except stompest.error.StompProtocolError:
        pass
Exemplo n.º 20
0
def recv_stomp():
    config = StompConfig(stomp_uri)
    client = Stomp(config)
    client.connect()
    client.subscribe(stomp_source,
                     {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
    frame = client.receiveFrame()
    print "Received: {}".format(frame.info())
    client.ack(frame)
    client.disconnect()
Exemplo n.º 21
0
 def setUp(self):
     config = self.getConfig(StompSpec.VERSION_1_0)
     client = Stomp(config)
     client.connect(host=VIRTUALHOST)
     client.subscribe(self.DESTINATION, {StompSpec.ACK_HEADER: StompSpec.ACK_AUTO})
     client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 'bla', StompSpec.ACK_HEADER: StompSpec.ACK_AUTO})
     while client.canRead(self.TIMEOUT):
         frame = client.receiveFrame()
         self.log.debug('Dequeued old %s' % frame.info())
     client.disconnect()
Exemplo n.º 22
0
 def run(self, messages=None):
     client = Stomp(self.config)
     client.connect()
     j = 0
     for message in messages:
         client.send(queue, message,
                     receipt='message-asterisk-%d' %
                     j, headers={'persistent': 'true'})
         j = j + 1
     client.disconnect(receipt='bye')
Exemplo n.º 23
0
    def test_3_timeout(self):
        timeout = 0.2
        client = Stomp(StompConfig(uri='failover:(tcp://localhost:61610,tcp://localhost:61613)?startupMaxReconnectAttempts=1,randomize=false', login=LOGIN, passcode=PASSCODE, version=StompSpec.VERSION_1_0))
        client.connect(host=VIRTUALHOST, connectTimeout=timeout)
        client.disconnect()

        client = Stomp(StompConfig(uri='failover:(tcp://localhost:61610,tcp://localhost:61611)?startupMaxReconnectAttempts=1,backOffMultiplier=3', login=LOGIN, passcode=PASSCODE, version=StompSpec.VERSION_1_0))
        self.assertRaises(StompConnectionError, client.connect, host=VIRTUALHOST, connectTimeout=timeout)

        client = Stomp(StompConfig(uri='failover:(tcp://localhost:61610,tcp://localhost:61613)?randomize=false', login=LOGIN, passcode=PASSCODE, version=StompSpec.VERSION_1_0)) # default is startupMaxReconnectAttempts = 0
        self.assertRaises(StompConnectionError, client.connect, host=VIRTUALHOST, connectTimeout=timeout)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('config_file')
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--logfile')
    parser.add_argument('--purge', action='store_true')
    parser.add_argument('--loadgen', action='store_true')
    parser.add_argument('--sessionid')
    parser.add_argument('--httpserver', action='store_true')
    args = parser.parse_args()

    if args.logfile:
        fh = FileHandler(args.logfile)
        formatter = logging.Formatter(FORMAT)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    if args.debug:
        logger.setLevel(logging.DEBUG)

    config = yaml.load(open(args.config_file))
    sessionmaker = config_to_db_session(config, Base)

    stomp_endpoint = config['stomp_endpoint']
    stomp_login = config['stomp_login']
    stomp_password = config['stomp_password']

    stomp_config = StompConfig(stomp_endpoint, login=stomp_login, passcode=stomp_password)

    if args.purge:
        purge_completed_jobs(sessionmaker())
        return
    elif args.loadgen:
        if not args.sessionid:
            raise ValueError('parameter sessionid is required for load generator')
        jm = StompSync(stomp_config)
        jm.connect()
        generate_load(jm, args.sessionid)
        jm.disconnect()
        return
    elif args.httpserver:
        from jobmanager.www import app
        app.config['DB'] = sessionmaker
        app.config['params'] = config
        app.run()
        return

    jm = JobManager(sessionmaker, config=stomp_config)
    jm.run()

    l = task.LoopingCall(jm.run_periodic_checks)
    l.start(PERIODIC_CHECK_INTERVAL)

    reactor.run()
Exemplo n.º 25
0
def test():
    config = StompConfig("tcp://%s:%d" % (ACTIVEMQ_HOST, ACTIVEMQ_PORT))
    client = Stomp(config)
    client.connect()
    client.subscribe(TOPIC,
                     {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
    while True:
        f = client.receiveFrame()
        client.ack(f)
        if f.body == "quit":
            client.disconnect()
            break
Exemplo n.º 26
0
class ConsumerAMQ(object):
    '''
    classdocs
    '''

    CONFIG = StompConfig('tcp://localhost:61613')
    QUEUE = '/queue/revideomessagesFE'

    FRAMEBODY = "MESSAGE"

    def __init__(self, sel, config=None, queue=None):
        '''
        Constructor
        '''

        if config is not None:
            self.CONFIG = config
        if queue is not None:
            self.QUEUE = queue
        self.selector = sel
        self.client = Stomp(self.CONFIG)
        self.client.connect()
        self.client.subscribe(
            self.QUEUE, {
                StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL,
                "selector": self.selector
            })

    def receive(self):
        frame = None
        cnt = 5
        while True:
            frame = self.client.receiveFrame()
            #print "frame: " + frame.info()
            if frame.command == self.FRAMEBODY:
                self.client.ack(frame)

                break
            frame = None
            cnt = cnt - 1
            if cnt <= 0:
                break
        if frame is None:
            return frame
        else:
            return frame.body

    def end(self):
        self.client.disconnect()
    def test_5_integration_stomp_1_1_heartbeat(self):
        version = StompSpec.VERSION_1_1
        if BROKER == 'apollo':
            print "Broker %s doesn't properly support heart-beating. Skipping test." % BROKER
            return

        port = 61612 if (BROKER == 'activemq') else PORT # stomp+nio on 61613 does not work properly, so use stomp on 61612
        client = Stomp(self.getConfig(StompSpec.VERSION_1_1, port))
        self.assertEquals(client.lastReceived, None)
        self.assertEquals(client.lastSent, None)

        heartBeatPeriod = 100
        try:
            client.connect(host=VIRTUALHOST, heartBeats=(heartBeatPeriod, heartBeatPeriod), versions=[version])
        except StompProtocolError as e:
            print 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version)
            return

        self.assertTrue((time.time() - client.lastReceived) < 0.1)
        if not (client.serverHeartBeat and client.clientHeartBeat):
            print 'broker does not support heart-beating. disconnecting ...'
            client.disconnect()
            client.close()
            return

        serverHeartBeatInSeconds = client.serverHeartBeat / 1000.0
        clientHeartBeatInSeconds = client.clientHeartBeat / 1000.0

        start = time.time()
        while (time.time() - start) < (2.5 * max(serverHeartBeatInSeconds, clientHeartBeatInSeconds)):
            time.sleep(0.5 * min(serverHeartBeatInSeconds, clientHeartBeatInSeconds))
            client.canRead(0)
            self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
            if (time.time() - client.lastSent) > (0.5 * clientHeartBeatInSeconds):
                client.beat()
                self.assertTrue((time.time() - client.lastSent) < 0.1)

        start = time.time()
        try:
            while not client.canRead(0.5 * clientHeartBeatInSeconds):
                pass
        except StompConnectionError:
            self.assertTrue((time.time() - start) < (3.0 * clientHeartBeatInSeconds))
            self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
            self.assertTrue((time.time() - client.lastSent) > clientHeartBeatInSeconds)
        else:
            raise
        client.close()
Exemplo n.º 28
0
class ActiveMQSession(object):
    """
    Create a scoped session for every request and
    closes it when the request ends.
    """
    def __init__(self, stompconf):
        self.client = Stomp(stompconf)

    def process_resource(self, req, resp, resource, params):
        self.client.connect()
        resource.activemq_conn = self.client

    def process_response(self, req, resp, resource, req_succeeded):
        if hasattr(resource, 'activemq_conn'):
            self.client.disconnect()
            self.client.close()
Exemplo n.º 29
0
def _sender(queue_req, msg, session_id, msg_id):
    stomp_config = config.mq_config.get('stomp')
    client = Stomp(StompConfig(stomp_config))
    client.connect()

    #H = {StompSpec.CONTENT_LENGTH_HEADER: len(msg), "length": len(msg)}
    H = {}
    H['MsgID'] = msg_id
    H['SessionID'] = session_id
    H['Signature'] = sign(msg)

    if isinstance(msg, dict): msg = json.dumps(msg)
    print '===> [%s] session_ids: %s, body: %s' % (queue_req, session_id, msg)

    client.send(queue_req, body=msg, headers=H, receipt="ok")
    client.disconnect()
    def test_5_integration_stomp_1_1_heartbeat(self):
        version = StompSpec.VERSION_1_1

        port = 61612 if (BROKER == 'activemq') else PORT # stomp+nio on 61613 does not work properly, so use stomp on 61612
        client = Stomp(self.getConfig(StompSpec.VERSION_1_1, port))
        self.assertEquals(client.lastReceived, None)
        self.assertEquals(client.lastSent, None)

        heartBeatPeriod = 100
        try:
            client.connect(host=VIRTUALHOST, heartBeats=(heartBeatPeriod, heartBeatPeriod), versions=[version])
        except StompProtocolError as e:
            print 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version)
            return

        self.assertTrue((time.time() - client.lastReceived) < 0.1)
        if not (client.serverHeartBeat and client.clientHeartBeat):
            print 'broker does not support heart-beating. disconnecting ...'
            client.disconnect()
            client.close()
            return

        serverHeartBeatInSeconds = client.serverHeartBeat / 1000.0
        clientHeartBeatInSeconds = client.clientHeartBeat / 1000.0

        start = time.time()
        while (time.time() - start) < (2.5 * max(serverHeartBeatInSeconds, clientHeartBeatInSeconds)):
            time.sleep(0.5 * min(serverHeartBeatInSeconds, clientHeartBeatInSeconds))
            client.canRead(0)
            self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
            if (time.time() - client.lastSent) > (0.5 * clientHeartBeatInSeconds):
                client.beat()
                self.assertTrue((time.time() - client.lastSent) < 0.1)

        start = time.time()
        try:
            while not client.canRead(0.5 * clientHeartBeatInSeconds):
                pass
            if client.receiveFrame().command == StompSpec.ERROR:
                raise StompProtocolError()
        except (StompConnectionError, StompProtocolError):
            self.assertTrue((time.time() - start) < (3.0 * clientHeartBeatInSeconds))
            self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
            self.assertTrue((time.time() - client.lastSent) > clientHeartBeatInSeconds)
        else:
            raise
        client.close()
Exemplo n.º 31
0
def change_order_status(id):
    conn = get_connection()
    cursor = conn.cursor()

    cursor.execute("SELECT status FROM orders where  order_id = " + str(id) +
                   "")
    rows = cursor.fetchall()

    status = rows[0][0]
    next_status = None
    if status == 'Ordered':
        next_status = 'ReadyToShip'
        queue = '/queue/readytoship'

    elif status == 'ReadyToShip':
        next_status = 'Shipped'
        queue = '/queue/shipped'

    elif status == 'Shipped':
        next_status = 'Delivered'

    if status == 'Delivered':
        return True
    if next_status == 'ReadyToShip' or next_status == 'Shipped':
        amq_conf = None
        #if isOpen('activemq.default', 61612):
        amq_conf = StompConfig('tcp://activemq-service.default:61613')
        #else:
        #    amq_conf = StompConfig('tcp://localhost:30012')
        try:
            client = Stomp(amq_conf)
            client.connect()
            client.send(queue, str(id).encode())
            client.disconnect()
        except:
            print("something went wrong")
    sql = "Update systeam_ecommerce.orders SET status = '" + next_status + "' where order_id = " + str(
        id) + ""
    rows = cursor.execute(sql)

    sql = "Delete from systeam_ecommerce.next_order where status = '" + status + "'"
    rows = cursor.execute(sql)

    conn.commit()
    conn.close()
    return True
    def test_2_transaction(self):
        config = self.getConfig(StompSpec.VERSION_1_0)
        client = Stomp(config)
        client.connect(host=VIRTUALHOST)
        client.subscribe(self.DESTINATION, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
        self.assertFalse(client.canRead(self.TIMEOUT))

        with client.transaction(4711) as transaction:
            self.assertEqual(transaction, '4711')
            client.send(self.DESTINATION, b'test message', {StompSpec.TRANSACTION_HEADER: transaction})
            self.assertFalse(client.canRead(0))
        self.assertTrue(client.canRead(self.TIMEOUT))
        frame = client.receiveFrame()
        self.assertEqual(frame.body, b'test message')
        client.ack(frame)

        with client.transaction(4713, receipt='4712') as transaction:
            self.assertEqual(transaction, '4713')
            self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712-begin'}))
            client.send(self.DESTINATION, b'test message', {StompSpec.TRANSACTION_HEADER: transaction})
            client.send(self.DESTINATION, b'test message without transaction')
            self.assertTrue(client.canRead(self.TIMEOUT))
            frame = client.receiveFrame()
            self.assertEqual(frame.body, b'test message without transaction')
            client.ack(frame)
            self.assertFalse(client.canRead(0))
        frames = [client.receiveFrame() for _ in range(2)]
        frames = list(sorted(frames, key=lambda f: f.command))
        frame = frames[0]
        client.ack(frame)
        self.assertEqual(frame.body, b'test message')
        frame = frames[1]
        self.assertEqual(frame, StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712-commit'}))

        try:
            with client.transaction(4714) as transaction:
                self.assertEqual(transaction, '4714')
                client.send(self.DESTINATION, b'test message', {StompSpec.TRANSACTION_HEADER: transaction})
                raise RuntimeError('poof')
        except RuntimeError as e:
            self.assertEqual(str(e), 'poof')
        else:
            raise
        self.assertFalse(client.canRead(self.TIMEOUT))

        client.disconnect()
Exemplo n.º 33
0
def sendqueue(tokenId):
    #try:
    timestr = time.strftime("%Y-%m-%d")
    QUEUE = jpsurvConfig.getAsString(QUEUE_NAME)
    QUEUE_CONFIG = StompConfig(jpsurvConfig.getAsString(QUEUE_URL))
    client = Stomp(QUEUE_CONFIG)
    client.connect()
    client.send(
        QUEUE,
        json.dumps({
            "filepath": UPLOAD_DIR,
            "token": tokenId,
            "timestamp": timestr
        }))
    client.disconnect()

    return
    def test_5_integration_stomp_1_1_heartbeat(self):
        version = StompSpec.VERSION_1_1

        client = Stomp(self.getConfig(StompSpec.VERSION_1_1))
        self.assertEqual(client.lastReceived, None)
        self.assertEqual(client.lastSent, None)

        heartBeatPeriod = 100
        try:
            client.connect(host=VIRTUALHOST, heartBeats=(heartBeatPeriod, heartBeatPeriod), versions=[version])
        except StompProtocolError as e:
            print('Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version))
            return

        self.assertTrue((time.time() - client.lastReceived) < 0.1)
        if not (client.serverHeartBeat and client.clientHeartBeat):
            print('broker does not support heart-beating. disconnecting ...')
            client.disconnect()
            client.close()
            return

        serverHeartBeatInSeconds = client.serverHeartBeat / 1000.0
        clientHeartBeatInSeconds = client.clientHeartBeat / 1000.0

        start = time.time()
        while (time.time() - start) < (2.5 * max(serverHeartBeatInSeconds, clientHeartBeatInSeconds)):
            time.sleep(0.5 * min(serverHeartBeatInSeconds, clientHeartBeatInSeconds))
            client.canRead(0)
            self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
            if (time.time() - client.lastSent) > (0.5 * clientHeartBeatInSeconds):
                client.beat()
                self.assertTrue((time.time() - client.lastSent) < 0.1)

        start = time.time()
        try:
            while not client.canRead(0.5 * clientHeartBeatInSeconds):
                pass
            if client.receiveFrame().command == StompSpec.ERROR:
                raise StompProtocolError()
        except (StompConnectionError, StompProtocolError):
            self.assertTrue((time.time() - start) < (3.0 * clientHeartBeatInSeconds))
            self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
            self.assertTrue((time.time() - client.lastSent) > clientHeartBeatInSeconds)
        else:
            raise
        client.close()
Exemplo n.º 35
0
 def run(self):
     files = os.listdir(self.path)
     for queue_file in files:
         archive_file = '{0}/archive/{1}'.format(self.path, queue_file)
         if queue_file.startswith('archive'):
             pass
         else:
             with open("{0}/{1}".format(self.path, queue_file), 'r') as qf:
                 get_lines = list(qf)
                 for line in get_lines:
                     dts = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                     client = Stomp(config=self.config)
                     client.connect()
                     print "Sending message {0} to queue {1}".format(line, queue_file)
                     with open(archive_file, 'a') as af:
                         af.write("{0} Sent message: {1} to queue {2}\n".format(dts, line, queue_file))
                     client.send(queue_file, json.dumps(line))
                     client.disconnect()
Exemplo n.º 36
0
def CreateConnection():
    try:
        Write2File(threading.currentThread().getName(), "INFO",
                   "Connecting to ActiveMQ Server.")
        client = Stomp(
            StompConfig("tcp://" + server + ":" + port,
                        login=login,
                        passcode=passcode,
                        version="1.2"))
        client.connect(versions=["1.2"], host=vhost,
                       heartBeats=(0, 60000))  #CONNECT
        subscription = client.subscribe(destination, {
            "ack": "client",
            "id": "0",
            "headers": {
                "activemq.prefetchSize": 1
            }
        })  #SUBSCRIB
        Write2File(threading.currentThread().getName(), "INFO",
                   "Connection to ActiveMQ Server successfully established.")
    except:
        Write2File(threading.currentThread().getName(), "ERROR",
                   str(sys.exc_info()))
        raise

    try:
        while (client.canRead(5)):
            Write2File(threading.currentThread().getName(), "DEBUG",
                       "Setting LOCK.")
            container.acquire()
            ReceiveFrame(client)
        else:
            Write2File(threading.currentThread().getName(), "INFO",
                       "Closing connection with ActiveMQ Server.")
            client.disconnect()
            if (container.acquire(blocking=False)):
                Write2File(threading.currentThread().getName(), "DEBUG",
                           "Removing LOCK.")
                container.release()
    except:
        Write2File(threading.currentThread().getName(), "ERROR",
                   str(sys.exc_info()))
        exit(1)
Exemplo n.º 37
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', nargs=1, required=True, help='destination', metavar='dest')
    parser.add_argument('-e', action='store_true', default=False, help='persistent msgs flag')
    parser.add_argument('-a', nargs=1, required=False, help='fixed timestamp', metavar='fixed timestamp')
    parser.add_argument('-n', nargs=1, default=False, help='number of msgs', metavar='int')
    parser.add_argument('-o', default=6163, type=int, help='port', metavar='port')
    parser.add_argument('-z', default=16, type=int, help='size of msg payload', metavar='msg prefix')
    parser.add_argument('-p', nargs=1, default=False, help='msg prefix', metavar='msg prefix')
    parser.add_argument('-s', nargs=1, required=True, help='broker', metavar='broker')
    parser.add_argument('-i', action='store_true', required=False, default=False, help='paired service type')
    parser.add_argument('-w', action='store_true', default=False, help='format message wrongly')
    parser.add_argument('-t', nargs=1, default=False, help='send msg every sec', metavar='float')
    parser.add_argument('-v', action='store_true', default=False, help='verbose')
    args = parser.parse_args()

    broker = 'tcp://%s:%i' % (args.s[0], args.o)
    config = StompConfig(broker)
    client = Stomp(config)
    client.connect()

    try:
        i = 0
        if args.n:
            while i < int(args.n[0]):
                msg = gen_msg(args)
                if args.v:
                    print str(i)
                    print '%.128s' % msg
                send_msg(args, client, msg)
                i += 1
        else:
            while True:
                msg = gen_msg(args)
                if args.v:
                    print str(i)
                    print '%.128s' % msg
                send_msg(args, client, msg)
                i += 1
    except KeyboardInterrupt:
        client.disconnect()
        raise SystemExit(1)
def sendqueue(inputFileId, emailAddress, fileName, url, socSystem):
    #try:
    import time
    now = time.strftime("%a %b %X %Z %Y")
    QUEUE = soccerConfig.getAsString(QUEUE_NAME)
    QUEUE_CONFIG = StompConfig(soccerConfig.getAsString(QUEUE_URL))
    filePath = os.path.join(RESULTS_PATH, inputFileId)
    client = Stomp(QUEUE_CONFIG)
    client.connect()
    client.send(
        QUEUE,
        json.dumps({
            "inputFileId": inputFileId,
            "emailAddress": emailAddress,
            "fileName": fileName,
            "timestamp": now,
            "url": url,
            "socSystem": socSystem
        }))
    client.disconnect()
 def test_3_socket_failure_and_replay(self):
     client = Stomp(self.getConfig(StompSpec.VERSION_1_0))
     client.connect(host=VIRTUALHOST)
     headers = {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}
     token = client.subscribe(self.DESTINATION, headers)
     client.sendFrame(StompFrame(StompSpec.DISCONNECT)) # DISCONNECT frame is out-of-band, as far as the session is concerned -> unexpected disconnect
     self.assertRaises(StompConnectionError, client.receiveFrame)
     client.connect(host=VIRTUALHOST)
     client.send(self.DESTINATION, b'test message 1')
     client.ack(client.receiveFrame())
     client.unsubscribe(token)
     headers = {StompSpec.ID_HEADER: 'bla', StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}
     client.subscribe(self.DESTINATION, headers)
     headers[StompSpec.DESTINATION_HEADER] = self.DESTINATION
     client.sendFrame(StompFrame(StompSpec.DISCONNECT)) # DISCONNECT frame is out-of-band, as far as the session is concerned -> unexpected disconnect
     self.assertRaises(StompConnectionError, client.receiveFrame)
     client.connect(host=VIRTUALHOST)
     client.send(self.DESTINATION, b'test message 2')
     client.ack(client.receiveFrame())
     client.unsubscribe((StompSpec.ID_HEADER, 'bla'))
     client.disconnect()
Exemplo n.º 40
0
def _sender(queue_req, msg, session_id, msg_id, selector=None):
    stomp_config = config.mq.get('stomp')
    client = Stomp(StompConfig(stomp_config))
    client.connect()

    #H = {StompSpec.CONTENT_LENGTH_HEADER: len(msg), "length": len(msg)}
    H = {}
    H['MsgID'] = msg_id
    H['SessionID'] = session_id
    H['Signature'] = sign(msg)
    if selector is not None:
        H[selector[0]] = selector[1]

    print 'send msg: '
    print json.dumps(msg, indent=1)
    if isinstance(msg, dict): msg = json.dumps(msg)
    logger.info('===>[%s] session_ids: %s, body: %s' %
                (queue_req, session_id, msg))

    client.send(queue_req, body=msg, headers=H, receipt="ok")
    client.disconnect()
def send_message(messageBody, destination=None, queueName=None):
    
    client = None
    if destination != None:
        client = Stomp(StompConfig(destination))
    else:
        client = Stomp(StompConfig("tcp://localhost:61613"))
    
    QUEUE = None
    if queueName != None:
        QUEUE = queueName
    else:
        QUEUE = "pods2jbpm"
        
    #client = Stomp(CONFIG)
    client.connect()
    
    body = messageBody
    
    client.send(QUEUE, body)
    
    client.disconnect()
Exemplo n.º 42
0
    def processData():
        # Process Files
        files = request.files
        print len(files)
        keys = []
        parameters = request.form
        param = {}
        for key in parameters:
            param[key] = parameters[key]

        for file in files:
            files[file].save(
                os.path.join(app.config[rs3d.UPLOAD_FOLDER],
                             files[file].filename))
            param[file] = files[file].filename

        try:
            client = Stomp(app.config[rs3d.QUEUE_CONFIG])
            client.connect()
            client.send(app.config[rs3d.QUEUE_NAME], json.dumps(param))
            client.disconnect()
            return "Success"
        except Exception as e:
            return "Failed"
Exemplo n.º 43
0
    def send(self):

        """
            Create a new stomp configuration client, connect and
            then serializes message by message posting
            them to your consumers in TOPIC standard
            and disconnect.
        """

        try:

            configuration = StompConfig(uri=self._broker_uri)
            client = Stomp(configuration)
            client.connect(connectTimeout=self._broker_timeout)

            for message in self._queue:
                serialized_message = json.dumps(message, ensure_ascii=False)
                client.send(self._queue_destination, serialized_message)

            client.disconnect()

        except Exception, e:
            self.log.error(u"QueueManagerError - Error on sending objects from queue.")
            self.log.debug(e)
Exemplo n.º 44
0
                pp.create_reduction_script()

            # Check for registered processors
            if type(configuration.processors) == list:
                for p in configuration.processors:
                    toks = p.split('.')
                    if len(toks) == 2:
                        processor_module = __import__("postprocessing.processors.%s" % toks[0], globals(), locals(), [toks[1], ], -1)
                        try:
                            processor_class = eval("processor_module.%s" % toks[1])
                            if namespace.queue == processor_class.get_input_queue_name():
                                # Instantiate and call the processor
                                proc = processor_class(data, configuration, send_function=pp.send)
                                proc()
                        except:
                            logging.error("PostProcessAdmin: Processor error: %s" % sys.exc_value)
                    else:
                        logging.error("PostProcessAdmin: Processors can only be specified in the format module.Processor_class")

        except:
            # If we have a proper data dictionary, send it back with an error message
            if type(data) == dict:
                data["error"] = str(sys.exc_value)
                stomp = Stomp(StompConfig(configuration.failover_uri, configuration.amq_user, configuration.amq_pwd))
                stomp.connect()
                stomp.send(configuration.postprocess_error, json.dumps(data))
                stomp.disconnect()
            raise
    except:
        logging.error("PostProcessAdmin: %s" % sys.exc_value)
Exemplo n.º 45
0
class JMSClient(object):
    """Class JMSClient
    """

    _mh = None
    _client = None
    _host = None
    _port = None
    _user = None
    _passw = None
    _verbose = None
    _is_connected = None

    def __init__(self, verbose=False):
        """Class constructor

        Called when the object is initialized

        Args:                   
           verbose (bool): verbose mode

        """

        try:

            self._mh = MasterHead.get_head()

            self._verbose = verbose
            if (self._verbose):
                basicConfig()
                getLogger().setLevel(DEBUG)

        except StompError as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())

    @property
    def client(self):
        """ STOMP client property getter """

        return self._client

    @property
    def host(self):
        """ server host property getter """

        return self._host

    @property
    def port(self):
        """ server port property getter """

        return self._port

    @property
    def user(self):
        """ username property getter """

        return self._user

    @property
    def passw(self):
        """ user password property getter """

        return self._passw

    @property
    def verbose(self):
        """ verbose mode property getter """

        return self._verbose

    @property
    def is_connected(self):
        """ is_connected property getter """

        return self._is_connected

    def connect(self, host, port=61613, user=None, passw=None, timeout=10):
        """Method connects to server

        Args:
           host (str): hostname
           port (str): port
           user (str): username
           passw (str): password
           timeout (int): timeout

        Returns:
           bool: result

        Raises:
           event: jms_before_connect
           event: jms_after_connected            

        """

        try:

            msg = 'host:{0}, port:{1}, user:{2}, passw:{3}, timeout:{4}'.format(
                host, port, user, passw, timeout)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_connecting', msg), self._mh.fromhere())

            ev = event.Event(
                'jms_before_connect', host, port, user, passw, timeout)
            if (self._mh.fire_event(ev) > 0):
                host = ev.argv(0)
                port = ev.argv(1)
                user = ev.argv(2)
                passw = ev.argv(3)
                timeout = ev.argv(4)

            self._host = host
            self._port = port
            self._user = user
            self._passw = passw

            if (ev.will_run_default()):
                self._client = Stomp(StompConfig('tcp://{0}:{1}'.format(self._host, self._port),
                                                 login=self._user, passcode=self._passw))
                self._client.connect(
                    connectTimeout=timeout, connectedTimeout=timeout)
                self._is_connected = True

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_connected'), self._mh.fromhere())
            ev = event.Event('jms_after_connect')
            self._mh.fire_event(ev)

            return True

        except StompError as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def disconnect(self):
        """Method disconnects from server 

        Args:   
           none       

        Returns:
           bool: result

        """

        try:

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_disconnecting'), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return False
            else:
                self._client.disconnect()
                self._client.close()
                self._is_connected = False
                self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                    'htk_jms_disconnected'), self._mh.fromhere())
                return True

        except StompError as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def send(self, destination_name, message, destination_type='queue', headers={}):
        """Method sends message

        JMS headers - JMSCorrelationID, JMSExpiration, JMSDeliveryMode, JMSPriority,
                      JMSReplyTo, JMSType

        Args:
           destination_name (str): queue|topic name
           message (str): message
           destination_type (str): queue|topic
           headers (dict): JMS headers, key - title, value - string

        Returns:
           bool: result

        Raises:
           event: jms_before_send
           event: jms_after_send             

        """

        try:

            msg = 'destination_name:{0}, message:{1}, destination_type:{2}, headers:{3}'.format(
                destination_name, message, destination_type, headers)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_sending_msg', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return False

            ev = event.Event(
                'jms_before_send', destination_name, message, destination_type, headers)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                message = ev.argv(1)
                destination_type = ev.argv(2)
                headers = ev.argv(3)

            if (ev.will_run_default()):

                headers_new = {}
                for key, value in headers.items():
                    if (key in mapping):
                        headers_new[mapping[key]] = value

                self._client.send('/{0}/{1}'.format(destination_type, destination_name), message if (
                    version_info[0] == 2) else message.encode('utf-8'), headers_new)

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_sent'), self._mh.fromhere())
            ev = event.Event('jms_after_send')
            self._mh.fire_event(ev)

            return True

        except StompError as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return False

    def receive(self, destination_name, cnt=1):
        """Method receives messages

        Args:
           destination_name (str): queue name
           cnt (int): count of messages

        Returns:
           list:  messages as dictionary {'message', JMS headers}

        Raises:
           event: jms_before_receive
           event: jms_after_receive             

        """

        try:

            msg = 'destination_name:{0}, count:{1}'.format(
                destination_name, cnt)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_receiving_msg', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return None

            ev = event.Event('jms_before_receive', destination_name, cnt)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                cnt = ev.argv(1)

            if (ev.will_run_default()):
                token = self._client.subscribe('/queue/{0}'.format(destination_name),
                                               {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})

                msgs = []
                i = 0
                while (i < cnt and self._client.canRead(1)):
                    frame = self._client.receiveFrame()
                    if (frame.command != 'MESSAGE'):
                        break
                    self._client.ack(frame)
                    msgs.append(frame)
                    i = i + 1

                self._client.unsubscribe(token)

                messages = []
                for msg in msgs:

                    message = {}
                    message['message'] = msg.body.decode()
                    for header in msg.rawHeaders:
                        if (header[0] in mapping.values()):
                            message[
                                list(mapping.keys())[list(mapping.values()).index(header[0])]] = header[1]

                    messages.append(message)

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_received', len(messages)), self._mh.fromhere())
            ev = event.Event('jms_after_receive')
            self._mh.fire_event(ev)

            return messages

        except StompError as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return None

    def browse(self, destination_name, cnt=100, jms_correlation_id=None, jms_type=None):
        """Method browses queue

        Args:
           destination_name (str): queue name
           cnt (int): count of messages
           jms_correlation_id (str): requested JMSCorrelationID
           jms_type (str): requested JMSType

        Returns:
           list: messages as dictionary {'message', JMS headers}

        Raises:
           event: jms_before_browse
           event: jms_after_browse              

        """

        try:

            msg = 'destination_name:{0}, count:{1}, jms_correlation_id:{2}, jms_type:{3}'.format(
                destination_name, cnt, jms_correlation_id, jms_type)
            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_browsing', msg), self._mh.fromhere())

            if (not self._is_connected):
                self._mh.demsg('htk_on_warning', self._mh._trn.msg(
                    'htk_jms_not_connected'), self._mh.fromhere())
                return None

            ev = event.Event(
                'jms_before_browse', destination_name, cnt, jms_correlation_id, jms_type)
            if (self._mh.fire_event(ev) > 0):
                destination_name = ev.argv(0)
                cnt = ev.argv(1)
                jms_correlation_id = ev.argv(2)
                jms_type = ev.argv(3)

            if (ev.will_run_default()):
                token = self._client.subscribe('/queue/{0}'.format(destination_name),
                                               {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})

                msgs = []
                i = 0
                while (i < cnt and self._client.canRead(1)):
                    frame = self._client.receiveFrame()

                    correlation_id = None
                    type = None
                    for header in frame.rawHeaders:
                        if (header[0] == 'correlation-id'):
                            correlation_id = header[1]
                        elif (header[0] == 'type'):
                            type = header[1]

                    if ((jms_correlation_id == None or jms_correlation_id == correlation_id) and
                            (jms_type == None or jms_type == type)):
                        msgs.append(frame)
                        i = i + 1

                self._client.unsubscribe(token)

                messages = []
                for msg in msgs:

                    message = {}
                    message['message'] = msg.body.decode()
                    for header in msg.rawHeaders:
                        if (header[0] in mapping.values()):
                            message[
                                list(mapping.keys())[list(mapping.values()).index(header[0])]] = header[1]

                    messages.append(message)

            self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
                'htk_jms_msg_received', len(messages)), self._mh.fromhere())
            ev = event.Event('jms_after_browse')
            self._mh.fire_event(ev)

            return messages

        except StompError as ex:
            self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
            return None
Exemplo n.º 46
0
def enqueue(queue_url, queue_name, data):
    """ Sends a message to a queue """
    client = Stomp(StompConfig(queue_url))
    client.connect()
    client.send(queue_name, data)
    client.disconnect()
Exemplo n.º 47
0
import time
from stompest.config import StompConfig
from stompest.sync import Stomp

server = "hostname"
port = "61613"
vhost = "yourvhost"
login = "******"
passcode = "password"
destination = "/queue/test"  #There're more options other than /queue/...

try:
    client = Stomp(
        StompConfig("tcp://" + server + ":" + port,
                    login=login,
                    passcode=passcode,
                    version="1.2"))
    client.connect(versions=["1.2"], host=vhost,
                   heartBeats=(0, 60000))  #CONNECT
    msgNum = int(input("Quantity of test messages: "))
    for i in range(msgNum):
        message = "test msg " + str(i + 1)
        client.send(destination,
                    body=message,
                    headers={"content-type": "text/plain"},
                    receipt=None)  #SEND
        time.sleep(1)
    client.disconnect()  #DISCONNECT
except Exception, e:
    print e
  def calculate():
    pathwayConfig = app.config[Pathway.CONFIG]
    try:
      ts = str(time.time())

      parameters = dict(request.form)
      for field in parameters:
        parameters[field] = parameters[field][0]
      parameters['idstr'] = ts
      filelist = request.files
      studyList = []

      num_studies = int(parameters['num_studies'])

      for i in xrange(1,num_studies+1):
        studyKey = "study_" + str(i)
        studyObj = {}

        studyObj['lambda'] = parameters['lambda_' + str(i)]
        del parameters['lambda_'+str(i)]

        studyObj['sample_sizes'] = []
        for resourceInd in range(1,int(parameters['num_resource_' + str(i)])+1):
          studyObj['sample_sizes'].append(parameters['sample_size_' + str(i) + '_' + str(resourceInd)])
          del parameters['sample_size_' + str(i) + '_' + str(resourceInd)]
        del parameters['num_resource_' + str(i)]

        studyFile = filelist[studyKey]
        if studyFile.filename:
          filename = os.path.join(os.getcwd(),app.config['UPLOAD_FOLDER'],ts + '-' + str(i) + '.study')
          studyObj['filename'] = filename
          studyFile.save(filename)
        else:
          return Pathway.buildFailure("The file seems to be missing from Study #" + i + ".")
        studyList.append(studyObj)
      del parameters['num_studies']
      parameters['studies'] = studyList

      if parameters['pathway_type'] == 'file_pathway':
        pathFile = filelist['file_pathway']
        if pathFile.filename:
          filename = os.path.join(app.config['UPLOAD_FOLDER'],ts + '.pathway')
          parameters['pathway'] = filename
          pathFile.save(filename)
        else:
          return Pathway.buildFailure("The pathway file seems to be missing.")
      elif parameters['pathway_type'] == 'database_pathway':
        parameters['pathway'] = os.path.join(app.config['PATHWAY_FOLDER'],parameters['database_pathway'])
      else:
        return Pathway.buildFailure("The pathway file seems to be missing.")
      if "database_pathway" in parameters:
        del parameters['database_pathway']

      if "selectAll" in parameters:
        del parameters['selectAll']
      if "selectItem" in parameters:
        del parameters['selectItem']
      superpop = {}
      subpop = {}
      for population in parameters['populations'].split(","):
        population = population.split('|')
        if os.path.isfile(os.path.join(app.config['POPULATION_FOLDER'],population[0],population[1]+'.txt')):
          superpop[population[0]] = 1
          subpop_file = os.path.join(app.config['POPULATION_FOLDER'],population[0],population[1]+'.txt')
          with open(subpop_file, 'r') as subpop_file:
            for line in subpop_file:
              subpop[line.strip()] = 1
        else:
          return Pathway.buildFailure("An invalid population was submitted.")
      if (len(superpop) > 1):
        return Pathway.buildFailure("An invalid population was submitted.")
      del parameters['populations']
      for population in superpop:
        parameters['plink'] = app.config['PLINK_PATTERN'].replace("$pop",population)
      parameters['population'] = []
      for population in subpop:
        parameters['population'].append(population)
      parameters['outdir'] = app.config['OUT_FOLDER']
      parameters['refinep'] = parameters.get('refinep',"").lower() in ['true','t','1']
      parameters['gene_subset'] = parameters.get('gene_subset',"").lower() in ['true','t','1']
      
      jsonout = {"submittedTime": parameters['idstr'], "payload": parameters}
      with open(os.path.join(app.config['OUT_FOLDER'],str(parameters['idstr'])+'.json'),'w') as outfile:
        json.dump(jsonout,outfile)
      
      client = Stomp(pathwayConfig[Pathway.QUEUE_CONFIG])
      client.connect()
      client.send(pathwayConfig.getAsString(Pathway.QUEUE_NAME), json.dumps(parameters))
      client.disconnect()
      return Pathway.buildSuccess("The request has been received. An email will be sent when the calculation has completed.")
    except Exception as e:
      exc_type, exc_obj, exc_tb = sys.exc_info()
      fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
      print("EXCEPTION------------------------------", exc_type, fname, exc_tb.tb_lineno)
      return Pathway.buildFailure(str(e))
Exemplo n.º 49
0
from stompest.config import StompConfig
from stompest.sync import Stomp

user = os.getenv('APOLLO_USER') or 'admin'
password = os.getenv('APOLLO_PASSWORD') or 'password'
host = os.getenv('APOLLO_HOST') or 'localhost'
port = int(os.getenv('APOLLO_PORT') or 61613)
destination = sys.argv[1:2] or ['/topic/event']
destination = destination[0]

messages = 10000
data = 'Hello World from Python'

config = StompConfig('tcp://%s:%d' % (host, port), login=user, passcode=password, version='1.1')
client = Stomp(config)
client.connect(host='mybroker')

count = 0
start = time.time()

for _ in xrange(messages):
    client.send(destination=destination, body=data, headers={'persistent': 'false'})
    count += 1

diff = time.time() - start
print 'Sent %s frames in %f seconds' % (count, diff)
  
client.disconnect(receipt='bye')
client.receiveFrame()
client.close()
Exemplo n.º 50
0
client.send(destination=destination, body="scai.fhg.de/voc/ccg", headers=headers)

for i in queries:
    client.send(destination=destination, body="scai.fhg.de/concept/" + str(i), headers=headers)

print u"# ErasmusMC ontology file".encode('utf-8')
print u"VR 0.0".encode('utf-8')
print u"ON CCG_Ontology".encode('utf-8')
print u"--".encode('utf-8')

@timeout(1)
def receiveFrames():
    frame = clientConsumer.receiveFrame()

    xml = ElementTree.fromstring(frame.body)
    modelText = xml.find('docelem').find('model').text
    if (modelText is not None):
        print unicode(modelText).encode('utf-8')
        print u"--".encode('utf-8')

for reply in queries:
    receiveFrames()

client.disconnect(receipt='bye')
client.receiveFrame()
client.close()
clientConsumer.disconnect(receipt='bye')
clientConsumer.receiveFrame()
clientConsumer.close()
Exemplo n.º 51
0
class PostProcessAdmin:
    def __init__(self, data, conf):
        logging.debug("json data: %s [%s]" % (str(data), type(data)))
        if not type(data) == dict:
            raise ValueError, "PostProcessAdmin expects a data dictionary"
        data["information"] = socket.gethostname()
        self.data = data
        self.conf = conf

        # List of error messages to be handled as information
        self.exceptions = self.conf.exceptions

        stompConfig = StompConfig(self.conf.failover_uri, self.conf.amq_user, self.conf.amq_pwd)
        self.client = Stomp(stompConfig)

        self.data_file = None
        self.facility = None
        self.instrument = None
        self.proposal = None
        self.run_number = None

    def _process_data(self, data):
        """
            Retrieve run information from the data dictionary
            provided with an incoming message.
            @param data: data dictionary
        """
        if data.has_key('data_file'):
            self.data_file = str(data['data_file'])
            if os.access(self.data_file, os.R_OK) == False:
                raise ValueError("Data file does not exist or is not readable: %s" % self.data_file)
        else:
            raise ValueError("data_file is missing: %s" % self.data_file)

        if data.has_key('facility'):
            self.facility = str(data['facility']).upper()
        else:
            raise ValueError("Facility is missing")

        if data.has_key('instrument'):
            self.instrument = str(data['instrument']).upper()
        else:
            raise ValueError("Instrument is missing")

        if data.has_key('ipts'):
            self.proposal = str(data['ipts']).upper()
        else:
            raise ValueError("IPTS is missing")

        if data.has_key('run_number'):
            self.run_number = str(data['run_number'])
        else:
            raise ValueError("Run number is missing")

    def reduce(self, remote=False):
        """
            Reduction process using job submission.
            @param remote: If True, the job will be submitted to a compute node
        """
        self._process_data(self.data)
        try:
            self.send('/queue/' + self.conf.reduction_started, json.dumps(self.data))
            instrument_shared_dir = os.path.join('/', self.facility, self.instrument, 'shared', 'autoreduce')
            proposal_shared_dir = os.path.join('/', self.facility, self.instrument, self.proposal, 'shared', 'autoreduce')
            log_dir = os.path.join(proposal_shared_dir, "reduction_log")
            if not os.path.exists(log_dir):
                os.makedirs(log_dir)

            # Allow for an alternate output directory, if defined
            if len(self.conf.dev_output_dir.strip()) > 0:
                proposal_shared_dir = self.conf.dev_output_dir
            logging.info("Using output directory: %s" % proposal_shared_dir)

            # Look for run summary script
            summary_script = os.path.join(instrument_shared_dir, "sumRun_%s.py" % self.instrument)
            if os.path.exists(summary_script) == True:
                summary_output = os.path.join(proposal_shared_dir, "%s_%s_runsummary.csv" % (self.instrument, self.proposal))
                cmd = "python " + summary_script + " " + self.instrument + " " + self.data_file + " " + summary_output
                logging.debug("Run summary subprocess started: " + cmd)
                subprocess.call(cmd, shell=True)
                logging.debug("Run summary subprocess completed, see " + summary_output)

            # Look for auto-reduction script
            reduce_script_path = os.path.join(instrument_shared_dir, "reduce_%s.py" % self.instrument)
            if os.path.exists(reduce_script_path) == False:
                self.send('/queue/' + self.conf.reduction_disabled, json.dumps(self.data))
                return

            # Run the reduction
            out_log = os.path.join(log_dir, os.path.basename(self.data_file) + ".log")
            out_err = os.path.join(log_dir, os.path.basename(self.data_file) + ".err")
            if remote:
                job_handling.remote_submission(self.conf, reduce_script_path,
                                               self.data_file, proposal_shared_dir,
                                               out_log, out_err)
            else:
                job_handling.local_submission(self.conf, reduce_script_path,
                                              self.data_file, proposal_shared_dir,
                                              out_log, out_err)

            # Determine error condition
            success, status_data = job_handling.determine_success_local(self.conf, out_err)
            self.data.update(status_data)
            if success:
                if os.path.isfile(out_err):
                    os.remove(out_err)
                self.send('/queue/' + self.conf.reduction_complete, json.dumps(self.data))
            else:
                self.send('/queue/' + self.conf.reduction_error, json.dumps(self.data))
        except:
            logging.error("reduce: %s" % sys.exc_value)
            self.data["error"] = "Reduction: %s " % sys.exc_value
            self.send('/queue/' + self.conf.reduction_error , json.dumps(self.data))

    def catalog_raw(self):
        """
            Catalog a nexus file containing raw data
        """
        self._process_data(self.data)
        try:
            from ingest_nexus import IngestNexus
            self.send('/queue/' + self.conf.catalog_started, json.dumps(self.data))
            if self.conf.comm_only is False:
                ingestNexus = IngestNexus(self.data_file)
                ingestNexus.execute()
                ingestNexus.logout()
                self.send('/queue/' + self.conf.catalog_complete, json.dumps(self.data))
        except:
            logging.error("catalog_raw: %s" % sys.exc_value)
            self.data["error"] = "Catalog: %s" % sys.exc_value
            self.send('/queue/' + self.conf.catalog_error, json.dumps(self.data))

    def catalog_reduced(self):
        """
            Catalog reduced data files for a given run
        """
        self._process_data(self.data)
        try:
            from ingest_reduced import IngestReduced
            self.send('/queue/' + self.conf.reduction_catalog_started, json.dumps(self.data))

            if self.conf.comm_only is False:
                # Send image to the web monitor
                if len(self.conf.web_monitor_url.strip()) > 0:
                    monitor_user = {'username': self.conf.amq_user, 'password': self.conf.amq_pwd}
                    proposal_shared_dir = os.path.join('/', self.facility, self.instrument, self.proposal, 'shared', 'autoreduce')

                    url_template = string.Template(self.conf.web_monitor_url)
                    url = url_template.substitute(instrument=self.instrument, run_number=self.run_number)

                    pattern = self.instrument + "_" + self.run_number + "*"
                    for dirpath, dirnames, filenames in os.walk(proposal_shared_dir):
                        listing = glob.glob(os.path.join(dirpath, pattern))
                        for filepath in listing:
                            f, e = os.path.splitext(filepath)
                            if e.startswith(os.extsep):
                                e = e[len(os.extsep):]
                                if e == "png" or e == "jpg" or filepath.endswith("plot_data.dat") or filepath.endswith("plot_data.json"):
                                    files = {'file': open(filepath, 'rb')}
                                    # Post the file if it's small enough
                                    if len(files) != 0 and os.path.getsize(filepath) < self.conf.max_image_size:
                                        request = requests.post(url, data=monitor_user, files=files, verify=False)
                                        logging.info("Submitted %s [status: %s]" % (filepath,
                                                                                    request.status_code))

                ingestReduced = IngestReduced(self.facility, self.instrument, self.proposal, self.run_number)
                ingestReduced.execute()
                ingestReduced.logout()
            self.send('/queue/' + self.conf.reduction_catalog_complete , json.dumps(self.data))
        except:
            logging.error("catalog_reduced: %s" % sys.exc_value)
            self.data["error"] = "Reduction catalog: %s" % sys.exc_value
            self.send('/queue/' + self.conf.reduction_catalog_error , json.dumps(self.data))

    def create_reduction_script(self):
        """
            Create a new reduction script from a template
        """
        try:
            import reduction_script_writer
            writer = reduction_script_writer.ScriptWriter(self.data["instrument"])
            writer.process_request(self.data,
                                   configuration=self.conf,
                                   send_function=self.send)
        except:
            logging.error("create_reduction_script: %s" % sys.exc_value)

    def send(self, destination, data):
        """
            Send an AMQ message
            @param destination: AMQ queue to send to
            @param data: payload of the message
        """
        logging.info("%s: %s" % (destination, data))
        self.client.connect()
        self.client.send(destination, data)
        self.client.disconnect()
Exemplo n.º 52
0
class StompClient(BaseComponent):
    """ Send and Receive messages from a STOMP queue """
    channel = "stomp"

    def init(self, host, port, username=None, password=None,
             connect_timeout=3, connected_timeout=3,
             version=StompSpec.VERSION_1_2, accept_versions=["1.0", "1.1", "1.2"],
             heartbeats=(0, 0), ssl_context=None,
             use_ssl=True,
             key_file=None,
             cert_file=None,
             ca_certs=None,
             ssl_version=ssl.PROTOCOL_SSLv23,
             key_file_password=None,
             proxy_host=None,
             proxy_port=None,
             proxy_user=None,
             proxy_password=None,
             channel=channel):
        """ Initialize StompClient.  Called after __init__ """
        self.channel = channel
        if proxy_host:
            LOG.info("Connect to %s:%s through proxy %s:%d", host, port, proxy_host, proxy_port)
        else:
            LOG.info("Connect to %s:%s", host, port)

        if use_ssl and not ssl_context:

            ssl_params = dict(key_file=key_file,
                              cert_file=cert_file,
                              ca_certs=ca_certs,
                              ssl_version=ssl_version,
                              password=key_file_password)
            LOG.info("Request to use old-style socket wrapper: %s", ssl_params)
            ssl_context = ssl_params

        if use_ssl:
            uri = "ssl://%s:%s" % (host, port)
        else:
            uri = "tcp://%s:%s" % (host, port)

        # Configure failover options so it only tries to connect once
        self._stomp_server = "failover:(%s)?maxReconnectAttempts=1,startupMaxReconnectAttempts=1" % uri

        self._stomp_config = StompConfig(uri=self._stomp_server, sslContext=ssl_context,
                                         version=version,
                                         login=username,
                                         passcode=password)

        self._heartbeats = heartbeats
        self._accept_versions = accept_versions
        self._connect_timeout = connect_timeout
        self._connected_timeout = connected_timeout
        Stomp._transportFactory = EnhancedStompFrameTransport
        Stomp._transportFactory.proxy_host = proxy_host
        Stomp._transportFactory.proxy_port = proxy_port
        Stomp._transportFactory.proxy_user = proxy_user
        Stomp._transportFactory.proxy_password = proxy_password
        self._client = Stomp(self._stomp_config)
        self._subscribed = {}
        self.server_heartbeat = None
        self.client_heartbeat = None
        self.ALLOWANCE = 2  # multiplier for heartbeat timeouts

    @property
    def connected(self):
        if self._client.session:
            return self._client.session.state == StompSession.CONNECTED
        else:
            return False

    @property
    def subscribed(self):
        return self._subscribed.keys()

    @property
    def stomp_logger(self):
        return LOG_CATEGORY

    @handler("disconnect")
    def _disconnect(self, receipt=None):
        if self.connected:
            self._client.disconnect(receipt=receipt)
        self._client.close(flush=True)
        self.fire(disconnected(reconnect=False))
        self._subscribed = {}
        return "disconnected"

    def start_heartbeats(self):
        LOG.info("Client HB: %s  Server HB: %s", self._client.clientHeartBeat, self._client.serverHeartBeat)
        if self._client.clientHeartBeat:
            if self.client_heartbeat:
                # Timer already exists, just reset it
                self.client_heartbeat.reset()
            else:
                LOG.info("Client will send heartbeats to server")
                # Send heartbeats at 80% of agreed rate
                self.client_heartbeat = Timer((self._client.clientHeartBeat / 1000.0) * 0.8,
                                              client_heartbeat(), persist=True)
                self.client_heartbeat.register(self)
        else:
            LOG.info("No Client heartbeats will be sent")

        if self._client.serverHeartBeat:
            if self.server_heartbeat:
                # Timer already exists, just reset it
                self.server_heartbeat.reset()
            else:
                LOG.info("Requested heartbeats from server.")
                # Allow a grace period on server heartbeats
                self.server_heartbeat = Timer((self._client.serverHeartBeat / 1000.0) * self.ALLOWANCE,
                                              server_heartbeat(), persist=True)
                self.server_heartbeat.register(self)
        else:
            LOG.info("Expecting no heartbeats from Server")

    @handler("connect")
    def connect(self, event, host=None, *args, **kwargs):
        """ connect to Stomp server """
        LOG.info("Connect to Stomp...")
        try:
            self._client.connect(heartBeats=self._heartbeats,
                                 host=host,
                                 versions=self._accept_versions,
                                 connectTimeout=self._connect_timeout,
                                 connectedTimeout=self._connected_timeout)
            LOG.info("State after Connection Attempt: %s", self._client.session.state)
            if self.connected:
                LOG.info("Connected to %s", self._stomp_server)
                self.fire(connected())
                self.start_heartbeats()
                return "success"

        except StompConnectionError:
            LOG.debug(traceback.format_exc())
            self.fire(connection_failed(self._stomp_server))
            event.success = False
        return "fail"

    @handler("server_heartbeat")
    def check_server_heartbeat(self, event):
        """ Confirm that heartbeat from server hasn't timed out """
        now = time.time()
        last = self._client.lastReceived or 0
        if last:
            elapsed = now - last
        else:
            elapsed = -1
        LOG.debug("Last received data %d seconds ago", elapsed)
        if ((self._client.serverHeartBeat / 1000.0) * self.ALLOWANCE + last) < now:
            LOG.error("Server heartbeat timeout. %d seconds since last heartbeat.  Disconnecting.", elapsed)
            event.success = False
            self.fire(heartbeat_timeout())
            if self.connected:
                self._client.disconnect()
            # TODO: Try to auto-reconnect?

    @handler("client_heartbeat")
    def send_heartbeat(self, event):
        if self.connected:
            LOG.debug("Sending heartbeat")
            try:
                self._client.beat()
            except StompConnectionError:
                event.success = False
                self.fire(disconnected())

    @handler("generate_events")
    def generate_events(self, event):
        if not self.connected:
            return
        try:
            if self._client.canRead(1):
                frame = self._client.receiveFrame()
                LOG.debug("Recieved frame %s", frame)
                self.fire(message(frame))
        except StompConnectionError:
            self.fire(disconnected())

    @handler("send")
    def send(self, event, destination, body, headers=None, receipt=None):
        LOG.debug("send()")
        if not self.connected:
            LOG.error("Can't send when Stomp is disconnected")
            self.fire(on_stomp_error(None, Exception("Message send attempted with stomp disconnected")))
            event.success = False
            return
        try:
            self._client.send(destination, body=body.encode('utf-8'), headers=headers, receipt=receipt)
            LOG.debug("Message sent")
        except StompConnectionError as err:
            event.success = False
            self.fire(disconnected())
        except StompError as err:
            LOG.error("Error sending ack")
            event.success = False
            self.fire(on_stomp_error(None, err))

    @handler("subscribe")
    def _subscribe(self, event, destination, ack=ACK_CLIENT_INDIVIDUAL):
        if ack not in ACK_MODES:
            raise ValueError("Invalid client ack mode specified")
        LOG.info("Subscribe to message destination %s", destination)
        try:
            # Set ID to match destination name for easy reference later
            frame, token = self._client.subscribe(destination,
                                                  headers={StompSpec.ACK_HEADER: ack,
                                                           'id': destination})
            self._subscribed[destination] = token
        except StompConnectionError as err:
            event.success = False
            self.fire(disconnected())
        except StompError as err:
            event.success = False
            LOG.debug(traceback.format_exc())
            self.fire(on_stomp_error(None, err))

    @handler("unsubscribe")
    def _unsubscribe(self, event, destination):
        if destination not in self._subscribed:
            LOG.error("Unsubscribe Request Ignored. Not subscribed to %s", destination)
            return
        try:
            token = self._subscribed.pop(destination)
            frame = self._client.unsubscribe(token)
            LOG.debug("Unsubscribed: %s", frame)
        except StompConnectionError as err:
            event.success = False
            self.fire(disconnected())
        except StompError as err:
            LOG.error("Error sending ack")
            event.success = False
            self.fire(on_stomp_error(frame, err))

    @handler("message")
    def on_message(self, event, headers, message):
        LOG.info("Stomp message received")

    @handler("ack")
    def ack_frame(self, event, frame):
        LOG.debug("ack_frame()")
        try:
            self._client.ack(frame)
            LOG.debug("Ack Sent")
        except StompConnectionError as err:
            LOG.error("Error sending ack")
            event.success = False
            self.fire(disconnected())
        except StompError as err:
            LOG.error("Error sending ack")
            event.success = False
            self.fire(on_stomp_error(frame, err))

    def get_subscription(self, frame):
        """ Get subscription from frame """
        LOG.info(self._subscribed)
        _, token = self._client.message(frame)
        return self._subscribed[token]
Exemplo n.º 53
0
from stompest.config import StompConfig
from stompest.protocol import StompSpec
from stompest.sync import Stomp

CONFIG = StompConfig('tcp://localhost:61613', version=StompSpec.VERSION_1_1)
QUEUE = '/queue/test'

if __name__ == '__main__':
  client = Stomp(CONFIG)
  client.connect(heartBeats=(0, 10000))
  client.subscribe(QUEUE, {StompSpec.ID_HEADER: 1, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
  client.send(QUEUE, 'test message 1')
  client.send(QUEUE, 'test message 2')
  while True:
    frame = client.receiveFrame()
    print 'Got %s' % frame.info()
    client.ack(frame)
  client.disconnect()

Exemplo n.º 54
0
class Client(object):
	def __init__(self):
		self.stompest = None
		self.greenlet = None
		self.subscriptions = {}
		self._last_id = 0

	def _next_id(self):
		self._last_id += 1
		return self._last_id

	def connect(self):
		if not self.stompest:
			CONFIG, EXTRA = _get_config()
			self._hostname = EXTRA.get('hostname', None)
			self.stompest = Stomp(CONFIG)

		if self.stompest.session.state != StompSession.DISCONNECTED:
			return

		while True:
			try:
				self.stompest.connect(host=self._hostname)
				logger.info('Connected')
				break
			except StompConnectTimeout:
				continue

		if not self.greenlet:
			self.greenlet = gevent.spawn(self._run)

	def _run(self):
		while True:
			try:
				frame = self.stompest.receiveFrame()
				self.stompest.ack(frame)
				if frame.command == 'ERROR':
					logger.error(frame.info())
				elif frame.command == 'MESSAGE':
					token = self.stompest.message(frame)
					if self.subscriptions.get(token):
						subscription = self.subscriptions[token]
						subscription.call(frame)
					else:
						logger.error("Received a message for %s (%s) but there was no matching subscription."
							% (frame.headers.get(StompSpec.DESTINATION_HEADER, '???'), token))
				else:
					logger.warning("Unknown frame: %s" % frame.info())
				# @todo Handle receipts
			except (gevent.GreenletExit, KeyboardInterrupt):
				# @todo Include a receipt in the disconnect. And instead of breaking right away wait for the
				#       receipt frame before disconnecting and consider waiting on any greenlets we started.
				self.stompest.disconnect()
				break
			except StompConnectionError:
				# We've been disconnected from the server. Try reconnecting to it.
				self.connect()

	def on(self, destination, callback):
		self.connect()
		token = self.stompest.subscribe(destination, {
			StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL,
			StompSpec.ID_HEADER: self._next_id(),
		})
		subscription = Subscription(
			conn=self,
			destination=destination,
			token=token,
			callback=callback
		)
		self.subscriptions[subscription.token] = subscription;

	# @todo consider adding optional support for additional custom headers
	def send(self, cmd, destination):
		self.connect()
		body = json.dumps(cmd)
		headers = {}
		headers[StompSpec.CONTENT_TYPE_HEADER] = 'application/json;charset=UTF-8'
		self.stompest.send(destination, body, headers)

	def join(self):
		try:
			self.connect()
		except (gevent.GreenletExit, KeyboardInterrupt):
			return
		try:
			gevent.joinall([self.greenlet])
		except KeyboardInterrupt:
			self.greenlet.kill(block=True)