mercredi 30 août 2017

Is there any better way to track subscriber changes?

Lets say we have a Client class with method to send messages to server queue. It is also subscribed to queue and server will send a notification back to all clients when it receives a new message from any of registered clients:

    public class Client {
        public void sendMessage(String message) {
            // Method to send messages to server
        }

        public void messageDelivered(String receivedMessage) {
            // This is a method subscribed successfull message delivering
        }
    }

In another class I would like to instansiate my Client, send a message and check that it was sent successfuly:

    public class ParentClass {
        protected void checkConnection() {
            Client client = new Client(); // Lets skip all configuration and connection details

            String testMessage = "Test message";
            client.sendMessage(testMessage);

            // Check that messageDelivered() was called in client with testMessage parameter
        }
    }

What is the best way to check that messageDelivered() was called asynchroniously inside parent checkConnection() method?

I see the simple solution to create a flag in Client class, update it when message is delivered and wrap checking of this flag in some loop limited by time in parent class. But this option looks very dirty for me. Maybe there are some better practicies or "watchers" in Java SDK?

Aucun commentaire:

Enregistrer un commentaire