summaryrefslogtreecommitdiff
path: root/cpp/bindings/qpid/ruby/lib/qpid/receiver.rb
blob: 0ce16309ed612deb0b5fdc5808b4877fc09c0440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

require 'cqpid'

require 'qpid/duration'

module Qpid

  module Messaging

    # Receiver is the entity through which messages are received.
    #
    # An instance of Receiver can only be created using an active (not
    # previously closed) Session.
    #
    # ==== Example
    #
    #   conn     = Qpid::Messaging::Connection.new :url => "mybroker:5762"
    #   conn.open
    #   session  = conn.create_session
    #   receiver = session.create_receiver "my-sender-queue"
    class Receiver

      def initialize(session, receiver_impl) # :nodoc:
        @session       = session
        @receiver_impl = receiver_impl
      end

      def receiver_impl # :nodoc:
        @receiver_impl
      end

      # Retrieves a message from the local queue, or waits for up to
      # the duration specified for one to become available.
      #
      # If a block is given, then it will be invaked after the next message
      # is received or the call times out, passing in the message or nil
      # respectively.
      #
      # ==== Options
      # * duration - the timeout to wait (def. Duration::FOREVER)
      #
      # ==== Examples
      #
      #   msg = rcvr.get # Uses the default timeout of forever
      #
      #   msg = rcvr.get Qpid::Messaging::Duration::IMMEDIATE # returns a message or exits immediately
      #
      #   # passes in a block to handle the received message
      #   rcvr.get Qpid::Messaging::Duration::SECOND do |message|
      #     if message.nil?
      #       puts "No message was received."
      #     else
      #       puts "Received this message: #{message.content}"
      #     end
      #   end
      def get(duration = Qpid::Messaging::Duration::FOREVER)
        message_impl = @receiver_impl.get duration.duration_impl
        create_message_wrapper message_impl unless message_impl.nil?
      end

      # Retrieves a message from the receiver's subscription, or waits
      # for up to the duration specified for one to become available.
      #
      # If a block is given, then it will be invaked after the next message
      # is received or the call times out, passing in the message or nil
      # respectively.
      #
      # ==== Options
      # * duration - the timeout to wait (def. Duration::FOREVER)
      #
      # ==== Examples
      #
      #   msg = rcvr.fetch # Uses the default timeout of forever
      #
      #   msg = rcvr.fetch Qpid::Messaging::Duration::IMMEDIATE # returns a message or exits immediately
      #
      #   # passes in a block to handle the received message
      #   rcvr.fetch Qpid::Messaging::Duration::SECOND do |message|
      #     if message.nil?
      #       puts "No message was received."
      #     else
      #       puts "Received this message: #{message.content}"
      #     end
      #   end
      def fetch(duration = Qpid::Messaging::Duration::FOREVER)
        message_impl = @receiver_impl.fetch duration.duration_impl
        create_message_wrapper message_impl unless message_impl.nil?
      end

      # Sets the capacity for this +Receiver+.
      #
      # ==== Options
      #
      # * capacity - the capacity
      #
      # ==== Examples
      #
      #   receiver.capacity = 50 # sets the incoming capacity to 50 messages
      #
      def capacity=(capacity); @receiver_impl.setCapacity capacity; end

      # Returns the capacity.
      #
      #
      # The capacity is the numnber of incoming messages that can be held
      # locally before being fetched.
      #
      # ==== Examples
      #
      #   puts "The receiver can hold #{rcv.capacity} messages."
      #
      def capacity; @receiver_impl.getCapacity; end

      # Returns the number of slots for receiving messages.
      #
      # This differs from +capacity+ in that it is the available slots in
      # the capacity for holding incoming messages, where available <= capacity.
      #
      # ==== Examples
      #
      #   puts "You can receive #{rcv.available} messages before blocking."
      #
      def available; @receiver_impl.getAvailable; end

      # Returns the number of messages that have been received and acknowledged
      # but whose acknowledgements have not been confirmed by the sender.
      #
      # ==== Examples
      #
      #   puts "You have #{rcv.unsettled} messages to be confirmed."
      #
      def unsettled; @receiver_impl.getUnsettled; end

      # Closes this +Receiver+.
      #
      # This does not affect the +Session+.
      def close; @receiver_impl.close; end

      # Returns whether the receiver is closed.
      #
      # ==== Examples
      #
      #   recv.close unless recv.closed?
      #
      def closed?; @receiver_impl.isClosed; end

      # Returns the name of this +Receiver+.
      #
      # ==== Examples
      #
      #   puts "Receiver: #{recv.name}"
      def name; @receiver_impl.getName; end

      # Returns the Session for this +Receiver+.
      def session; @session; end

      private

      def create_message_wrapper message_impl # :nodoc:
        Qpid::Messaging::Message.new(:impl => message_impl)
      end

    end

  end

end