Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide out of the box implementation of the thread scope [SPR-2581] #7270

Closed
spring-projects-issues opened this issue Sep 13, 2006 · 5 comments
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Eugene Kuleshov opened SPR-2581 and commented

Thread-bound scope can help to address concurrency issues (e.g. batching up requests per thread). So, it would be really handy if Spring provided such scope out of the box for Spring 2.0. E.g. something like this:

public class ThreadScope implements Scope {

  private final ThreadLocal threadScope = new ThreadLocal() {
      protected Object initialValue() {
        return new HashMap();
      }
    };
  
  public Object get(String name, ObjectFactory objectFactory) {
    Map scope = (Map) threadScope.get();
    Object object = scope.get(name);
    if(object==null) {
      object = objectFactory.getObject();
      scope.put(name, object);
    }
    return object;
  }

  public Object remove(String name) {
    Map scope = (Map) threadScope.get();
    return scope.remove(name);
  }

  public void registerDestructionCallback(String name, Runnable callback) {
  }
  
}

Affects: 2.0 final

Referenced from: commits ad2cc34

16 votes, 22 watchers

@spring-projects-issues
Copy link
Collaborator Author

Claus Ibsen commented

Eugene should you not also link to your excellent blog about a real life use-case for this scope?

http://jroller.com/page/eu?entry=implementing_efficinet_id_generator

@spring-projects-issues
Copy link
Collaborator Author

Claus Nielsen commented

I'd also really like to see this in Spring, and thought I'd mention another usecase.
We use HttpInvokerProxyFactoryBean for remoting, like this:

[code]
<bean id="selfServiceFacade"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="${selfservice.url}" />
<property name="serviceInterface"
value="com.bmd.bs400.facade.SelfServiceFacade" />
<property name="httpInvokerRequestExecutor" ref="httpClient" />
</bean>

<bean id="httpClient"
	class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
	<property name="httpClient">
		<bean class="org.apache.commons.httpclient.HttpClient">
			<property name="connectionTimeout" value="15000" />
		</bean>
	</property>
</bean>

[/code]

This worked very well until we put it in a multithreaded application. Then we started getting protocol exceptions from httpClient.
We used Eugene's thread-scope to make httpClient thread-scoped to resolve this. The application works again and the bean configuration now looks like this:

[code]
<bean id="selfServiceFacade"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="${selfservice.url}" />
<property name="serviceInterface"
value="com.bmd.bs400.facade.SelfServiceFacade" />
<property name="httpInvokerRequestExecutor" ref="httpClient" />
</bean>

<bean
	class="org.springframework.beans.factory.config.CustomScopeConfigurer">
	<property name="scopes">
		<map>
			<entry key="thread">
				<bean class="com.bmd.demo.ThreadScope" />
			</entry>
		</map>
	</property>
</bean>

<bean id="httpClient"
	class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"
	scope="thread">
	<property name="httpClient">
		<bean class="org.apache.commons.httpclient.HttpClient">
			<property name="connectionTimeout" value="15000" />
			<aop:scoped-proxy />
		</bean>
	</property>
</bean>

[/code]

@spring-projects-issues
Copy link
Collaborator Author

David Winterfeldt commented

I made an implementation that can support destruction callbacks as long as the Thread or Runnable make a call when they finish. There is currently a Runnable that can be a wrapper for another Runnable that will always make the call when it finishes so destruction callbacks occur. More information can be found on this wiki and it's also available for checkout from subversion. It's also available for download as a Maven artifact if anyone wants to use it before Spring includes an implementation of this in a release.

http://www.springbyexample.org/twiki/bin/view/Example/CustomThreadScopeModule

http://svn.springbyexample.org/custom-thread-scope/trunk

<dependency>
<groupId>org.springbyexample</groupId>
<artifactId>custom-thread-scope</artifactId>
<version>1.0</version>
</dependency>
<repositories>
<repository>
<id>springbyexample.org</id>
<name>Spring by Example</name>
<url>http://www.springbyexample.org/maven/repo\
</repository>
</repositories>

@spring-projects-issues
Copy link
Collaborator Author

Jorg Heymans commented

@David Winterfeldt: ThreadScopeContextHolder.currentThreadScopeAttributes().clear() has protected access in your implementation, is this intended or just an oversight ?

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

I've added Eugene's version, see org.springframework.context.support.SimpleThreadScope, with a big Javadoc warning that created objects will not be cleaned up. I've also added a link to David's Spring By Example version as an alternative.

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0 M2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants