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-issuemaster opened this issue Sep 13, 2006 · 5 comments
Closed

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Sep 13, 2006

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-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Dec 13, 2006

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-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 24, 2007

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-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 9, 2008

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-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 18, 2008

Jorg Heymans commented

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

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jan 22, 2009

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.