Quick Start¶
This guide will get you up and running with BunnyFarm in just a few minutes. We'll create a simple message class, publish a message, and process it.
Prerequisites¶
Before starting, make sure you have:
- BunnyFarm installed
- RabbitMQ server running locally or accessible remotely
- Basic familiarity with Ruby
Step 1: Create Your First Message Class¶
Create a file called greeting_message.rb
:
require 'bunny_farm'
class GreetingMessage < BunnyFarm::Message
# Define the fields this message expects
fields :name, :language
# Define the actions this message can perform
actions :send_greeting
def send_greeting
puts "Hello #{@items[:name]}! (in #{@items[:language]})"
# Mark the message as successfully processed
success!
# Return true to ACK the message
successful?
end
end
Step 2: Configure BunnyFarm¶
Create a simple configuration (or use environment variables):
Step 3: Publish a Message¶
Create a file called publisher.rb
:
require_relative 'greeting_message'
# Configure BunnyFarm
BunnyFarm.config do
app_id 'greeting_publisher'
end
# Create a new message
message = GreetingMessage.new
# Set the message data
message[:name] = 'Alice'
message[:language] = 'English'
# Publish the message with the 'send_greeting' action
message.publish('send_greeting')
if message.successful?
puts "Message published successfully!"
else
puts "Failed to publish message: #{message.errors.join(', ')}"
end
Step 4: Create a Consumer¶
Create a file called consumer.rb
:
require_relative 'greeting_message'
# Configure BunnyFarm
BunnyFarm.config do
app_id 'greeting_consumer'
end
puts "Starting message consumer..."
puts "Press Ctrl+C to stop"
# Start processing messages (this will block)
BunnyFarm.manage
Step 5: Run the Example¶
-
Start the consumer (in one terminal):
-
Publish a message (in another terminal):
You should see output like:
Consumer terminal:
Publisher terminal:
Understanding What Happened¶
- Message Class:
GreetingMessage
defines the structure and behavior - Fields DSL:
fields :name, :language
specifies expected data - Actions DSL:
actions :send_greeting
defines available operations - Routing Key: The message was routed using
GreetingMessage.send_greeting
- JSON Serialization: Data was automatically serialized/deserialized
- AMQP Flow: Message traveled through RabbitMQ from publisher to consumer
Next Steps¶
Now that you have a basic understanding, explore:
- Basic Concepts - Understand BunnyFarm's architecture
- Message Structure - Learn about the Fields and Actions DSL
- Configuration Options - Advanced configuration
- Examples - More comprehensive examples
Common Issues¶
Connection Refused¶
If you see connection errors, ensure RabbitMQ is running:
# Check RabbitMQ status
sudo systemctl status rabbitmq-server
# Or for Docker
docker ps | grep rabbitmq
Permission Denied¶
Make sure your AMQP credentials are correct in your environment variables or configuration.
Messages Not Processing¶
Verify that your consumer is listening to the correct queue and routing key configuration.