Giter Club home page Giter Club logo

spring-async-production-issue-replay's Introduction

Spring Async Thread Issue

Recently, we got major issue in production related to missing Httpheader and HttpRequest parameter after introducing Spring Async. Creating this demo application to make that developer understand how to use Spring Async and why in Async method you would not get HTTP request.

With the below snippet you will find out that DomainUtils.getBannerId() or request.getHeader("banner-id") will return null.

	@Async
	@Override
	public void dummyApiAdaptor() throws Exception {
		System.out.println("I am inside DummyAdaptor1Impl::: dummyApiAdaptor ");
		System.out.println("Thread ID :::::::::" + Thread.currentThread().getId());
		Thread.sleep(50000);
		System.out.println("BannerId ::" + DomainUtils.getBannerId());
	}

Reason is that when The request is created by the servlet container and the request passes through a dispatcher servlet. In the Dispatcher servlet, it identifies that the controller by request mapping and calls the desired method of the controller. When the request been served by the controller and then the controller or service handover the task to Async thread executor and complete the request. Then servlet container either deletes the request object. That is purely based on implementation of servlet container. Because of this, In Async method we would not receive any value from httpservlet request.

Correct way would be to pass the information as a value before invoking Async Object

// Get the value first , then invoke Async method
dummyAdaptor.dummyApiAdaptor(DomainUtils.getBannerId());

@Async
	@Override
	public void dummyApiAdaptor(String bannerId) throws Exception {
		System.out.println("I am inside DummyAdaptor1Impl::: dummyApiAdaptor with bannerId ::: " + bannerId);
		System.out.println("Thread ID :::::::::" + Thread.currentThread().getId());
		Thread.sleep(50000);
		// Here HttpRequest instance will be destroyed by servlet container and below method will return null
		// So while using Async, pass information as parameter don't rely on Http component.
		System.out.println("Httprequest BannerId ::" + DomainUtils.getBannerId());

}  

Without understanding the consequences, One should not add Async annotation at multiple places with the intention to parallelly execute the threads.

Hope, The developer who have added that snipper will understands the concept and will be a great developer one day!

spring-async-production-issue-replay's People

Contributors

ritreshgirdhar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.