mirror of
https://github.com/jlengrand/quarkus.git
synced 2026-03-10 08:41:22 +00:00
Wire up request scope
This commit is contained in:
@@ -123,6 +123,7 @@ public class ArcAnnotationProcessor implements ResourceProcessor {
|
||||
ArcContainer container = template.getContainer();
|
||||
template.initBeanContainer(container);
|
||||
template.setupInjection(container);
|
||||
template.setupRequestScope(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
<groupId>org.jboss.shamrock</groupId>
|
||||
<artifactId>shamrock-core-runtime</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.undertow</groupId>
|
||||
<artifactId>undertow-servlet</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -11,8 +11,11 @@ import org.jboss.shamrock.runtime.InjectionFactory;
|
||||
import org.jboss.shamrock.runtime.InjectionInstance;
|
||||
import org.jboss.shamrock.runtime.RuntimeInjector;
|
||||
|
||||
import io.undertow.server.HttpServerExchange;
|
||||
import io.undertow.servlet.api.DeploymentInfo;
|
||||
import io.undertow.servlet.api.ThreadSetupHandler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Martin Kouba
|
||||
*/
|
||||
public class ArcDeploymentTemplate {
|
||||
@@ -43,6 +46,28 @@ public class ArcDeploymentTemplate {
|
||||
};
|
||||
}
|
||||
|
||||
public void setupRequestScope(@ContextObject("deploymentInfo") DeploymentInfo deploymentInfo, @ContextObject("arc.container") ArcContainer arcContainer) {
|
||||
if(deploymentInfo == null) {
|
||||
return;
|
||||
}
|
||||
deploymentInfo.addThreadSetupAction(new ThreadSetupHandler() {
|
||||
@Override
|
||||
public <T, C> Action<T, C> create(Action<T, C> action) {
|
||||
return new Action<T, C>() {
|
||||
@Override
|
||||
public T call(HttpServerExchange exchange, C context) throws Exception {
|
||||
arcContainer.requestContext().activate();
|
||||
try {
|
||||
return action.call(exchange, context);
|
||||
} finally {
|
||||
arcContainer.requestContext().deactivate();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setupInjection(ArcContainer container) {
|
||||
RuntimeInjector.setFactory(new InjectionFactory() {
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.jboss.shamrock.example.arc;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
|
||||
@RequestScoped
|
||||
public class RequestScopedBean {
|
||||
|
||||
int count;
|
||||
|
||||
public int incrementAndGet() {
|
||||
return ++count;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.jboss.shamrock.example.arc;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
@Path("/request-scoped")
|
||||
public class TestRequestScopeEndpoint {
|
||||
|
||||
@Inject
|
||||
private RequestScopedBean requestScopedBean;
|
||||
|
||||
@GET
|
||||
public String manualValidation() {
|
||||
|
||||
requestScopedBean.incrementAndGet();
|
||||
requestScopedBean.incrementAndGet();
|
||||
return "" + requestScopedBean.incrementAndGet();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.jboss.shamrock.example.test;
|
||||
|
||||
import org.jboss.shamrock.junit.GraalTest;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(GraalTest.class)
|
||||
public class RequestScopeITCase extends RequestScopeTestCase {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.jboss.shamrock.example.test;
|
||||
|
||||
import org.jboss.shamrock.example.testutils.URLTester;
|
||||
import org.jboss.shamrock.junit.ShamrockTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(ShamrockTest.class)
|
||||
public class RequestScopeTestCase {
|
||||
|
||||
@Test
|
||||
public void testRequestScope() {
|
||||
Assert.assertEquals("3", URLTester.relative("rest/request-scoped").invokeURL().asString());
|
||||
Assert.assertEquals("3", URLTester.relative("rest/request-scoped").invokeURL().asString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user