Method that checks if the status is ok

Asked

Viewed 299 times

2

I have this code right below that makes a connection to my server.
I would like to perform a status check of my server through a method that connects to that server, how can I implement this?

public class ConnectionRabbitmqFactoryConsumer {

    private static final Logger LOGGER = 
        SaveLog.lauchLog(ReadConfigDatabase.class.getName());

    public static ConnectionFactory getConnection() {

        try {

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.120.150");
            factory.setUsername("adm");
            factory.setPassword("adm");
            factory.setPort(5672);

            return factory;

        } catch (Exception e) {

            e.printStackTrace(System.err);
            LOGGER.error("error conection rabbitmq ");
        }

        return null;

    }

}

This is an application that performs sending message from my server / client. The code below prepares the sending of this message, before sending it falls into this getConnection(), to see if everything is ok with my server.

    @Override
public void run() {

    try {

        //Execultar tarefas assincronas
        ExecutorService threader = Executors.newFixedThreadPool(20);
        Connection connection = ConnectionRabbitmqFactoryConsumer.getConnection().newConnection(threader);

.......

  • You want a method to check if the server is online then?

  • That, exactly.

1 answer

1

I believe the code below helps you:

public static boolean hostAvailabilityCheck() { 
    try (Socket s = new Socket("192.168.120.150", 5672)) {
        return true;
    } catch (IOException ex) {
        /* ignore */
    }
    return false;
}

Source: https://stackoverflow.com/a/17149882/2387977 . I recommend viewing the link mentioned about problems related to this solution.

  • And in case I have user and password? how does it get? Grateful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.