Newer
Older
jooby-sse / src / main / java / route / SseRoute.java
Mark George on 4 May 2021 866 bytes Created project
package route;

import io.jooby.Jooby;
import java.util.concurrent.TimeUnit;

/**
 * A simple SSE server using Jooby 2.
 *
 * Supports multiple concurrent clients and handles client disconnects.
 *
 * Created to test if Apache Camel can consume SSE (it can't - boo) as a way
 * of bridging webhooks payloads into a Camel router.
 *
 * @author Mark George
 */
public class SseRoute extends Jooby {

	{
		sse("/api/sales/{usercode}", (sse) -> {

			String usercode = sse.getContext().path("usercode").value();

			// loop so that we can stream new messages
			while (true) {

				// check if client is still connected
				if (sse.isOpen()) {

					// sent a message
					sse.send("Hi " + usercode);
					System.out.println("sent");

					TimeUnit.SECONDS.sleep(1);

				} else {
					System.out.println("Client closed connection");
					break;
				}
			}

		});
	}

}