Esempio n. 1
0
def main():
    parser = ArgumentParser(
        description="Simple AMS example of subscription pull/consume")
    parser.add_argument('--host',
                        type=str,
                        default='messaging-devel.argo.grnet.gr',
                        help='FQDN of AMS Service')
    parser.add_argument('--token', type=str, required=True, help='Given token')
    parser.add_argument('--project',
                        type=str,
                        required=True,
                        help='Project  registered in AMS Service')
    parser.add_argument('--subscription',
                        type=str,
                        required=True,
                        help='Subscription name')
    args = parser.parse_args()

    # initialize service with given token and project
    ams = ArgoMessagingService(endpoint=args.host,
                               token=args.token,
                               project=args.project)

    for msg in ams.pullack_sub(args.subscription,
                               retry=5,
                               retrysleep=15,
                               return_immediately=True):
        try:
            data = msg.get_data()
            msgid = msg.get_msgid()
            print('msgid={0}'.format(msgid))
            print('msgdata={0}'.format(data))
        except AmsException as e:
            print(e)
            raise SystemExit(1)
Esempio n. 2
0
def main():
    parser = ArgumentParser(description="Simple AMS message publish example")
    parser.add_argument('--host',
                        type=str,
                        default='messaging-devel.argo.grnet.gr',
                        help='FQDN of AMS Service')
    parser.add_argument('--token', type=str, required=True, help='Given token')
    parser.add_argument('--project',
                        type=str,
                        required=True,
                        help='Project  registered in AMS Service')
    parser.add_argument('--topic', type=str, required=True, help='Given topic')
    parser.add_argument('--subscription',
                        type=str,
                        required=True,
                        help='Subscription name')
    parser.add_argument('--nummsgs',
                        type=int,
                        default=3,
                        help='Number of messages to pull and ack')
    args = parser.parse_args()

    ams = ArgoMessagingService(endpoint=args.host,
                               token=args.token,
                               project=args.project)

    # static sleep between retry attempts
    msg = AmsMessage(data='foo1', attributes={'bar1': 'baz1'}).dict()
    try:
        ret = ams.publish(args.topic, msg, retry=3, retrysleep=5, timeout=5)
        print(ret)
    except AmsException as e:
        print(e)

    # iptables -A OUTPUT -d messaging-devel.argo.grnet.gr -j DROP

    ackids = list()
    for id, msg in ams.pull_sub(args.subscription,
                                args.nummsgs,
                                retry=3,
                                retrysleep=5,
                                timeout=5):
        data = msg.get_data()
        msgid = msg.get_msgid()
        attr = msg.get_attr()
        print('msgid={0}, data={1}, attr={2}'.format(msgid, data, attr))
        ackids.append(id)

    if ackids:
        ams.ack_sub(args.subscription,
                    ackids,
                    retry=3,
                    retrysleep=5,
                    timeout=5)

    # backoff with each next retry attempt exponentially longer
    msg = AmsMessage(data='foo2', attributes={'bar2': 'baz2'}).dict()
    try:
        ret = ams.publish(args.topic, msg, retry=3, retrybackoff=5, timeout=5)
        print(ret)
    except AmsException as e:
        print(e)

    # iptables -A OUTPUT -d messaging-devel.argo.grnet.gr -j DROP

    ackids = list()
    for id, msg in ams.pull_sub(args.subscription,
                                args.nummsgs,
                                retrybackoff=3,
                                retrysleep=5,
                                timeout=5):
        data = msg.get_data()
        msgid = msg.get_msgid()
        attr = msg.get_attr()
        print('msgid={0}, data={1}, attr={2}'.format(msgid, data, attr))
        ackids.append(id)

    if ackids:
        ams.ack_sub(args.subscription, ackids)

    # static sleep between retry attempts. this example uses consume context
    # method that pull and acks msgs in one call.
    msg = AmsMessage(data='foo3', attributes={'bar3': 'baz3'}).dict()
    try:
        ret = ams.publish(args.topic, msg, retry=3, retrysleep=5, timeout=5)
        print(ret)
    except AmsException as e:
        print(e)

    try:
        msgs = ams.pullack_sub(args.subscription,
                               args.nummsgs,
                               retry=3,
                               retrysleep=5,
                               timeout=5)
        for msg in msgs:
            data = msg.get_data()
            msgid = msg.get_msgid()
            attr = msg.get_attr()
            print('msgid={0}, data={1}, attr={2}'.format(msgid, data, attr))

    except AmsException as e:
        print(e)