Top Level Namespace

Defined Under Namespace

Modules: Seahorse, SqsPoller, Sqspoller

Constant Summary collapse

HEADERS =

Returns true if the queue was created; otherwise, false.

Examples:

exit 1 unless queue_created?(
  Aws::SQS::Client.new(region: 'us-west-2'),
  'my-queue'
)

Returns:

  • (Boolean)

    true if the queue was created; otherwise, false.

{
  'Content-Type' => 'application/json',
  'Accept' => 'application/json'
}

Instance Method Summary collapse

Instance Method Details

#messages_sent?(sqs_client, queue_url, entries) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/sqs-ruby-example-create-queue.rb', line 35

def messages_sent?(sqs_client, queue_url, entries)
  sqs_client.send_message_batch(
    queue_url: queue_url,
    entries: entries
  )
  true
rescue StandardError => e
  puts "Error sending messages: #{e.message}"
  false
end

#process_http_response(response) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/sqs-ruby-example-create-queue.rb', line 98

def process_http_response(response)
  case response.code
  when 200
    return "OK"
  else
    raise "Service did not return 200 OK response. #{response.code}"
  end
end

#queue_created?(sqs_client, queue_name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/sqs-ruby-example-create-queue.rb', line 27

def queue_created?(sqs_client, queue_name)
  sqs_client.create_queue(queue_name: queue_name)
  true
rescue StandardError => e
  puts "Error creating queue: #{e.message}"
  false
end

#run_meObject

Full example call: Replace us-west-2 with the AWS Region you’re using for Amazon SQS.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sqs-ruby-example-create-queue.rb', line 55

def run_me
  region = 'us-west-2'
  queue_name = 'my-queue'
  sqs_client = Aws::SQS::Client.new(region: region)

  puts "Creating the queue named '#{queue_name}'..."

  if queue_created?(sqs_client, queue_name)
    puts 'Queue created.'
  else
    puts 'Queue not created.'
  end
  queue_url = sqs_client.get_queue_url(queue_name: queue_name).queue_url
  messages = Array.new
  connection_pool = Concurrent::RubyThreadPoolExecutor.new(max_threads: 20, min_threads: 1, max_queue: 100000)
  10000.times do |index|
    puts "hello - #{index} - #{index % 10}"
    body = {
      'MessageType' => 'PROCESS_TRUCK_LOCATION',
      'Source' => "#{index} SQS updates",
      'Content' => {
        'TimeZone' => 'UTC'
      }
    }
    msg = {
      id: SecureRandom.uuid,
      message_body: body.to_json
    }
=begin
    RestClient::Request.execute(:method => :post, :url => "http://localhost:5001/process", :payload => body.to_json, :headers => HEADERS,  :timeout => 10, :open_timeout => 5) do |response, request, result|
      process_http_response response
    end
=end
    messages.push(msg)
    if index % 10 == 9
      submit_task(connection_pool, messages.clone, queue_url, sqs_client)
      messages = Array.new
    end
  end
  connection_pool.shutdown
  connection_pool.wait_for_termination
end

#submit_task(connection_pool, messages, queue_url, sqs_client) ⇒ Object



46
47
48
49
50
51
# File 'lib/sqs-ruby-example-create-queue.rb', line 46

def submit_task(connection_pool, messages, queue_url, sqs_client)
  connection_pool.post do
    messages_sent?(sqs_client, queue_url, messages)
    puts "Messages Sent"
  end
end