You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When i created a pulsar topic using mop and when trying to consume that topic it consumes only till the last stored data and not the new data's published to the topic.
To Reproduce
Steps to reproduce the behavior:
setup
Below is the pulsar.service file for Raspberry Pi 4
now a python script is used to send messages to the mqtt broker
importrandomimporttimeimportjsonimportpaho.mqtt.clientasmqttdefsend_data_to_mqtt(broker: str, port: int, topic: str,
message: str) ->None:
""" Sends a message to a specified MQTT topic. Parameters: broker (str): The MQTT broker address. port (int): The port number for the MQTT broker. topic (str): The MQTT topic to which the message will be sent. message (str): The message to be sent to the MQTT topic. Returns: None """# Create an MQTT client instanceclient=mqtt.Client()
# Connect to the MQTT brokerclient.connect(broker, port)
# Publish the message to the specified topicclient.publish(topic, message)
# Disconnect from the brokerclient.disconnect()
# Example usageif__name__=="__main__":
broker_address="192.168.1.21"broker_port=1883# Default MQTT port# mqtt_topic = "persistent://public/default/vishnu-mqtt-topic"mqtt_topic="vishnu-mqtt-topic"whileTrue:
data= {
"key": random.randint(0, 100)
}
data_to_send=json.dumps(data)
# data_to_send = data_to_send + str(random.randint(0, 100))send_data_to_mqtt(broker_address, broker_port, mqtt_topic, data_to_send)
print(json.dumps(data))
time.sleep(1)
This sends data continuously without any problem
Now i created a consumer using py
importpulsarimporttimefrompulsarimportConsumerdefcreate_consumer(client: pulsar.Client, topic: str, subscription_name: str) ->pulsar.Consumer:
"""Creates a Pulsar consumer and returns it."""returnclient.subscribe(
topic=topic,
subscription_name=subscription_name,
consumer_type=pulsar.ConsumerType.Shared, # Exclusive for testinginitial_position=pulsar.InitialPosition.Earliest,
)
defreconnect(client: pulsar.Client, topic: str, subscription_name: str) ->Consumer|None:
"""Attempts to reconnect and recreate the consumer."""try:
print("Attempting to reconnect...")
time.sleep(5) # Avoid spamming reconnection attemptsreturncreate_consumer(client, topic, subscription_name)
exceptExceptionase:
print(f"Reconnection failed: {e}")
returnNonedefconsume_messages(consumer: pulsar.Consumer, client: pulsar.Client, topic: str, subscription_name: str):
"""Handles the consumption of messages, including connection handling and reconnection logic."""whileTrue:
msg=Nonetry:
# Check the connection and receive a message with timeoutifconsumer.is_connected():
msg=consumer.receive(timeout_millis=5000) # Timeout of 5 seconds# consumer.close()# print(msg.data().decode('utf-8'))else:
consumer=reconnect(client, topic, subscription_name)
print("Connection lost")
ifmsg:
# Decode and process the messagemessage_data=msg.data().decode('utf-8')
message_id=msg.message_id()
print(f"\n\nReceived message: '{message_data}' \nid: '{message_id}'")
# Acknowledge successful processingconsumer.acknowledge(msg)
else:
print("No new messages within the timeout period. Waiting...")
exceptpulsar.Timeout:
# Handle timeout and continue listeningprint("Timeout occurred. No new messages available. Checking again...")
# print(msg.data().decode('utf-8'))exceptpulsar.ConnectErrorase:
# Handle connection error and attempt to reconnectprint(f"Connection error: {e}")
consumer=reconnect(client, topic, subscription_name)
ifconsumerisNone:
# Exit if reconnection failsbreakexceptExceptionase:
print(f"Error processing message: {e}")
ifmsg:
consumer.negative_acknowledge(msg)
exceptKeyboardInterrupt:
print("KeyboardInterrupt detected. Exiting...")
defmain():
# Initialize Pulsar clientclient=pulsar.Client('pulsar://192.168.1.21:6650')
# Define topic and subscriptiontopic="persistent://public/default/vishnu-mqtt-topic"subscription_name='pulsar-mqtt123-testing-subscription'# Create initial consumerconsumer=create_consumer(client, topic, subscription_name)
# Consume messagesconsume_messages(consumer, client, topic, subscription_name)
# Close the client connection upon exitingprint("Closing client connection...")
client.close()
if__name__=="__main__":
main()
Now this consumer reads data till whatever is available and not new data
i have attached a screenshot of the responses from the 2 py scripts when running
We can see that the {"key": 11} is the last one received, but there is still new value's getting added to the topic.
Expected behavior
Should be able to consume messages continuously from the topic created using mop.
Screenshots
Desktop (please complete the following information):
Describe the bug
When i created a pulsar topic using mop and when trying to consume that topic it consumes only till the last stored data and not the new data's published to the topic.
To Reproduce
Steps to reproduce the behavior:
pulsar.service
file forRaspberry Pi 4
pulsar.service
file forUbuntu
git clone https://github.com/streamnative/mop.git cd mop mvn clean install -DskipTests mkdir /home/ubuntu/apache-pulsar-3.0.7/protocols cp /home/ubuntu/mop/mqtt-impl/target/pulsar-protocol-handler-mqtt-3.4.0-SNAPSHOT.nar /home/ubuntu/apache-pulsar-3.0.7/protocols/ nano /home/ubuntu/apache-pulsar-3.0.7/conf/standalone.conf
sudo ss -tulnp | grep 1883
{"key": 11}
is the last one received, but there is still new value's getting added to the topic.Expected behavior
Should be able to consume messages continuously from the topic created using mop.
Screenshots
Desktop (please complete the following information):
Additional context
Raspberry Pi 4
andUbuntu
to test this problem, but both had same problem.The text was updated successfully, but these errors were encountered: