Method that checks if the status is ok

2

I have this code just below that makes a connection to my server. I would like to perform a check of the status 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 sends the message from my server / client. The code below, prepares to send this message, before sending it drops in 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);

.......

    
asked by anonymous 13.04.2015 / 20:55

1 answer

1

I believe the following code will help you:

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

Source: link . I recommend viewing the link mentioned about problems related to this solution.

    
14.04.2015 / 13:33