Last active
August 29, 2015 14:08
-
-
Save jgcasta/d966a129ede5282697d8 to your computer and use it in GitHub Desktop.
Read message variable from class listener when using Stomp interface to ActiveMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# ------------------------------------------------------------------------ | |
# Licensed to the Apache Software Foundation (ASF) under one or more | |
# contributor license agreements. See the NOTICE file distributed with | |
# this work for additional information regarding copyright ownership. | |
# The ASF licenses this file to You under the Apache License, Version 2.0 | |
# (the "License"); you may not use this file except in compliance with | |
# the License. You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# ------------------------------------------------------------------------ | |
''' | |
Modified to access the message attribute from outside the class | |
jgcasta at gmail dot com | |
2014-11-04 | |
''' | |
import time | |
import sys | |
import os | |
import stomp | |
user = os.getenv("ACTIVEMQ_USER") or "admin" | |
password = os.getenv("ACTIVEMQ_PASSWORD") or "password" | |
host = os.getenv("ACTIVEMQ_HOST") or "localhost" | |
port = os.getenv("ACTIVEMQ_PORT") or 61613 | |
destination = sys.argv[1:2] or ["/topic/event"] | |
destination = destination[0] | |
class MyListener(object): | |
def __init__(self, conn): | |
self.conn = conn | |
self.count = 0 | |
self.start = time.time() | |
# attribute class definition | |
self.variable = '' | |
def on_error(self, headers, message): | |
print('received an error %s' % message) | |
def on_message(self, headers, message): | |
# write variable content | |
self.variable = message | |
conn = stomp.Connection(host_and_ports = [(host, port)]) | |
# create MyListener object and connect to ActiveMQ | |
ojbListener = MyListener(conn) | |
conn.set_listener('', ojbListener) | |
conn.start() | |
conn.connect(login=user,passcode=password) | |
conn.subscribe(destination=destination, ack='auto') | |
print("Waiting for messages...") | |
# where you need read variable | |
print objListener.variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment