Results 1 to 3 of 3

Thread: Inheritance of Application Context to child threads

  1. #1
    Join Date
    Feb 2010
    Posts
    1

    Default Inheritance of Application Context to child threads

    Hello,
    I am developing an simple REST resource with Spring MVC. The resource aggregates some data from other data sources. The processing of request would be much faster when I parallelize all subrequests to other data sources.

    I use an TaskExecutor that maintains a pool of threads and these threads are used by main thread that processes an incomming HTTP request. The main thread starts all subtasks, waits until all of them are finished and construct an response.

    When the threads processing subtask they need to be in the same Application context as the main thread, becouse they need to read some request scoped beans.

    Could you please give me some hint how can I inject the application context to subthreads or is this idea completely wrong by design?

    Many thanks!
    Jiri

  2. #2
    Join Date
    Dec 2009
    Location
    Pune,India
    Posts
    60

    Default single context in parallel processing.

    here is quick response i have very little time. if you have problem ask me.


    I have currently executed parallel thread in my project using weblogic work manager. i gathered data from diffrent web services. i call this web services parallelly.

    i have web service call which delegate call to my ParallelHandler method which is simle java class.

    in my main application context of web service say applicaation-Context.xml.
    i have
    you may declare in same application context instead of importing.

    <import resource="classpath:applicationContext-ParallelComponent.xml"/>

    <bean id-"balabal"> etc. all ben defination.


    in my applicationContext-ParallelComponent.xml

    there are 2 entreis.
    say
    <bean id="workManagerTaskExecutor" class="org.springframework.scheduling.commonj.Work ManagerTaskExecutor">

    <property name="workManagerName" value="java:comp/env/wm/MyWorkManager"/>

    </bean>

    <bean id="myHandlerService" class="foo.com.impl.MyParallelHandler" init-method="initBeanFactory">
    <property name="workManagerTaskExecutor">
    <ref bean="workManagerTaskExecutor" />
    </property>
    </bean>



    init-method is initialize method which load bean-ref application context.

    beanRefContext.xml

    <bean id="commonsBeanFactory" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
    <constructor-arg>
    <list>
    <value>parellelDataBuilder.xml</value>
    </list>
    </constructor-arg>
    </bean>







    in my java parallel hanlder classes

    public class MyParallelHandler implements MyHandlerService {

    /**
    * bean factory
    */
    private BeanFactory beanFactory;

    private WorkManagerTaskExecutor workManagerTaskExecutor = null;


    public void initBeanFactory() {

    BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator
    .getInstance("beanRefContext.xml");
    beanFactory = locator.useBeanFactory("commonsBeanFactory").getFa ctory();
    }

    in my
    parellelDataBuilder.xml i have define all the bean which implement weblogic common j work interface object.

    for example
    <bean id="dummyDataWork" class="mywork.DummyDataWork" scope="prototype">
    <property name="dataBuilder">
    <ref bean="dataBuilder" />
    </property>
    </bean>

    This builder will call another web service but that is not important here.


    in your parallelHanlder claass mentiond above say some method.

    someMethod(){
    DummyDataWork dummyDataWork= (DummyDataWork) beanFactory
    .getBean("dummyDataWork");

    // this must be prototype scope. since one object per request.

    }

  3. #3
    Join Date
    Dec 2009
    Location
    Pune,India
    Posts
    60

    Default thread and application context.

    first you need to seperate all the beans you want to use in thred object.
    and declared all the java beans which are threads ( implment runable etc) in your same bean file and inject all prototype bean in your thread beans. your thread bean will also prototype.

    you then need to access those bean programmatically using getBean.

    you can can load this application context using approach i have used above.

    in Spring 3.0 i have heard about thread scope please look in to it.
    method injection also you can use.

    i have implemented above one in spring 2.0.7

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •