jnp is a JBoss protocol which exposes jndi. It is, by default, bound to port 1099. I’d been using that port as a heartbeat, but “cheating” — I would open a socket and then close it immediately. However, this caused problems. jnp is chatty. And it got upset at my not letting it say ‘hi’ before I dropped the connection. So, here’s a code snippet (jruby, the Java should be an exercise for the student) which allows you to actually do an intelligent check.
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 |
def check_port(server) @logger.debug "check_port: #{server.fqdn}(#{server.host}:#{server.port})" begin env = java.util.Properties.new(); env.set_property("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); env.set_property("java.naming.provider.url", "#{server.host}:#{server.port}"); ctx = javax.naming.InitialContext.new(env); # if the server is not running, we'll get an error here because # it will timeout. ctx.list("") ctx.close @logger.debug "check_port: #{server.fqdn}(#{server.host}:#{server.port}): Succeeded" rescue javax.naming.CommunicationException => comm_error @logger.debug "check_port: #{server.fqdn}(#{server.host}:#{server.port}): Not Running" begin ctx.close rescue Exception => ignore_me end return false rescue Exception => e2 @logger.info "check_port: #{server.fqdn}(#{server.host}:#{server.port}): FAILED: #{e2.to_s}" return false end return true end |