# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import time

import stomp

from MQ import *

class MsgForwarder(object):
    def __init__(self, con):
        self.con = con
        self.count = 0
    def on_message(self, headers, message):
            try:
                if self.count % 2 != 0:
                    raise Problem()
                self.con.send('NonXAout', message)
            except Problem:
                print('Problem')
            self.count = self.count + 1

class Bad(DB):
    def get_connection(self):
        con = stomp.Connection([('localhost', 61613)])
        con.start()
        con.connect()
        return con
    def test(self):
        con = self.get_connection()
        con.set_listener('forwarder', MsgForwarder(con))
        con.subscribe(destination='NonXAin', id=0, ack='auto')
        time.sleep(0.1)
        con.remove_listener('forwarder')
        con.disconnect()

demo(Bad());
