-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from aol/reactiveAsync
Support async NIO requests via reactive-streams
- Loading branch information
Showing
8 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
micro-grizzly-with-jersey/src/test/java/app/publisher/binder/direct/AsyncPublisherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package app.publisher.binder.direct; | ||
|
||
import com.aol.micro.server.MicroserverApp; | ||
import com.aol.micro.server.config.Microserver; | ||
import com.aol.micro.server.module.ConfigurableModule; | ||
import com.aol.micro.server.rest.jersey.AsyncBinder; | ||
import com.aol.micro.server.testing.RestAgent; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@Microserver | ||
public class AsyncPublisherTest { | ||
RestAgent rest = new RestAgent(); | ||
MicroserverApp server; | ||
|
||
@Before | ||
public void startServer() { | ||
|
||
server = new MicroserverApp(() -> "binder"); | ||
server.start(); | ||
|
||
} | ||
|
||
@After | ||
public void stopServer() { | ||
server.stop(); | ||
} | ||
|
||
@Test | ||
public void runAppAndBasicTest() throws InterruptedException, ExecutionException { | ||
assertThat(rest.get("http://localhost:8080/binder/test/myEndPoint"), is("hello world!")); | ||
} | ||
@Test | ||
public void runAppAndBasicTest2() throws InterruptedException, ExecutionException { | ||
assertThat(rest.get("http://localhost:8080/binder/test/async2"), is("hello")); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
micro-grizzly-with-jersey/src/test/java/app/publisher/binder/direct/AsyncResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package app.publisher.binder.direct; | ||
|
||
import com.aol.micro.server.auto.discovery.Rest; | ||
import cyclops.async.Future; | ||
import cyclops.stream.ReactiveSeq; | ||
import cyclops.stream.Spouts; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import java.util.concurrent.Executors; | ||
import java.util.stream.Stream; | ||
|
||
|
||
@Rest | ||
@Path("/test") | ||
public class AsyncResource { | ||
|
||
private void sleep() { | ||
try { | ||
Thread.sleep(10000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path("myEndPoint") | ||
public Future<String> myEndPoint() { | ||
return Future.ofSupplier(() -> { | ||
sleep(); | ||
return "hello world!"; | ||
}, Executors.newFixedThreadPool(1)); | ||
} | ||
|
||
@GET | ||
@Path("async2") | ||
public ReactiveSeq<String> async2() { | ||
return Spouts.publishOn(Stream.of("hello"), Executors.newFixedThreadPool(1)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
micro-jersey/src/main/java/com/aol/micro/server/rest/jersey/AsyncBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.aol.micro.server.rest.jersey; | ||
|
||
import org.glassfish.hk2.utilities.binding.AbstractBinder; | ||
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher; | ||
import org.glassfish.jersey.server.spi.internal.ResourceMethodInvocationHandlerProvider; | ||
|
||
import javax.inject.Singleton; | ||
|
||
|
||
public class AsyncBinder extends AbstractBinder { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(AsyncDispatcher.AsyncDispatcherProvider.class).to( | ||
ResourceMethodDispatcher.Provider.class).in(Singleton.class) | ||
.ranked(1); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
micro-jersey/src/main/java/com/aol/micro/server/rest/jersey/AsyncDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.aol.micro.server.rest.jersey; | ||
|
||
import cyclops.stream.ReactiveSeq; | ||
import cyclops.stream.Spouts; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
import org.glassfish.hk2.api.ServiceHandle; | ||
import org.glassfish.hk2.api.ServiceLocator; | ||
import org.glassfish.jersey.server.ContainerRequest; | ||
import org.glassfish.jersey.server.internal.LocalizationMessages; | ||
import org.glassfish.jersey.server.internal.inject.ConfiguredValidator; | ||
import org.glassfish.jersey.server.internal.process.AsyncContext; | ||
import javax.inject.Provider; | ||
|
||
import org.glassfish.jersey.server.model.Invocable; | ||
import org.glassfish.jersey.server.spi.internal.ResourceMethodDispatcher; | ||
import org.reactivestreams.Publisher; | ||
|
||
|
||
import javax.ws.rs.ProcessingException; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class AsyncDispatcher implements ResourceMethodDispatcher { | ||
|
||
private final ResourceMethodDispatcher originalDispatcher; | ||
|
||
@Context | ||
private javax.inject.Provider<AsyncContext> asyncContext; | ||
@Context | ||
private javax.inject.Provider<ContainerRequestContext> containerRequestContext; | ||
|
||
|
||
public AsyncDispatcher(ResourceMethodDispatcher originalDispatcher) { | ||
this.originalDispatcher = originalDispatcher; | ||
} | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
static class AsyncDispatcherProvider implements Provider{ | ||
@Context | ||
private ServiceLocator serviceLocator; | ||
@Override | ||
public ResourceMethodDispatcher create(Invocable method, InvocationHandler handler, ConfiguredValidator validator) { | ||
final Class<?> returnType = method.getHandlingMethod().getReturnType(); | ||
if(Publisher.class.isAssignableFrom(returnType)){ | ||
Set<Provider> providers = serviceLocator.getAllServiceHandles(ResourceMethodDispatcher.Provider.class) | ||
.stream() | ||
.filter(h->!h.getActiveDescriptor() | ||
.getImplementationClass() | ||
.equals(AsyncDispatcherProvider.class)) | ||
.map(ServiceHandle::getService) | ||
.collect(Collectors.toSet()); | ||
|
||
for (ResourceMethodDispatcher.Provider provider : providers) { | ||
ResourceMethodDispatcher dispatcher = provider.create(method, handler, validator); | ||
if (dispatcher != null) { | ||
AsyncDispatcher result = new AsyncDispatcher(dispatcher); | ||
serviceLocator.inject(result); | ||
return result; | ||
} | ||
} | ||
|
||
} | ||
return null; | ||
} | ||
} | ||
@Override | ||
public Response dispatch(Object resource, ContainerRequest request) throws ProcessingException { | ||
final AsyncContext context = this.asyncContext.get(); | ||
if(!context.suspend()) | ||
throw new ProcessingException(LocalizationMessages.ERROR_SUSPENDING_ASYNC_REQUEST()); | ||
final ContainerRequestContext requestContext = containerRequestContext.get(); | ||
|
||
Publisher pub = (Publisher)originalDispatcher.dispatch(resource, request) | ||
.getEntity(); | ||
Spouts.from(pub).onEmptySwitch(()->Spouts.of(Response.noContent().build())) | ||
.forEach(1,context::resume, context::resume); | ||
|
||
return null; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters