Skip to content

Conversation

@archfish
Copy link
Owner

@archfish archfish commented Oct 1, 2025

fixed #7
fixed #9

Breaking Changes in Producer Message Implementation

Overview

This update introduces significant changes to how messages are serialized and handled in the Pulsar SDK, particularly for producer messages. These changes affect how content types are managed and how consumers process incoming messages.

Key Changes

  1. Automatic Content-Type Management
    Previous behavior: Developers had to manually set Content-Type headers for messages.
    New behavior: The PulsarSdk::Producer::Message class now automatically sets Content-Type headers based on the message type:
    String messages: text/plain; charset=utf-8
    Hash/Array messages: application/json; charset=utf-8
    Other objects: Automatically converted to JSON if possible, otherwise to string
  2. Content-Type Constants
    New constants have been added to PulsarSdk::Producer::Message:
CONTENT_TYPE_JSON = 'application/json; charset=utf-8'.freeze
CONTENT_TYPE_TEXT = 'text/plain; charset=utf-8'.freeze
  1. Enhanced Message Serialization
    The serialize_message! method now automatically handles serialization based on message type:
    String messages are preserved as-is with text Content-Type
    Hash and Array messages are automatically converted to JSON with JSON Content-Type
    Other objects are converted to JSON if they respond to to_json, otherwise to string

  2. New Helper Method: json_encoded?
    Both producer and consumer message classes now include a json_encoded? method to check if a message needs JSON deserialization:

# Check if message requires JSON deserialization
if msg.json_encoded?
  parsed_data = JSON.parse(msg.payload)
end
  1. Consumer Message Processing
    Consumer messages are now automatically processed based on their Content-Type:

JSON messages are automatically identified via json_encoded? method
Applications can use the same method to determine how to process messages

Migration Guide

Before (Old Code):

# Manual Content-Type management
msg = PulsarSdk::Producer::Message.new("Hello World")
# Had to manually set Content-Type if needed

# Message processing in consumer
# Had to guess or manually check content type

After (New Code):

# Automatic Content-Type management
msg = PulsarSdk::Producer::Message.new({ name: "John", age: 30 })
# Content-Type automatically set to application/json

# Message processing in consumer
consumer.listen do |cmd, msg|
  if msg.json_encoded?
    # Process as JSON
    data = JSON.parse(msg.payload)
  else
    # Process as text
    text = msg.payload
  end
end

Impact Assessment

High Impact

Applications that manually set Content-Type headers may experience conflicts
Consumers that relied on manual Content-Type checking need to update to use json_encoded? method

Medium Impact

Message serialization behavior has changed, which may affect message format expectations
Error handling for JSON parsing may need adjustment

Low Impact

Basic message sending functionality remains the same
API for creating messages is unchanged, only the internal behavior is enhanced

Recommendations

Review existing code that manually sets Content-Type headers
Update consumer message processing to use the new json_encoded? helper method
Test message serialization with different data types to ensure compatibility
Update any documentation or examples that show manual Content-Type management
These changes improve the developer experience by automating content type management while providing better tools for message processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants