Search in sources :

Example 1 with IChannelListener

use of org.eclipse.flux.client.IChannelListener in project flux by eclipse.

the class RabbitMQFluxClientTest method testChannelListener.

/**
	 * Use asynchronous connectToChannel and disconnectFromChannel together with channel listener
	 * to avoid race condition when sending / receiving messages.
	 */
public void testChannelListener() throws Exception {
    final String[] channels = { SUPER_USER, "Bob", "Alice" };
    final BasicFuture<Void> echoServiceReady = new BasicFuture<>();
    //resolves at end of callback spagetti sequence
    final BasicFuture<Void> theEnd = new BasicFuture<Void>();
    final Process<List<String>> root = new Process<List<String>>(SUPER_USER) {

        protected java.util.List<String> execute() throws Exception {
            final List<String> receivedMessages = new ArrayList<String>();
            echoServiceReady.get();
            conn.addMessageHandler(new MessageHandler("echoResponse") {

                @Override
                public void handle(String type, JSONObject message) {
                    try {
                        receivedMessages.add(message.getString("msg"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            conn.addChannelListener(new IChannelListener() {

                public void disconnected(String oldChannel) {
                    String newChannel = nextChannel(oldChannel);
                    if (newChannel != null) {
                        conn.connectToChannel(newChannel);
                    } else {
                        theEnd.resolve(null);
                    }
                }

                @Override
                public void connected(final String currentChannel) {
                    try {
                        send("echoRequest", new JSONObject().put(USERNAME, currentChannel).put("msg", "Hello on channel " + currentChannel));
                        //Give some time for response
                        setTimeout(500, new TimerTask() {

                            public void run() {
                                try {
                                    conn.disconnectFromChannel(currentChannel);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                private String nextChannel(String oldChannel) {
                    for (int i = 0; i < channels.length - 1; i++) {
                        if (channels[i].equals(oldChannel)) {
                            return channels[i + 1];
                        }
                    }
                    return null;
                }
            });
            conn.disconnectFromChannel(channels[0]);
            // must wait until callback spagetti finishes before allowing this process to terminate.
            theEnd.get();
            return receivedMessages;
        }
    };
    Process<Void> echoService = new Process<Void>(SUPER_USER) {

        protected Void execute() throws Exception {
            conn.addMessageHandler(new RequestResponseHandler(conn, "echoRequest") {
            });
            echoServiceReady.resolve(null);
            //once main process finished we can finish too
            await(root);
            return null;
        }

        ;
    };
    run(echoService, root);
    ArrayList<String> expected = new ArrayList<String>();
    for (int i = 1; i < channels.length; i++) {
        expected.add("Hello on channel " + channels[i]);
    }
    assertArrayEquals(expected.toArray(), root.result.get().toArray());
}
Also used : IChannelListener(org.eclipse.flux.client.IChannelListener) MessageHandler(org.eclipse.flux.client.MessageHandler) RequestResponseHandler(org.eclipse.flux.client.RequestResponseHandler) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) JSONObject(org.json.JSONObject) TimerTask(java.util.TimerTask) BasicFuture(org.eclipse.flux.client.util.BasicFuture) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimerTask (java.util.TimerTask)1 TimeoutException (java.util.concurrent.TimeoutException)1 IChannelListener (org.eclipse.flux.client.IChannelListener)1 MessageHandler (org.eclipse.flux.client.MessageHandler)1 RequestResponseHandler (org.eclipse.flux.client.RequestResponseHandler)1 BasicFuture (org.eclipse.flux.client.util.BasicFuture)1 JSONObject (org.json.JSONObject)1