Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
utils
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
f-interop-contributors
utils
Commits
6e66e290
Commit
6e66e290
authored
Nov 30, 2017
by
Federico Sismondi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
event bus listener component enhacements
parent
2830d227
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
29 deletions
+26
-29
amqp_synch_call.py
amqp_synch_call.py
+0
-2
event_bus_utils.py
event_bus_utils.py
+26
-27
No files found.
amqp_synch_call.py
View file @
6e66e290
...
...
@@ -170,5 +170,3 @@ if __name__ == '__main__':
# r = amqp_request(connection, m, 'someImaginaryComponent')
# print(repr(r))
event_bus_utils.py
View file @
6e66e290
import
os
import
pika
import
logging
import
threading
# for using it as library and as a __main__
...
...
@@ -15,25 +16,23 @@ AMQP_EXCHANGE = 'amq.topic'
class
AmqpSynchCallTimeoutError
(
Exception
):
pass
class
AmqpListener
(
threading
.
Thread
):
COMPONENT_ID
=
'amqp_listener_
%
s'
%
uuid
.
uuid1
()
DEFAULT_TOPIC_SUSBCRIPTIONS
=
[
'#'
]
DEFAULT_EXCHAGE
=
'amq.topic'
def
__init__
(
self
,
amqp_url
,
amqp_exchange
,
topics
,
callback
):
def
__init__
(
self
,
amqp_url
,
amqp_exchange
,
callback
,
topics
=
None
):
threading
.
Thread
.
__init__
(
self
)
if
callback
is
None
:
self
.
message_dipatcher
=
print
self
.
message_di
s
patcher
=
print
else
:
self
.
message_dipatcher
=
callback
self
.
message_di
s
patcher
=
callback
try
:
self
.
connection
=
pika
.
BlockingConnection
(
pika
.
URLParameters
(
amqp_url
))
self
.
channel
=
self
.
connection
.
channel
()
except
pika
.
exceptions
.
ProbableAccessDeniedError
:
self
.
message_dipatcher
(
'Probable access denied error. Is provided AMQP_URL correct?'
)
self
.
exit
()
self
.
connection
=
pika
.
BlockingConnection
(
pika
.
URLParameters
(
amqp_url
))
self
.
channel
=
self
.
connection
.
channel
()
if
amqp_exchange
:
self
.
exchange
=
amqp_exchange
...
...
@@ -47,15 +46,15 @@ class AmqpListener(threading.Thread):
arguments
=
{
'x-max-length'
:
200
})
if
topics
:
# subscribe only to passed list
for
t
in
topics
:
self
.
channel
.
queue_bind
(
exchange
=
self
.
exchange
,
queue
=
self
.
services_queue_name
,
routing_key
=
t
)
self
.
topics
=
topics
else
:
self
.
topics
=
self
.
DEFAULT_EXCHAGE
else
:
# subscribe to all events
for
t
in
self
.
topics
:
self
.
channel
.
queue_bind
(
exchange
=
self
.
exchange
,
queue
=
self
.
services_queue_name
,
routing_key
=
'#'
)
routing_key
=
t
)
# Hello world message
m
=
MsgTestingToolComponentReady
(
component
=
self
.
COMPONENT_ID
,
...
...
@@ -98,29 +97,28 @@ class AmqpListener(threading.Thread):
m
=
Message
.
from_json
(
body
)
m
.
update_properties
(
**
props_dict
)
m
.
routing_key
=
method
.
routing_key
self
.
message_dipatcher
(
m
)
self
.
message_di
s
patcher
(
m
)
except
NonCompliantMessageFormatError
as
e
:
self
.
message_dipatche
r
(
'
%
s got a non compliant message error
%
s'
%
(
self
.
__class__
.
__name__
,
e
))
logging
.
erro
r
(
'
%
s got a non compliant message error
%
s'
%
(
self
.
__class__
.
__name__
,
e
))
except
Exception
as
e
:
pass
# self.message_dipatcher('Error : %s' % str(e))
# self.message_dipatcher(str(body))
logging
.
error
(
e
)
finally
:
ch
.
basic_ack
(
delivery_tag
=
method
.
delivery_tag
)
def
run
(
self
):
self
.
message_dipatcher
(
"Starting thread listening on the event bus.."
)
logging
.
info
(
"Starting thread listening on the event bus on topics
%
s"
%
self
.
topics
)
for
i
in
range
(
1
,
4
):
try
:
self
.
channel
.
start_consuming
()
except
pika
.
exceptions
.
ConnectionClosed
as
err
:
self
.
message_dipatcher
(
err
)
self
.
message_dipatcher
(
'Unexpected connection closed, retrying
%
s/
%
s'
%
(
i
,
4
))
logging
.
error
(
'Unexpected connection closed, retrying
%
s/
%
s'
%
(
i
,
4
))
logging
.
error
(
err
)
logging
.
info
(
'Bye byes!'
)
self
.
message_dipatcher
(
'Bye byes!'
)
def
publish_message
(
connection
,
message
):
"""
...
...
@@ -238,12 +236,14 @@ if __name__ == '__main__':
def
callback_function
(
message_received
):
print
(
"Callback function received:
\n\t
"
+
repr
(
message_received
))
# amqp listener example:
amqp_listener_thread
=
AmqpListener
(
amqp_url
=
AMQP_URL
,
amqp_exchange
=
AMQP_EXCHANGE
,
topics
=
'#'
,
callback
=
callback_function
)
callback
=
callback_function
,
topics
=
'#'
)
try
:
amqp_listener_thread
.
start
()
...
...
@@ -271,4 +271,3 @@ if __name__ == '__main__':
except
AmqpSynchCallTimeoutError
as
e
:
print
(
"Nobody answered to our request :'("
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment