summaryrefslogtreecommitdiff
path: root/cpp/bindings/qpid/ruby/lib/qpid_messaging/session.rb
blob: 7e6e11f6549d2a1a28fbcc3af5003e5b069816c0 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#--
# 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.
#++

module Qpid

  module Messaging

    # A +Session+ represents a distinct conversation between end points. They are
    # created from an active (i.e., not closed) Connection.
    #
    # A +Session+ is used to acknowledge individual or all messages that have
    # passed through it
    class Session

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

      def session_impl # :nodoc:
        @session_impl
      end

      # Returns the Connection associated with this session.
      def connection
        @connection
      end

      # Creates a new endpoint for sending messages.
      #
      # The address can either be an instance Address or else an
      # address string.
      #
      # ==== Arguments
      #
      # * +address+ - the end point address.
      def create_sender(address)
        _address = address

        if address.class == Qpid::Messaging::Address
          _address = address.address_impl
        end

        sender_impl = @session_impl.createSender(_address)
        sender_name = sender_impl.getName

        Qpid::Messaging::Sender.new(self, sender_impl)
      end

      # Retrieves the Sender with the specified name.
      #
      # Raises an exception if no such Sender exists.
      #
      # ==== Arguments
      #
      # * +name+ - the name of the Sender
      def sender(name)
        Qpid::Messaging::Sender.new self, @session_impl.getSender(name)
      end

      # Creates a new endpoint for receiving messages.
      #
      # The +address+ can either be an instance Address or else an
      # address string.
      #
      # ==== Arguments
      #
      # * +address+ - the end point address.
      def create_receiver(address)
        result        = nil
        receiver_impl = nil

        if address.class == Qpid::Messaging::Address
          address_impl = address.address_impl
          receiver_impl = @session_impl.createReceiver address_impl
        else
          receiver_impl = @session_impl.createReceiver(address)
        end

        Qpid::Messaging::Receiver.new self, receiver_impl
      end

      # Retrieves the +Receiver+ with the specified name, or nil if no such
      # Receiver exists.
      #
      # ==== Arguments
      #
      # * +name+ - the name of the Receiver
      def receiver(name)
        Qpid::Messaging::Receiver.new self, @session_impl.getReceiver(name)
      end

      # Closes the +Session+ and all associated +Sender+ and +Receiver+ instances.
      #
      # *NOTE:* All +Session+ instances for a Connection are closed when the
      # Connection is closed. But closing a +Session+ does not affect the
      # owning Connection.
      def close; @session_impl.close; end

      # Commits any pending transactions for a transactional session.
      def commit; @session_impl.commit; end

      # Rolls back any uncommitted transactions on a transactional session.
      def rollback; @session_impl.rollback; end

      # Acknowledges one or more outstanding messages that have been received
      # on this session.
      #
      # ==== Arguments
      #
      # * +options+ - the set of options
      #
      # ==== Options
      #
      # * :message - if specified, then only that Message is acknowledged
      # * :sync - if true, the call will block until processed by the broker
      #
      # ==== Examples
      #
      #   # acknowledge all received messages
      #   session.acknowledge
      #
      #   # acknowledge a single message
      #   session.acknowledge :message => message
      #
      #   # acknowledge all messages, wait until the call finishes
      #   session.acknowledge :sync => true
      #
      #--
      # TODO: Add an optional block to be used for blocking calls.
      #++
      def acknowledge(options = {})
        sync = options[:sync] || false
        message = options[:message] if options[:message]

        unless message.nil?
          @session_impl.acknowledge message.message_impl, sync
        else
          @session_impl.acknowledge sync
        end
      end

      # Rejects the specified message. A rejected message will not be
      # redelivered.
      #
      # NOTE: A message cannot be rejected once it has been acknowledged.
      def reject(message); @session_impl.reject message.message_impl; end

      # Releases the message, which allows the broker to attempt to
      # redeliver it.
      #
      # NOTE: A message connot be released once it has been acknowled.
      def release(message); @session_impl.release message.message_impl; end

      # Requests synchronization with the broker.
      #
      # ==== Arguments
      #
      # * +options+ - the list of options
      #
      # ==== Options
      #
      # * +:block+ - if true, the call blocks until the broker acknowledges it
      #
      #--
      # TODO: Add an optional block to be used for blocking calls.
      #++
      def sync(args = {})
        block = args[:block] || false
        @session_impl.sync block
      end

      # Returns the total number of receivable messages, and messages already
      # received, by Receiver instances associated with this +Session+.
      def receivable; @session_impl.getReceivable; end

      # Returns the number of messages that have been acknowledged by this
      # +Session+ whose acknowledgements have not been confirmed as processed
      # by the broker.
      def unsettled_acks; @session_impl.getUnsettledAcks; end

      # Fetches the next Receiver with a message pending. Waits the specified
      # number of milliseconds before timing out.
      #
      # For a Receiver to be returned, it must have a capacity > 0 and have
      # Messages locally queued.
      #
      # If no Receiver is found within the time out period, then a MessageError
      # is raised.
      #
      # ==== Arguments
      #
      # * +timeout+ - the duration
      #
      # ==== Examples
      #
      #   loop do
      #
      #     begin
      #       # wait a maximum of one minute for the next receiver to be ready
      #       recv = session.next_receiver Qpid::Messaging::Duration::MINUTE
      #
      #       # get and dispatch the message
      #       msg = recv.get
      #       dispatch_message msg
      #
      #     rescue
      #       puts "No receivers were returned"
      #     end
      #
      #   end
      def next_receiver(timeout = Qpid::Messaging::Duration::FOREVER, &block)
        receiver_impl = @session_impl.nextReceiver(timeout.duration_impl)

        unless receiver_impl.nil?
          recv = Qpid::Messaging::Receiver.new self, receiver_impl
          block.call recv unless block.nil?
        end

        return recv
      end

      # Returns true if there were exceptions on this session.
      def errors?; @session_impl.hasError; end

      # If the +Session+ has been rendered invalid due to some exception,
      # this method will result in that exception being raised.
      #
      # If none have occurred, then no exceptions are raised.
      #
      # ==== Examples
      #
      #   # show any errors that occurred during the Session
      #   if @session.errors?
      #     begin
      #       @session.errors
      #     rescue Exception => error
      #       puts "An error occurred: #{error}"
      #     end
      #   end
      def errors; @session_impl.checkError; end

    end

  end

end