0

We have a spring based application which has 2 levels of runnables: parents (dispatchers) and children (loaders) where each parent running in a ThreadPoolExecutor has a set of children (again in ThreadPoolExecutors) which require a set of parent specific values (which they obtain currently by calling parent.getSomeProperty())

My question is: how can i autowire beans used by a single parent and its children? Or is there a way to make those children aware of their "session"/"context" sort of saying (which can be a simple ID obtained by hashing "something"?

Sample use case:

  • Each dispatcher has a set of User attributes (like UserID, 3rdParty tokens/passwords, counters, isAborted..etc) which loaders reference
  • Loaders get data from 3rd party different endpoints and generate common Obejects (which have attributes coming from parent and 3rd party) and put them into a single FastMap (Javolution) which is consumed in batches by another bean doing DB INSERT/UPDATE in another thread

I'm currently passing the dispatcher "parent" into all of its children, which makes it mute to use AppContext.getBean(...) and then set common values/pass the parent.

6
  • Rather than couple the child to the parent, have you considered encapsulating the the parameters into some sort of context object and passing it to the child workers, possibly using a queue?
    – John R
    Jul 1, 2015 at 16:55
  • Same problem: what's the key of the Map shared "context" which the child uses? Alternatively, passing it becomes the same as passing the pointer to the parent...it cannot be autowired Jul 1, 2015 at 17:41
  • how your parent obtaining all those attributes ?
    – Bhupi
    Jul 1, 2015 at 18:01
  • It differs, 3 major usages: AMQ Message, HTTP Request, DB (User based stuff) Jul 1, 2015 at 19:02
  • Can a child ever belong to multiple parents?
    – John R
    Jul 1, 2015 at 21:29

1 Answer 1

0

Since I haven't got any answers, I'll post my own implementation which I have so far:

I implemented a SharedStore static object which has mainly two methods:

void putShared(Object o, String objectKey)
<T> T getShared(String objectKey, int parentCode, Class<T> type)

The parent thread calls put, while the children do the gets:

//Parent:
SomeClass someObject;
putShared(someObject, "someObjectKey");

This uses Thread.CurrentThread().hashCode() to put the object into a concurrent hash map

And the child has a key set in constructor:

//Child constructor in BaseRunnable abstract class
private int parentKey = Thread.currentThread().hashCode();

and calls the getMethod with:

//Child
getShared("someKey", parentKey, SomeClass.class);

And uses it as needed (which is done concurrently by multiple children inside the ThreadPoolExecutor in the child

All Runnable beans are autowired prototypes with lazy init and extend base abstract class which implements the default constructor and sets the parentKey, the rest of the stuff like DAOs and 3rdParty api obejects are autowired normally.

Criticism appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.