Getting Started with AMQP, Qpid, Python & Fedora

The Advanced Message Queuing Protocol (AMQP) is the first open standard for Enterprise Messaging and has been adopted by Cisco, [Microsoft][microsoft], Novel, Red Hat and others.

One implementation of AMQP currently packaged by Red Hat and Fedora is Qpid. Qpid is a collection of brokers (C++, Java) and clients (C++, Java, Python, Ruby) which aims to be 100% AMQP Compliant.

Below is a step-by-step tutorial on getting started with Qpid and the Python client in Fedora.

  1. Install the Qpid broker and Python client

    yum install qpidd python-qpid
    
  2. Start the broker

    service qpidd start
    
  3. Create a message producer (producer.py)

    from qpid.connection import Connection
    from qpid.datatypes import Message, uuid4
    from qpid.util import connect
    
    # Create connection and session
    socket = connect('localhost', 5672)
    connection = Connection(sock=socket, username='guest', password='guest')
    connection.start()
    session = connection.session(str(uuid4()))
    
    # Setup queue
    session.queue_declare(queue='message_queue')
    session.exchange_bind(exchange='amq.direct', queue='message_queue', binding_key='routing_key')
    
    # Setup routing properties
    properties = session.delivery_properties(routing_key='routing_key')
    
    # Send messages
    session.message_transfer(destination='amq.direct', message=Message(properties, 'Message one'))
    session.message_transfer(destination='amq.direct', message=Message(properties, 'Message two'))
    session.message_transfer(destination='amq.direct', message=Message(properties, 'Done'))
    
    # Close session
    session.close(timeout=10)
    
  4. Create a message consumer (consumer.py)

    from qpid.connection import Connection
    from qpid.datatypes import RangedSet, uuid4
    from qpid.util import connect
    
    # Create connection and session
    socket = connect('localhost', 5672)
    connection = Connection(sock=socket, username='guest', password='guest')
    connection.start()
    session = connection.session(str(uuid4()))
    
    # Define local queue
    local_queue_name = 'my_local_queue'
    
    # Create local queue
    queue = session.incoming(local_queue_name)
    
    # Route messages from message_queue to my_local_queue
    session.message_subscribe(queue='message_queue', destination=local_queue_name)
    queue.start()
    
    content = ''
    
    while content != 'Done':
        # Get message from the local queue
        message = queue.get(timeout=10)
        # Get body of the message
        content = message.body
        # Accept message (removes it from the queue)
        session.message_accept(RangedSet(message.id))
        # Print message content
        print content
    
    # Close session
    session.close(timeout=10)
    
  5. First run the producer and then the consumer

    Message one
    Message two
    Done
    

Source: Qpid direct example

String Slicing in Bash (like Python)

A simple function to slice strings in Bash similar to Python's string slicing functionality.

Examples

[silas@pluto ~]$ string_slice "12345" 0 1
1
[silas@pluto ~]$ string_slice "12345" 0 3
123
[silas@pluto ~]$ string_slice "12345" 2 3
3
[silas@pluto ~]$ string_slice "12345" 2 -2
3
[silas@pluto ~]$ string_slice "12345" -3
345

Implementation

function string_slice {
    STRING="$1"
    declare -i LENGTH="${#STRING}"
    declare -i START="$2"
    declare -i END="$3"
    if [ $START -lt 0 ]; then
        START=$[ $LENGTH + $START ]
    fi
    if [ $END -le 0 ]; then
        END=$[ $LENGTH + $END ]
    fi
    START=$[ $START + 1 ]
    (echo "$STRING" | cut -c $START-$END) 2> /dev/null
}

Profile Management with Git and GitHub

The following describes a simple way to manage you profile configuration files using GitHub.

Features

  • Centralized configuration management
  • Files live in their native locations (no symbolic linking)
  • Home directory is not a Git repository
  • All the power of git with a simple alias

Setup Repository

  • Log into GitHub and create a repository named config
  • Add your public keys to GitHub (if you haven't done so already)
  • Open a terminal and switch to your home directory

    cd ~
    
  • Create a configuration directory

    mkdir .config.git
    
  • Add the following alias to your current session and your .bash_profile

    alias config='git --git-dir=$HOME/.config.git/ --work-tree=$HOME'
    echo "alias config='git --git-dir=$HOME/.config.git/ --work-tree=$HOME'" >> .bash_profile
    
  • Add your .bash_profile to the configuration repository

    config add .bash_profile
    
  • Commit the changes

    config commit -m 'Initial commit'
    
  • Change the origin to GitHub

    config remote add origin git@github.com:GITHUB_USERNAME/config.git
    
  • Push the changes

    config push origin master
    

If you get an error when running config pull to the effect of You asked me to pull without... run the follow:

    echo -e '[branch "master"]\n  remote = origin\n  merge = refs/heads/master' >> ~/.config.git/config

Setup Configuration Management on a Different System

  1. Add your public keys to GitHub (if you haven't done so already)
  2. Switch to your home directory

    cd ~
    
  3. Backup your local configuration files, example:

    mv .bash_profile .bash_profile.bk
    
  4. Clone your configuration repository

    git clone git@github.com:GITHUB_USERNAME/config.git config.git
    
  5. Move the git metadata to ~/.config.git

    mv config.git/.git .config.git
    
  6. Enable dotglob

    shopt -s dotglob
    
  7. Move your configuration files to your home directory

    mv -i config.git/* .
    
  8. Delete the config.git directory

    rmdir config.git
    
  9. Logout and log back in

Basic Usage

  • config pull - get latest configuration changes
  • config add FILENAME - add a configuration file
  • config commit -a - save all configuration changes
  • config push - push configuration changes to GitHub
  • and any other config GIT_OPTION

You can see my configuration repository at http://github.com/silas/config.

Source: Manage your $HOME with git by Robert Escriva