jmx4r 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,14 +6,14 @@ require "rubygems"
6
6
 
7
7
  dir = File.dirname(__FILE__)
8
8
  lib = File.join(dir, "lib", "jmx4r.rb")
9
- version = "0.0.2"
9
+ version = "0.0.3"
10
10
 
11
11
  task :default => [:test]
12
12
 
13
13
  Rake::TestTask.new do |test|
14
14
  test.libs << "test"
15
15
  test.test_files = ["test/ts_all.rb"]
16
- test.verbose = false
16
+ test.verbose = false
17
17
  end
18
18
 
19
19
  Rake::RDocTask.new do |rdoc|
@@ -25,6 +25,12 @@ Rake::RDocTask.new do |rdoc|
25
25
  rdoc.options << "-S"
26
26
  end
27
27
 
28
+ desc "Publish current documentation to Rubyforge"
29
+ task :publish_docs => [:rdoc] do
30
+ sh "scp -r doc/html/* " +
31
+ "jmesnil@rubyforge.org:/var/www/gforge-projects/jmx4r/doc/"
32
+ end
33
+
28
34
  spec = Gem::Specification.new do |spec|
29
35
  spec.name = "jmx4r"
30
36
  spec.version = version
@@ -1,15 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
-
2
+ require 'rubygems'
3
3
  require 'jmx4r'
4
4
 
5
5
  class_loading = JMX::MBean.find_by_name "java.lang:type=ClassLoading"
6
6
 
7
- # implicite way...
7
+ # implicit way...
8
8
  class_loading.attributes.keys.each { |attr| puts "#{attr}: #{class_loading.send attr}"}
9
9
 
10
10
  puts "--"
11
11
 
12
- # ... or explicite
12
+ # ... or explicit
13
13
  puts "loaded class count: #{class_loading.loaded_class_count}"
14
14
  puts "total loaded class count: #{class_loading.total_loaded_class_count}"
15
15
  puts "unloaded class count: #{class_loading.unloaded_class_count}"
@@ -1,11 +1,10 @@
1
1
  #!/usr/bin/env jruby
2
+ require 'rubygems'
2
3
  require 'jmx4r'
3
4
 
4
- #JMX::MBean.establish_connection :host => "localhost", :port => 3000
5
-
6
5
  logging = JMX::MBean.find_by_name "java.util.logging:type=Logging"
7
6
  logging.logger_names.each do |logger_name|
8
- logging.set_logger_level logger_name, "INFO"
7
+ logging.set_logger_level logger_name, "INFO"
9
8
  end
10
9
 
11
10
  memory = JMX::MBean.find_by_name "java.lang:type=Memory"
@@ -1,26 +1,26 @@
1
1
  #!/usr/bin/env jruby
2
-
2
+ require 'rubygems'
3
3
  require 'jmx4r'
4
4
 
5
5
  logging = JMX::MBean.find_by_name "java.util.logging:type=Logging"
6
6
  puts "--"
7
7
  logging.logger_names.sort.each do |logger|
8
- level = logging.get_logger_level logger
9
- puts "#{logger} #{level}"
8
+ level = logging.get_logger_level logger
9
+ puts "#{logger} #{level}"
10
10
  end
11
11
  puts "--"
12
12
 
13
13
  if ARGV.length == 1
14
- level = ARGV[0]
15
- puts "set all loggers to #{level}"
16
- logging.logger_names.each do |logger|
17
- logging.set_logger_level logger, level
18
- end
14
+ level = ARGV[0]
15
+ puts "set all loggers to #{level}"
16
+ logging.logger_names.each do |logger|
17
+ logging.set_logger_level logger, level
18
+ end
19
19
  end
20
20
 
21
21
  if ARGV.length == 2
22
- logger = ARGV[0]
23
- level = ARGV[1]
24
- puts "set #{logger} to #{level}"
25
- logging.set_logger_level logger, level
22
+ logger = ARGV[0]
23
+ level = ARGV[1]
24
+ puts "set #{logger} to #{level}"
25
+ logging.set_logger_level logger, level
26
26
  end
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env jruby
2
-
2
+ require 'rubygems'
3
3
  require 'jmx4r'
4
4
 
5
5
  def display header, memory_usage
6
- puts header
7
- memory_usage.composite_type.key_set.each do |type|
8
- puts "\t#{type} : #{memory_usage.get type}"
9
- end
6
+ puts header
7
+ memory_usage.composite_type.key_set.each do |type|
8
+ puts "\t#{type} : #{memory_usage.get type}"
9
+ end
10
10
  end
11
11
  memory = JMX::MBean.find_by_name "java.lang:type=Memory"
12
12
 
@@ -14,8 +14,8 @@ display "Heap Memory Usage", memory.heap_memory_usage
14
14
  display "Non Heap Memory Usage", memory.heap_memory_usage
15
15
 
16
16
  if ARGV.length == 1 and ARGV[0] == "gc"
17
- puts "trigger a garbage collection"
18
- memory.gc
19
- display "Heap Memory Usage", memory.heap_memory_usage
20
- display "Non Heap Memory Usage", memory.heap_memory_usage
17
+ puts "trigger a garbage collection"
18
+ memory.gc
19
+ display "Heap Memory Usage", memory.heap_memory_usage
20
+ display "Non Heap Memory Usage", memory.heap_memory_usage
21
21
  end
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env jruby
2
+ require 'rubygems'
3
+ require 'jmx4r'
4
+ require 'jconsole'
5
+
6
+ # This example will display memory usage for 3 Java applications running locally
7
+ # and manageable through diffent JMX ports.
8
+ # It will display memory usage, trigger a garbage collection on the 3 Java
9
+ # applications and display again the memory usage.
10
+
11
+ ports = [3000, 3001, 3002]
12
+
13
+ # We use jconsole as our target Java applications
14
+ # and specify on which port we can connect to their
15
+ # MBean server
16
+ ports.each { |port| JConsole::start :port => port }
17
+
18
+ # horizontal rule used for display
19
+ HR = "+----------------+----------------+----------------+"
20
+
21
+ def display_memory_usages (ports)
22
+ puts HR
23
+ puts "| Node | Heap Used | Non Heap Used |"
24
+ puts HR
25
+
26
+ ports.each do |port|
27
+ memory = JMX::MBean.find_by_name "java.lang:type=Memory", :port => port
28
+ heap_used = memory.heap_memory_usage.get("used")
29
+ non_heap_used = memory.non_heap_memory_usage.get("used")
30
+ puts "| localhost:#{port} |#{heap_used.to_s.rjust(15)} |#{non_heap_used.to_s.rjust(15)} |"
31
+ end
32
+
33
+ puts HR
34
+ end
35
+
36
+ puts "Before GC:"
37
+ display_memory_usages ports
38
+ ports.each do |port|
39
+ memory = JMX::MBean.find_by_name "java.lang:type=Memory", :port => port
40
+ memory.gc
41
+ end
42
+ puts "After GC:"
43
+ display_memory_usages ports
44
+
@@ -1,39 +1,62 @@
1
-
1
+ # JConsole module is used by jmx4r unit tests.
2
+ #
3
+ # {jconsole}[http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html]
4
+ # is used as the target remote Java application manageable by JMX
5
+ # (we do not used it for its JMX capability, just because it is a ready to
6
+ # use Java application available with every JDK).
2
7
  module JConsole
3
- def JConsole.start(args={})
4
- port = args[:port] || 3000
5
- pwd_file = args[:pwd_file]
6
- access_file = args[:access_file]
8
+
9
+ # Start a new instance of jconsole which is accessible on port 3000.
10
+ # By default, no authentication is required to connect to it.
11
+ #
12
+ # The args hash accepts 3 keys:
13
+ # [:port] the port which will be listens to JMX connections
14
+ # [:pwd_file] the path to the file containing the authentication credentials
15
+ # [:access_file] the path to the file containing the authorization credentials
16
+ #
17
+ # The file path corresponding to :pwd_file must have <b>600 permission</b>
18
+ # (<tt>chmod 600 jmxremote.password</tt>).
19
+ #
20
+ # Both <tt>:pwd_file</tt> and <tt>:access_file+</tt> must be specified to run a secure
21
+ # jconsole (see {JMX password & access files}[http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html#PasswordAccessFiles])
22
+ def JConsole.start(args={})
23
+ port = args[:port] || 3000
24
+ pwd_file = args[:pwd_file]
25
+ access_file = args[:access_file]
7
26
 
8
- cmd =<<-EOCMD.split("\n").join(" ")
9
- jconsole
10
- -J-Dcom.sun.management.jmxremote
11
- -J-Dcom.sun.management.jmxremote.port=#{port}
12
- -J-Dcom.sun.management.jmxremote.ssl=false
13
- -J-Dcom.sun.management.jmxremote.authenticate=#{!pwd_file.nil?}
14
- EOCMD
15
- if pwd_file
16
- cmd << " -J-Dcom.sun.management.jmxremote.password.file=#{pwd_file}"
17
- cmd << " -J-Dcom.sun.management.jmxremote.access.file=#{access_file}"
18
- end
19
- Thread.start { system cmd }
20
- sleep 2
21
- end
27
+ cmd =<<-EOCMD.split("\n").join(" ")
28
+ jconsole
29
+ -J-Dcom.sun.management.jmxremote
30
+ -J-Dcom.sun.management.jmxremote.port=#{port}
31
+ -J-Dcom.sun.management.jmxremote.ssl=false
32
+ -J-Dcom.sun.management.jmxremote.authenticate=#{!pwd_file.nil?}
33
+ EOCMD
34
+ if pwd_file and access_file
35
+ cmd << " -J-Dcom.sun.management.jmxremote.password.file=#{pwd_file}"
36
+ cmd << " -J-Dcom.sun.management.jmxremote.access.file=#{access_file}"
37
+ end
38
+ Thread.start { system cmd }
39
+ sleep 4
40
+ end
22
41
 
23
- def JConsole.stop()
24
- jconsole_pid = `ps a -o pid,command | grep -w jconsole | grep -v grep | grep -v ruby | cut -c -5`
25
- `kill #{jconsole_pid}` if jconsole_pid != ""
26
- sleep 2
27
- end
42
+ # Stop an instance of JConsole (by killing its process)
43
+ #
44
+ # By default, it will kill the process corresponding to an instance JConsole with
45
+ # a port on 3000. Another port can be specified in parameter.
46
+ def JConsole.stop(port=3000)
47
+ jconsole_pid = `ps a -w -o pid,command | grep -w jconsole | grep port=#{port} | grep -v grep | grep -v ruby | cut -c -5`
48
+ `kill #{jconsole_pid}` if jconsole_pid != ""
49
+ sleep 3
50
+ end
28
51
  end
29
52
 
30
53
  if ARGV.length == 1
31
- case ARGV[0]
32
- when "start"
33
- JConsole::start
34
- puts "started jconsole"
35
- when "stop"
36
- JConsole::stop
37
- puts "stopped jconsole"
38
- end
54
+ case ARGV[0]
55
+ when "start"
56
+ JConsole::start
57
+ puts "started jconsole"
58
+ when "stop"
59
+ JConsole::stop
60
+ puts "stopped jconsole"
61
+ end
39
62
  end
@@ -1,100 +1,147 @@
1
+ # Copyright 2007 Jeff Mesnil (http://jmesnil.net)
1
2
  require 'java'
2
3
 
3
4
  class String
4
- # Transform a CamelCase String to a snake_case String.
5
- #--
6
- # Code has been taken from ActiveRecord
7
- def snake_case
8
- self.to_s.gsub(/::/, '/').
9
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
10
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
11
- tr("-", "_").
12
- downcase
13
- end
5
+ # Transform a CamelCase String to a snake_case String.
6
+ #--
7
+ # Code has been taken from ActiveRecord
8
+ def snake_case
9
+ self.to_s.gsub(/::/, '/').
10
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
11
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
12
+ tr("-", "_").
13
+ downcase
14
+ end
14
15
  end
15
16
 
16
17
  module JMX
17
- class MBean
18
- include_package 'javax.management'
19
- include_package 'javax.management.remote'
20
- include_class 'java.util.HashMap'
18
+ class MBean
19
+ include_package 'javax.management'
20
+ include_package 'javax.management.remote'
21
+ include_class 'java.util.HashMap'
21
22
 
22
- attr_reader :object_name, :operations, :attributes
23
+ attr_reader :object_name, :operations, :attributes
23
24
 
24
- def initialize(object_name)
25
- @mbsc = MBean.establish_connection
26
- @object_name = object_name
27
- info = @mbsc.getMBeanInfo @object_name
28
- @attributes = Hash.new
29
- info.attributes.each do | mbean_attr |
30
- @attributes[mbean_attr.name.snake_case] = mbean_attr.name
31
- self.class.instance_eval do
32
- define_method mbean_attr.name.snake_case do
33
- @mbsc.getAttribute @object_name, "#{mbean_attr.name}"
34
- end
35
- end
36
- if mbean_attr.isWritable
37
- self.class.instance_eval do
38
- define_method "#{mbean_attr.name.snake_case}=" do |value|
39
- attr = Attribute.new mbean_attr.name, value
40
- @mbsc.setAttribute @object_name, attr
41
- end
42
- end
43
- end
44
- end
45
- @operations = Hash.new
46
- info.operations.each do |mbean_op|
47
- param_types = mbean_op.signature.map {|param| param.type}
48
- @operations[mbean_op.name.snake_case] = [mbean_op.name, param_types]
49
- end
25
+ # Creates a new MBean.
26
+ #
27
+ # object_name:: a string corresponding to a valid ObjectName
28
+ # mbsc:: a connection to a MBean server. If none is passed,
29
+ # use the global connection created by
30
+ # MBean.establish_connection
31
+ def initialize(object_name, mbsc=nil)
32
+ @mbsc = mbsc || @@mbsc
33
+ @object_name = object_name
34
+ info = @mbsc.getMBeanInfo @object_name
35
+ @attributes = Hash.new
36
+ info.attributes.each do | mbean_attr |
37
+ @attributes[mbean_attr.name.snake_case] = mbean_attr.name
38
+ self.class.instance_eval do
39
+ define_method mbean_attr.name.snake_case do
40
+ @mbsc.getAttribute @object_name, "#{mbean_attr.name}"
41
+ end
50
42
  end
51
-
52
- def method_missing(method, *args, &block)
53
- if @operations.keys.include?(method.to_s)
54
- op_name, param_types = @operations[method.to_s]
55
- @mbsc.invoke @object_name,
56
- op_name,
57
- args.to_java(:Object),
58
- param_types.to_java(:String)
59
- else
60
- super
43
+ if mbean_attr.isWritable
44
+ self.class.instance_eval do
45
+ define_method "#{mbean_attr.name.snake_case}=" do |value|
46
+ attr = Attribute.new mbean_attr.name, value
47
+ @mbsc.setAttribute @object_name, attr
61
48
  end
49
+ end
62
50
  end
51
+ end
52
+ @operations = Hash.new
53
+ info.operations.each do |mbean_op|
54
+ param_types = mbean_op.signature.map {|param| param.type}
55
+ @operations[mbean_op.name.snake_case] = [mbean_op.name, param_types]
56
+ end
57
+ end
63
58
 
64
- def self.establish_connection(args={})
65
- host = args[:host] || "localhost"
66
- port = args[:port] || 3000
67
- username = args[:username]
68
- password = args[:password]
59
+ def method_missing(method, *args, &block) #:nodoc:
60
+ if @operations.keys.include?(method.to_s)
61
+ op_name, param_types = @operations[method.to_s]
62
+ @mbsc.invoke @object_name,
63
+ op_name,
64
+ args.to_java(:Object),
65
+ param_types.to_java(:String)
66
+ else
67
+ super
68
+ end
69
+ end
69
70
 
70
- @@mbsc ||= begin
71
- url = "service:jmx:rmi:///jndi/rmi://#{host}:#{port}/jmxrmi"
72
- env = HashMap.new
73
- if !username.nil? and username.length > 0
74
- credentials = [username, password]
75
- env.put JMXConnector::CREDENTIALS, credentials.to_java(:String)
76
- end
77
- connector = JMXConnectorFactory::connect JMXServiceURL.new(url), env
78
- connector.getMBeanServerConnection
79
- end
80
- end
71
+ @@mbsc = nil
81
72
 
82
- def self.remove_connection
83
- @@mbsc = nil
84
- end
73
+ # establish a connection to a remote MBean server which will
74
+ # be used by all subsequent MBeans.
75
+ #
76
+ # See MBean.create_connection for a list of the keys that are
77
+ # accepted in arguments.
78
+ #
79
+ # Examples
80
+ #
81
+ # JMX::MBean.establish_connection :port => "node23", :port => 1090
82
+ # JMX::MBean.establish_connection :port => "node23", :username => "jeff", :password => "secret"
83
+ def self.establish_connection(args={})
84
+ @@mbsc ||= create_connection args
85
+ end
85
86
 
86
- def self.mbsc
87
- establish_connection
88
- end
87
+ def self.remove_connection(args={})
88
+ @@mbsc = nil
89
+ end
89
90
 
90
- def self.find_all_by_name(name)
91
- object_name = ObjectName.new(name)
92
- object_names = MBean.mbsc.queryNames(object_name, nil)
93
- object_names.map { |on| MBean.new(on) }
94
- end
91
+ def self.connection(args={})
92
+ if args.has_key? :host or args.has_key? :port
93
+ return create_connection(args)
94
+ else
95
+ MBean.establish_connection(args) unless @@mbsc
96
+ return @@mbsc
97
+ end
98
+ end
95
99
 
96
- def self.find_by_name(name)
97
- MBean.new ObjectName.new(name)
98
- end
100
+ # Create a connection to a remote MBean server.
101
+ #
102
+ # The args accepts 4 keys:
103
+ # [:host] the host of the MBean server (defaults to "localhost")
104
+ # [:port] the port of the MBean server (defaults to 3000)
105
+ # [:username] the name of the user (if the MBean server requires authentication).
106
+ # No default
107
+ # [:password] the password of the user (if the MBean server requires authentication).
108
+ # No default
109
+ def self.create_connection(args={})
110
+ host= args[:host] || "localhost"
111
+ port = args[:port] || 3000
112
+ username = args[:username]
113
+ password = args[:password]
114
+
115
+ url = "service:jmx:rmi:///jndi/rmi://#{host}:#{port}/jmxrmi"
116
+ env = HashMap.new
117
+ if !username.nil? and username.length > 0
118
+ credentials = [username, password]
119
+ env.put JMXConnector::CREDENTIALS, credentials.to_java(:String)
120
+ end
121
+ connector = JMXConnectorFactory::connect JMXServiceURL.new(url), env
122
+ connector.getMBeanServerConnection
123
+ end
124
+
125
+ # Returns an array of MBeans corresponding to all the MBeans
126
+ # registered for the ObjectName passed in parameter (which may be
127
+ # a pattern).
128
+ #
129
+ # The args accepts the same keys than #create_connection and an
130
+ # additional one:
131
+ # [:connection] a MBean server connection (as returned by #create_connection)
132
+ def self.find_all_by_name(name, args={})
133
+ object_name = ObjectName.new(name)
134
+ mbsc = args[:connection] || MBean.connection(args)
135
+ object_names = mbsc.queryNames(object_name, nil)
136
+ object_names.map { |on| MBean.new(on, mbsc) }
137
+ end
138
+
139
+ # Same as #find_all_by_name but the ObjectName passed in parameter
140
+ # can not be a pattern.
141
+ # Only one single MBean is returned.
142
+ def self.find_by_name(name, args={})
143
+ mbsc = args[:connection] || MBean.connection(args)
144
+ MBean.new ObjectName.new(name), mbsc
99
145
  end
146
+ end
100
147
  end
@@ -4,31 +4,31 @@ require "jmx4r"
4
4
  require "jconsole"
5
5
 
6
6
  class TestAttribute < Test::Unit::TestCase
7
- def setup
8
- JConsole::start
9
- @memory = JMX::MBean.find_by_name "java.lang:type=Memory"
10
- end
7
+ def setup
8
+ JConsole::start
9
+ @memory = JMX::MBean.find_by_name "java.lang:type=Memory"
10
+ end
11
11
 
12
- def teardown
13
- JMX::MBean.remove_connection
14
- JConsole::stop
15
- end
12
+ def teardown
13
+ JMX::MBean.remove_connection
14
+ JConsole::stop
15
+ end
16
16
 
17
- def test_unknwown_attribute
18
- assert_raise(NoMethodError) { @memory.unknown_attribute }
19
- end
17
+ def test_unknwown_attribute
18
+ assert_raise(NoMethodError) { @memory.unknown_attribute }
19
+ end
20
20
 
21
- def test_readable_attribute
22
- assert_equal false, @memory.verbose
23
- end
21
+ def test_readable_attribute
22
+ assert_equal false, @memory.verbose
23
+ end
24
24
 
25
- def test_writable_attribute
26
- assert_equal false, @memory.verbose
27
- @memory.verbose = true
28
- assert_equal true, @memory.verbose
29
- end
25
+ def test_writable_attribute
26
+ assert_equal false, @memory.verbose
27
+ @memory.verbose = true
28
+ assert_equal true, @memory.verbose
29
+ end
30
30
 
31
- def test_non_writable_attribute
32
- assert_raise(NoMethodError) { @memory.object_pending_finalization_count = -1 }
33
- end
31
+ def test_non_writable_attribute
32
+ assert_raise(NoMethodError) { @memory.object_pending_finalization_count = -1 }
33
+ end
34
34
  end
@@ -13,12 +13,12 @@ class TestAuthentication < Test::Unit::TestCase
13
13
  @pwd_file = File.new(pwd_path, "w")
14
14
  @pwd_file.puts "#{@username} #{@password}"
15
15
  @pwd_file.close
16
-
16
+
17
17
  # pwd file must be readable only by user
18
18
  # but somehow File.chmod is not working
19
19
  # with JRuby
20
20
  `chmod 0600 #{@pwd_file.path}`
21
-
21
+
22
22
  access_path = "/tmp/jmx4r.access"
23
23
  @access_file = File.new(access_path, "w")
24
24
  @access_file.puts "#{@username} readwrite"
@@ -35,14 +35,20 @@ class TestAuthentication < Test::Unit::TestCase
35
35
  File.delete @pwd_file.path if File.file? @pwd_file.path
36
36
  File.delete @access_file.path if File.file? @access_file.path
37
37
  end
38
-
38
+
39
39
  def test_establish_auth_connection_with_correct_credentials
40
40
  JMX::MBean.establish_connection :username => @username, :password => @password
41
41
  end
42
42
 
43
- def test_establish_auth_connection_with_incorrect_credentials
43
+ def test_establish_auth_connection_with_invalid_username
44
+ assert_raise(NativeException) {
45
+ JMX::MBean.establish_connection :username => "invalid user name", :password => @password
46
+ }
47
+ end
48
+
49
+ def test_establish_auth_connection_with_invalid_password
44
50
  assert_raise(NativeException) {
45
- JMX::MBean.establish_connection :username => "not a valid user name", :password => @password
51
+ JMX::MBean.establish_connection :username => @username, :password => "invalid password"
46
52
  }
47
53
  end
48
54
 
@@ -4,30 +4,37 @@ require "jmx4r"
4
4
  require "jconsole"
5
5
 
6
6
  class TestConnection < Test::Unit::TestCase
7
- def teardown
8
- JMX::MBean.remove_connection
9
- JConsole::stop
10
- end
7
+ def teardown
8
+ JMX::MBean.remove_connection
9
+ end
11
10
 
12
- def test_establish_connection_with_bad_port
13
- assert_raise(NativeException) {
14
- JMX::MBean.establish_connection :port => 9999
15
- }
16
- end
11
+ def test_establish_connection_with_bad_port
12
+ assert_raise(NativeException) {
13
+ JMX::MBean.establish_connection :port => 9999
14
+ }
15
+ end
17
16
 
18
- def test_establish_connection_with_bad_host
19
- assert_raise(NativeException) {
20
- JMX::MBean.establish_connection :host => "not a valid host"
21
- }
22
- end
17
+ def test_establish_connection_with_bad_host
18
+ assert_raise(NativeException) {
19
+ JMX::MBean.establish_connection :host => "not a valid host"
20
+ }
21
+ end
23
22
 
24
- def test_establish_connection
25
- JConsole::start
26
- JMX::MBean.establish_connection
23
+ def test_establish_connection
24
+ begin
25
+ JConsole::start
26
+ JMX::MBean.establish_connection
27
+ ensure
28
+ JConsole::stop
27
29
  end
30
+ end
28
31
 
29
- def test_establish_connection_with_custom_port
30
- JConsole::start :port => 3001
31
- JMX::MBean.establish_connection :port => 3001
32
+ def test_establish_connection_with_custom_port
33
+ begin
34
+ JConsole::start :port => 3001
35
+ JMX::MBean.establish_connection :port => 3001
36
+ ensure
37
+ JConsole::stop 3001
32
38
  end
39
+ end
33
40
  end
@@ -0,0 +1,64 @@
1
+ require "test/unit"
2
+
3
+ require "jmx4r"
4
+ require "jconsole"
5
+
6
+ class TestMultipleConnections < Test::Unit::TestCase
7
+ def setup
8
+ @ports = [3001, 3002]
9
+ @ports.each {|port| JConsole::start :port => port }
10
+
11
+ # the MBeanServerDelegate ID is unique for each MBean Server
12
+ @delegate_on = "JMImplementation:type=MBeanServerDelegate"
13
+ end
14
+
15
+ def teardown
16
+ @ports.each do |port|
17
+ JMX::MBean.remove_connection :port => port
18
+ JConsole::stop port
19
+ end
20
+ end
21
+
22
+ def test_same_connection_port
23
+ delegate_1 = JMX::MBean.find_by_name @delegate_on, :port => @ports[0]
24
+ delegate_2 = JMX::MBean.find_by_name @delegate_on, :port => @ports[0]
25
+
26
+ assert_equal delegate_1.m_bean_server_id, delegate_2.m_bean_server_id
27
+ end
28
+
29
+ def test_different_connection_port
30
+ delegate_1 = JMX::MBean.find_by_name @delegate_on, :port => @ports[0]
31
+ delegate_2 = JMX::MBean.find_by_name @delegate_on, :port => @ports[1]
32
+
33
+ assert_not_equal delegate_1.m_bean_server_id, delegate_2.m_bean_server_id
34
+ end
35
+
36
+ def test_same_connection
37
+ mbsc = JMX::MBean.establish_connection :port => @ports[0]
38
+
39
+ delegate_1 = JMX::MBean.find_by_name @delegate_on, :connection => mbsc
40
+ delegate_2 = JMX::MBean.find_by_name @delegate_on, :connection => mbsc
41
+
42
+ assert_equal delegate_1.m_bean_server_id, delegate_2.m_bean_server_id
43
+ end
44
+
45
+ def test_different_connection
46
+ mbsc_1 = JMX::MBean.create_connection :port => @ports[0]
47
+ mbsc_2 = JMX::MBean.create_connection :port => @ports[1]
48
+
49
+ delegate_1 = JMX::MBean.find_by_name @delegate_on, :connection => mbsc_1
50
+ delegate_2 = JMX::MBean.find_by_name @delegate_on, :connection => mbsc_2
51
+
52
+ assert_not_equal delegate_1.m_bean_server_id, delegate_2.m_bean_server_id
53
+ end
54
+
55
+ def test_global_connection
56
+ JMX::MBean.establish_connection :port => @ports[0]
57
+
58
+ delegate_1 = JMX::MBean.find_by_name @delegate_on
59
+ delegate_2 = JMX::MBean.find_by_name @delegate_on
60
+
61
+ assert_equal delegate_1.m_bean_server_id, delegate_2.m_bean_server_id
62
+ end
63
+
64
+ end
@@ -2,4 +2,5 @@ require "test/unit"
2
2
 
3
3
  require "tc_connection"
4
4
  require "tc_auth"
5
+ require "tc_multiple_connections"
5
6
  require "tc_attributes"
metadata CHANGED
@@ -3,23 +3,22 @@ homepage: http://code.google.com/p/jmx4r/
3
3
  extensions: []
4
4
  executables: []
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
6
+ version: 0.0.3
7
7
  post_install_message:
8
- date: 2007-06-26 22:00:00 +00:00
8
+ date: 2007-07-02 22:00:00 +00:00
9
9
  files:
10
10
  - examples/class_loading.rb
11
11
  - examples/jvm_mngmt.rb
12
12
  - examples/logging.rb
13
13
  - examples/memory.rb
14
+ - examples/memory_on_many_nodes.rb
14
15
  - lib/jconsole.rb
15
16
  - lib/jmx4r.rb
16
- - test/tc_connection.rb
17
17
  - test/tc_attributes.rb
18
- - test/remote_server.rb
19
- - test/ts_all.rb
20
18
  - test/tc_auth.rb
21
- - test/mbean_base.rb
22
- - test/toto.rb
19
+ - test/tc_connection.rb
20
+ - test/tc_multiple_connections.rb
21
+ - test/ts_all.rb
23
22
  - Rakefile
24
23
  - README.txt
25
24
  - LICENSE.txt
@@ -1,44 +0,0 @@
1
- require 'java'
2
-
3
- class MBeanBase < java.lang.Object
4
- include_package 'javax.management'
5
- include DynamicMBean
6
-
7
- attr_reader :attributes
8
-
9
- def initialize
10
- @attributes = []
11
- end
12
-
13
- def getMBeanInfo
14
- begin
15
- mbean_attrs = @attributes.map do |attr|
16
- MBeanAttributeInfo.new attr.to_s, attr.to_s, "java.lang.String", true, false, false
17
- end
18
- MBeanInfo.new(self.class.to_s, self.class.to_s,
19
- mbean_attrs.to_java('javax.management.MBeanAttributeInfo'),
20
- #[].to_java('javax.management.MBeanAttributeInfo'),
21
- [].to_java('javax.management.MBeanConstructorInfo'),
22
- [].to_java('javax.management.MBeanOperationInfo'),
23
- [].to_java('javax.management.MBeanNotificationInfo'))
24
- rescue
25
- puts $!
26
- end
27
- end
28
- end
29
-
30
- class TestMBean < MBeanBase
31
- #mbean_attr_reader :hello
32
-
33
- def initialize
34
- super
35
- @hello = "world"
36
- end
37
- end
38
-
39
- require 'remote_server'
40
- server = RemoteServer.new
41
- server.start
42
- mbean = TestMBean.new
43
- mbean.attributes << :hello
44
- server.connector_server.getMBeanServer.register_mbean mbean, ObjectName.new("jmx4r:type=Test")
@@ -1,33 +0,0 @@
1
- require 'java'
2
- include_class 'javax.management.ObjectName'
3
-
4
- class RemoteServer
5
- include_package 'java.rmi.registry'
6
- include_package 'java.lang.management'
7
- include_package 'javax.management.remote'
8
- include_package 'javax.management'
9
-
10
- attr_reader :connector_server
11
-
12
- def start(port=3000)
13
-
14
- LocateRegistry.createRegistry(port);
15
-
16
- mbs = ManagementFactory.platform_mbean_server
17
- url = JMXServiceURL.new "service:jmx:rmi:///jndi/rmi://:#{port}/jmxrmi"
18
- @connector_server = JMXConnectorServerFactory.newJMXConnectorServer url, nil , mbs
19
- @connector_server.start
20
- end
21
-
22
- def stop
23
- @connector_server.stop
24
- end
25
- end
26
-
27
- include_class 'javax.management.StandardMBean'
28
-
29
- module Test
30
- def hello
31
- puts "hello"
32
- end
33
- end
@@ -1,15 +0,0 @@
1
- require 'java'
2
-
3
- class A < java.lang.Object
4
- def A.dsl_method(*args)
5
- puts args.join(', ')
6
- end
7
- end
8
-
9
- class B < A
10
- dsl_method :a, :b
11
- end
12
-
13
- b = B.new
14
-
15
-