Skip to content

Reactive Microserver

johnmcclean-aol edited this page Nov 14, 2015 · 4 revisions

Microserver integrates with simple-react

since v0.60 offers more integration points

  1. Offers fully non-blocking / event driven execution via Reactive interface / mixin
  2. Communicate across threads using simple-react adapters (Queues / Topics / Signals)

The Reactive Mixin

The Reactive mixin offers options such as

  1. ioStreamBuilder() : returns a performance optimised builder for executing IO Bound Streams
  2. cpuStreamBuilder() : returns a performance optimised builder for executing CPU Bound Streams
  3. The ability to switch a Stream between being optimised for IO & CPU bound operations
  4. enqueue facility to push data onto an asyncrhonous Queue to be picked up by a connected Stream

Non-blocking example

From the built-in manifest resouce

   @GET
   @Produces("application/json")
   public void mainfest(@Suspended AsyncResponse asyncResponse, @Context ServletContext context) {
   	
   	this.ioStreamBuilder().of("/META-INF/MANIFEST.MF")
   				.map(url->context.getResourceAsStream(url))
   				.map(this::getManifest)
   				.peek(result->asyncResponse.resume(result))
   				.run();
   	
   }

Communicating via Queues

simple-react Stream listening for & reacting to events

     public static void main(String[] args){
		LazyFutureStream<String> stream = Pipes.register("test", QueueFactories.
											<String>boundedNonBlockingQueue(100)
												.build());
		stream.filter(it->it!=null).peek(System.out::println).run();
		new MicroserverApp(()-> "simple-app").run();
	}

Rest Resources can pass events onwards

       @GET
   @Produces("text/plain")
   @Path("/ping")
   public String ping() {
   	this.enqueue("test","ping : " + next++);
   	return "ok";
   }

Every Rest call to ping will result in our Lazy infinite stream printing out the ping counter to the console.