Class: Hazelcast::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hazelcast-client.rb

Defined Under Namespace

Classes: DefaultMapListener

Constant Summary collapse

GEM_ROOT =
File.expand_path(File.dirname(__FILE__))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, password = nil, host = nil) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
# File 'lib/hazelcast-client.rb', line 14

def initialize(username = nil, password = nil, host = nil)
  @username = username || "dev"
  @password = password || "dev-pass"
  @host     = host || "localhost"
  @conn_id  = self.class.connection_id @username, @password, @host
  self.class.connect @username, @password, @host
  client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/hazelcast-client.rb', line 76

def method_missing(meth, *args, &blk)
  if client.respond_to? meth
    client.send meth, *args, &blk
  else
    super
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/hazelcast-client.rb', line 12

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



12
13
14
# File 'lib/hazelcast-client.rb', line 12

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/hazelcast-client.rb', line 12

def username
  @username
end

Class Method Details

.connect(username, password, *hosts) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hazelcast-client.rb', line 92

def self.connect(username, password, *hosts)
  conn_id = connection_id(username, password, *hosts)
  if connections.key? conn_id
    connections[conn_id]
  else
    puts ">> Connecting to [#{hosts.inspect}] as [#{username}] with [#{password}]..."
    client_config = com.hazelcast.client.ClientConfig.new
    group_config = com.hazelcast.config.GroupConfig.new username, password
    hosts.each {|host| client_config.add_address host }
    client_config.set_group_config group_config
    connections[conn_id] = com.hazelcast.client.HazelcastClient.newHazelcastClient client_config
  end
end

.connection_id(username, password, *hosts) ⇒ Object



88
89
90
# File 'lib/hazelcast-client.rb', line 88

def self.connection_id(username, password, *hosts)
  "#{username}:#{password}:#{hosts.map{ |x| x.to_s }.sort.join('|')}"
end

.connectionsObject



84
85
86
# File 'lib/hazelcast-client.rb', line 84

def self.connections
  @connections ||= {}
end

Instance Method Details

#clientObject



23
24
25
# File 'lib/hazelcast-client.rb', line 23

def client
  self.class.connections[@conn_id]
end

#cluster(name) ⇒ Object



27
28
29
# File 'lib/hazelcast-client.rb', line 27

def cluster(name)
  client.getCluster name.to_s
end

#list(name) ⇒ Object



31
32
33
# File 'lib/hazelcast-client.rb', line 31

def list(name)
  client.getList name.to_s
end

#lock(name) ⇒ Object



35
36
37
# File 'lib/hazelcast-client.rb', line 35

def lock(name)
  client.getLock name.to_s
end

#map(name) ⇒ Object



39
40
41
# File 'lib/hazelcast-client.rb', line 39

def map(name)
  client.getMap name.to_s
end

#multi_map(name) ⇒ Object



43
44
45
# File 'lib/hazelcast-client.rb', line 43

def multi_map(name)
  client.getMultiMap name.to_s
end

#queue(name) ⇒ Object



47
48
49
# File 'lib/hazelcast-client.rb', line 47

def queue(name)
  client.getQueue name.to_s
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/hazelcast-client.rb', line 72

def respond_to?(meth)
  super || client.respond_to?(meth)
end

#set(name) ⇒ Object



51
52
53
# File 'lib/hazelcast-client.rb', line 51

def set(name)
  client.getSet name.to_s
end

#topic(name) ⇒ Object



55
56
57
# File 'lib/hazelcast-client.rb', line 55

def topic(name)
  client.getTopic name.to_s
end

#transactionObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hazelcast-client.rb', line 59

def transaction
  txn = client.getTransaction
  txn.begin
  begin
    yield
    txn.commit
    nil
  rescue => e
    txn.rollback
    e
  end
end