org.springframework.web.context.request.async.AsyncWebRequest Java Examples

The following examples show how to use org.springframework.web.context.request.async.AsyncWebRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void timeoutValueAndCallback() throws Exception {

	AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
	WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);

	ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
	emitter.onTimeout(mock(Runnable.class));
	emitter.onCompletion(mock(Runnable.class));

	MethodParameter returnType = returnType(TestController.class, "handle");
	this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);

	verify(asyncWebRequest).setTimeout(19000L);
	verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
	verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
	verify(asyncWebRequest).startAsync();
}
 
Example #2
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyEmitterWithTimeoutValue() throws Exception {

	AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
	WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);

	ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
	emitter.onTimeout(mock(Runnable.class));
	emitter.onCompletion(mock(Runnable.class));

	MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
	this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);

	verify(asyncWebRequest).setTimeout(19000L);
	verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
	verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
	verify(asyncWebRequest).startAsync();
}
 
Example #3
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void responseBodyEmitterWithErrorValue() throws Exception {

	AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
	WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);

	ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
	emitter.onError(mock(Consumer.class));
	emitter.onCompletion(mock(Runnable.class));

	MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
	this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);

	verify(asyncWebRequest).addErrorHandler(any(Consumer.class));
	verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
	verify(asyncWebRequest).startAsync();
}
 
Example #4
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	List<HttpMessageConverter<?>> converters = Arrays.asList(
			new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());

	this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest();
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #5
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void responseBodyEmitterWithErrorValue() throws Exception {

	AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
	WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);

	ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
	emitter.onError(mock(Consumer.class));
	emitter.onCompletion(mock(Runnable.class));

	MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
	this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);

	verify(asyncWebRequest).addErrorHandler(any(Consumer.class));
	verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
	verify(asyncWebRequest).startAsync();
}
 
Example #6
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseBodyEmitterWithTimeoutValue() throws Exception {

	AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
	WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);

	ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
	emitter.onTimeout(mock(Runnable.class));
	emitter.onCompletion(mock(Runnable.class));

	MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
	this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);

	verify(asyncWebRequest).setTimeout(19000L);
	verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
	verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
	verify(asyncWebRequest).startAsync();
}
 
Example #7
Source File: StreamingResponseBodyReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {

	this.handler = new StreamingResponseBodyReturnValueHandler();
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest("GET", "/path");
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #8
Source File: StreamingResponseBodyReturnValueHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.handler = new StreamingResponseBodyReturnValueHandler();
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest("GET", "/path");
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #9
Source File: DeferredResultReturnValueHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.handler = new DeferredResultMethodReturnValueHandler();
	this.request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #10
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {

	List<HttpMessageConverter<?>> converters = Arrays.asList(
			new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());

	this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
	this.request = new MockHttpServletRequest();
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #11
Source File: StreamingResponseBodyReturnValueHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.handler = new StreamingResponseBodyReturnValueHandler();
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest("GET", "/path");
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #12
Source File: DeferredResultReturnValueHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.handler = new DeferredResultMethodReturnValueHandler();
	this.request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #13
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {

	List<HttpMessageConverter<?>> converters = Arrays.asList(
			new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());

	this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
	this.request = new MockHttpServletRequest();
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #14
Source File: OpenEntityManagerInViewTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}
 
Example #15
Source File: OpenEntityManagerInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}
 
Example #16
Source File: OpenSessionInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenSessionInViewInterceptorAsyncScenario() throws Exception {
	// Initial request thread

	final SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);

	OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
	interceptor.setSessionFactory(sf);

	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(sf));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue("Session not bound to async thread", TransactionSynchronizationManager.hasResource(sf));

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(sf));

	verify(session, never()).close();

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));

	verify(session).setFlushMode(FlushMode.MANUAL);
	verify(session).close();
}
 
Example #17
Source File: OpenSessionInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenSessionInViewFilterAsyncScenario() throws Exception {
	final SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);

	// Initial request during which concurrent handling starts..

	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");

	final AtomicInteger count = new AtomicInteger(0);

	final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
	filter.init(filterConfig);

	final FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
			assertTrue(TransactionSynchronizationManager.hasResource(sf));
			count.incrementAndGet();
		}
	};

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	filter.doFilter(this.request, this.response, filterChain);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	assertEquals(1, count.get());
	verify(session, never()).close();

	// Async dispatch after concurrent handling produces result ...

	this.request.setAsyncStarted(false);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	filter.doFilter(this.request, this.response, filterChain);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	assertEquals(2, count.get());

	verify(session).setFlushMode(FlushMode.MANUAL);
	verify(session).close();

	wac.close();
}
 
Example #18
Source File: OpenEntityManagerInViewTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}