Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect failure :: publisher #293

Closed
madkote opened this issue Mar 5, 2020 · 2 comments
Closed

Reconnect failure :: publisher #293

madkote opened this issue Mar 5, 2020 · 2 comments
Labels

Comments

@madkote
Copy link

madkote commented Mar 5, 2020

Please review the following publisher example.

import logging
import sys
import time

import stomp

def stomp_connect(conn, user='guest', password='guest'):
    conn.connect(login=user, passcode=password, wait=True)

class ReconnectListener(stomp.ConnectionListener):
        def __init__(self, conn):
            self.conn = conn

        def on_error(self, headers, message):
            print('received an error "%s"' % message)

        def on_message(self, headers, message):
            print('received a message "%s"' % message)
            for x in range(10):
                print(x)
                time.sleep(1)
            print('processed message')

        def on_disconnected(self):
            print('disconnected')
            stomp_connect(self.conn)

def demo_publish():
    print()
    print('+----------------------------------------+')
    print('| Single ActiveMQ publisher example         |')
    print('+----------------------------------------+')
    #
    #
    host_and_ports = [('localhost', 61613)]
    attempts = 30
    name_queue = 'test_queue'
    #
    #
    conn = stomp.Connection(
        host_and_ports=host_and_ports,
        reconnect_attempts_max=attempts
    )
    conn.set_listener('', ReconnectListener(conn))
    stomp_connect(conn)
    #
    #
    counter = 0
    sleep = 13.0
    try:
        while True:
            try:
                print('>> send', counter)
                conn.send(
                    name_queue,
                    'message #%s, time=%s' % (counter, time.time())
                )
                print('>>> >>> sleep')
                counter += 1
                time.sleep(sleep)
            except KeyboardInterrupt:
                break
    finally:
        print('> disconnect')
        conn.disconnect()
        print('> disconnect done')

if __name__ == '__main__':
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
    demo_publish()

python: 3.7
stomp: 6.0.0

Normally, the pubilisher sends all messages. If you shutdown the ActiveMQ server, and starts again (with docker-compose), then reconnect want work....

python demo_send.py 
INFO:stomp.py:attempt reconnection (True, None, 0)
INFO:stomp.py:Attempting connection to host localhost, port 61613
INFO:stomp.py:Established connection to host localhost, port 61613
INFO:stomp.py:Starting receiver loop (<Thread(Thread-1, started daemon 140657043883776)>)
INFO:stomp.py:Created thread <Thread(Thread-1, started daemon 140657043883776)> using func <function default_create_thread at 0x7fed4604acb0>
DEBUG:stomp.py:Sending frame: [b'STOMP', b'\n', b'accept-version:1.1\n', b'login:guest\n', b'passcode:guest\n', b'\n', b'\x00']
DEBUG:stomp.py:Received frame: 'CONNECTED', headers={'server': 'ActiveMQ/5.15.6', 'heart-beat': '0,0', 'session': 'ID:activemq-46317-1583420355347-3:1', 'version': '1.1'}, body=''
DEBUG:stomp.py:Heartbeats calculated (0, 0)
>> send 0
DEBUG:stomp.py:Sending frame: [b'SEND', b'\n', b'content-length:34\n', b'destination:test_queue\n', b'\n', b'message #0, time=1583420399.842412', b'\x00']
>>> >>> sleep
DEBUG:stomp.py:nothing received, raising CCE
INFO:stomp.py:Receiver loop ended
disconnected
INFO:stomp.py:attempt reconnection (True, None, 0)
INFO:stomp.py:Attempting connection to host localhost, port 61613
INFO:stomp.py:Established connection to host localhost, port 61613
INFO:stomp.py:Starting receiver loop (<Thread(Thread-2, started daemon 140657035491072)>)
INFO:stomp.py:Created thread <Thread(Thread-2, started daemon 140657035491072)> using func <function default_create_thread at 0x7fed4604acb0>
DEBUG:stomp.py:nothing received, raising CCE
DEBUG:stomp.py:Sending frame: [b'STOMP', b'\n', b'accept-version:1.1\n', b'login:guest\n', b'passcode:guest\n', b'\n', b'\x00']
ERROR:stomp.py:Error sending frame
Traceback (most recent call last):
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 630, in send
    self.socket.sendall(encoded_frame)
OSError: [Errno 9] Bad file descriptor
INFO:stomp.py:Receiver loop ended
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 360, in __receiver_loop
    self.notify("disconnected")
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 230, in notify
    notify_func()
  File "demo_send.py", line 43, in on_disconnected
    stomp_connect(self.conn)
  File "demo_send.py", line 24, in stomp_connect
    conn.connect(login=user, passcode=password, wait=True)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/connect.py", line 162, in connect
    Protocol11.connect(self, *args, **kwargs)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/protocol.py", line 332, in connect
    self.send_frame(cmd, headers)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/protocol.py", line 244, in send_frame
    self.transport.transmit(frame)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 271, in transmit
    self.send(packed_frame)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 634, in send
    raise e
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 630, in send
    self.socket.sendall(encoded_frame)
OSError: [Errno 9] Bad file descriptor

>> send 1
DEBUG:stomp.py:Sending frame: [b'SEND', b'\n', b'content-length:33\n', b'destination:test_queue\n', b'\n', b'message #1, time=1583420412.85417', b'\x00']
> disconnect
  File "demo_send.py", line 89, in <module>
    demo_publish()
  File "demo_send.py", line 83, in demo_publish
    conn.disconnect()
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/connect.py", line 172, in disconnect
    Protocol11.disconnect(self, receipt, headers, **keyword_headers)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/protocol.py", line 351, in disconnect
    traceback.print_stack()
DEBUG:stomp.py:Not sending disconnect, already disconnected
> disconnect done
Traceback (most recent call last):
  File "demo_send.py", line 89, in <module>
    demo_publish()
  File "demo_send.py", line 74, in demo_publish
    'message #%s, time=%s' % (counter, time.time())
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/protocol.py", line 398, in send
    self.send_frame(CMD_SEND, headers, body)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/protocol.py", line 244, in send_frame
    self.transport.transmit(frame)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 271, in transmit
    self.send(packed_frame)
  File "/home/madkote/demo-stomp/venv/lib/python3.7/site-packages/stomp/transport.py", line 636, in send
    raise exception.NotConnectedException()
stomp.exception.NotConnectedException

The sleep value is high enough to prevent sending during reconnection and the issue occurs before actual sending.

@madkote
Copy link
Author

madkote commented Mar 5, 2020

Is the intention reconnect is good only for listener connections correct?

@madkote
Copy link
Author

madkote commented Mar 5, 2020

One more comment: also when synchronizing send and reconnect -> the issue is same.

@madkote madkote changed the title Reconnect failing :: publisher Reconnect failure :: publisher Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants