«

»

Aug 25

Using jnp as a JBoss heartbeat

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.

  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
Enhanced by Zemanta

Leave a Reply