Class: AbstractThriftClient
- Inherits:
-
Object
- Object
- AbstractThriftClient
- Defined in:
- lib/thrift_client/abstract_thrift_client.rb
Direct Known Subclasses
Constant Summary collapse
- DISCONNECT_ERRORS =
[ IOError, Thrift::ProtocolException, Thrift::TransportException ]
- APPLICATION_ERRORS =
[ Thrift::ApplicationException ]
- DEFAULTS =
DEFAULT_BEFORE_METHOD = Proc.new {|method_name| puts “prepare call #{method_name} method.”} DEFAULT_AFTER_METHOD = Proc.new {|method_name| puts “called #{method_name} method.”} DEFAULT_ON_EXCEPTION = Proc.new {|method_name, e| puts “call #{method_name} method ouccur exception.”; raise e}
{ :protocol => Thrift::CompactProtocol, :transport => Thrift::Socket, :transport_wrapper => Thrift::FramedTransport, :disconnect_exception_classes => DISCONNECT_ERRORS, :application_exception_classes => APPLICATION_ERRORS, :size => 1, :timeout => nil, :client_class => nil, :test_on_borrow => true, :pool_timeout => 0, :servers => "127.0.0.1:9090", :multiplexed => false, :before_method => nil, :after_method => nil, :on_exception => nil }
Instance Method Summary collapse
- #add_callback(callback_type, &block) ⇒ Object
- #destroy ⇒ Object
- #handle_method(method_name, *args) ⇒ Object
-
#initialize(options = {}) ⇒ AbstractThriftClient
constructor
A new instance of AbstractThriftClient.
Constructor Details
#initialize(options = {}) ⇒ AbstractThriftClient
Returns a new instance of AbstractThriftClient.
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 |
# File 'lib/thrift_client/abstract_thrift_client.rb', line 42 def initialize( = {}) = () servers = parse_servers(["servers"]) #initialize options @options = DEFAULTS.merge() if(@options[:size] > 1) @options[:server_class] = MultiClientServer else @options[:server_class] = SingleClientServer end #initialize servers info @server_list = servers.collect do |s| @options[:server_class].new(s, @options) end.sort_by { rand } @callbacks = {} # @callbacks[:before_method] = [] # @callbacks[:after_method] = [] # @callbacks[:on_exception] = [] # @callbacks[:before_method].push(@options[:before_method]) # @callbacks[:after_method].push(@options[:after_method]) # @callbacks[:on_exception].push(DEFAULT_ON_EXCEPTION) #initialize client methods @client_methods = [] @options[:client_class].instance_methods.each do |method_name| if method_name != 'send_message' && method_name =~ /^send_(.*)$/ instance_eval("def #{$1}(*args); handle_method(:'#{$1}', *args); end", __FILE__, __LINE__) @client_methods << $1 end end end |
Instance Method Details
#add_callback(callback_type, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/thrift_client/abstract_thrift_client.rb', line 79 def add_callback(callback_type, &block) case callback_type when :before_method, :after_method, :on_exception @callbacks[callback_type] ||= [] @callbacks[callback_type].push(block) else raise ArgumentError.new("Unknown callback type #{callback_type},only support before_method|after_method|on_exception") end return self end |
#destroy ⇒ Object
122 123 124 |
# File 'lib/thrift_client/abstract_thrift_client.rb', line 122 def destroy @server_list.each {|server| server.destroy if server} end |
#handle_method(method_name, *args) ⇒ Object
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 |
# File 'lib/thrift_client/abstract_thrift_client.rb', line 90 def handle_method(method_name, *args) begin server = next_server client = server.get_client no_available_server unless client do_callbacks(:before_method, method_name) result = client.send(method_name, *args) do_callbacks(:after_method, method_name) return result rescue *@options[:application_exception_classes] => e #TODO whether or not wrapper these exceptions if(@callbacks[:on_exception] && @callbacks[:on_exception].length > 0) do_callbacks(:on_exception, method_name, e) else raise e end rescue *@options[:disconnect_exception_classes] => e begin server.disconnect(client) if server # tag the server is inactive rescue Exception => e end #TODO whether or not wrapper these exceptions if(@callbacks[:on_exception] && @callbacks[:on_exception].length > 0) do_callbacks(:on_exception, method_name, e) else raise e end ensure server.return_client(client) if server end end |