You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@reef.apache.org by Tobin Baker <td...@cs.washington.edu> on 2015/10/14 21:24:35 UTC

Constructing other objects from a REEF Task using ambient configuration

I need to instantiate several objects from my Task implementation, and I'd
like to be able to inject constructor parameters for those objects using
the ambient configuration (Task + Context). I've been passing around
serialized configuration and creating injectors to instantiate each
parameter explicitly and pass it into the (non-injectable) constructor, but
there must be a better way. Basically, how do I access the ambient
configuration for a Task and use it to instantiate another object with an
injectable constructor?

Thanks,
Tobin

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Tobin Baker <td...@cs.washington.edu>.
Oh, interesting. I was doing something similar by injecting serialized
configuration and creating an injector in the constructor, but this is
certainly nicer.

On Wed, Oct 14, 2015 at 1:30 PM, Markus Weimer <ma...@weimo.de> wrote:

> An alternative (if a bit less elegant) is to have an `Injector`
> injected. Tang forks that off of the injector used to create the
> object. Hence, it has all the parameters.
>
> If you can, just depending on your other objects is the nicer solution,
> though.
>
> Markus
>
> On Wed, Oct 14, 2015 at 10:18 PM, Tobin Baker <td...@cs.washington.edu>
> wrote:
> > That makes sense. It'll require a lot of refactoring, but I'll see if I
> can
> > get this approach to work. Thanks.
> >
> > On Wed, Oct 14, 2015 at 12:45 PM, Markus Weimer <ma...@weimo.de> wrote:
> >
> >> Can't you just depend on an instance of those classes in your Task's
> >> constructor? The Task gets instantiated via Tang on an Injector that
> >> has all the context and task configuration available.
> >>
> >> Markus
> >>
> >> On Wed, Oct 14, 2015 at 9:37 PM, Tobin Baker <tdbaker@cs.washington.edu
> >
> >> wrote:
> >> > I should mention that the reason I'm looking for an alternative is
> that I
> >> > can't figure out how to extract default values for optional parameters
> >> in a
> >> > ConfigurationModule by constructing an injector from a serialized
> >> > Configuration and calling getNamedInstance. getNamedInstance
> apparently
> >> > doesn't extract default values at all for missing variables in a
> >> > Configuration, and I'm wondering if the only way to access default
> values
> >> > from an injector is to use it to instantiate an object with an
> injectable
> >> > constructor (getInstance).
> >> >
> >> > On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <
> tdbaker@cs.washington.edu
> >> >
> >> > wrote:
> >> >
> >> >> I need to instantiate several objects from my Task implementation,
> and
> >> I'd
> >> >> like to be able to inject constructor parameters for those objects
> using
> >> >> the ambient configuration (Task + Context). I've been passing around
> >> >> serialized configuration and creating injectors to instantiate each
> >> >> parameter explicitly and pass it into the (non-injectable)
> constructor,
> >> but
> >> >> there must be a better way. Basically, how do I access the ambient
> >> >> configuration for a Task and use it to instantiate another object
> with
> >> an
> >> >> injectable constructor?
> >> >>
> >> >> Thanks,
> >> >> Tobin
> >> >>
> >> >>
> >>
>

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Markus Weimer <ma...@weimo.de>.
I believe so, yes. Also, you probably want to wrap the Injector in an
InjectionFuture. That way, you can have it passed in the constructor,
but only access it much later when it is fully baked.

Markus

On Thu, Oct 15, 2015 at 12:48 AM, Tobin Baker <td...@cs.washington.edu> wrote:
> If I try to use the injected Injector inside my constructor, I get this:
>
> java.util.ConcurrentModificationException: Detected attempt to use Injector
> from within an injected constructor!
>
> Presumably I could use the Injector outside the constructor (haven't tried
> that yet)?
>
> On Wed, Oct 14, 2015 at 1:30 PM, Markus Weimer <ma...@weimo.de> wrote:
>
>> An alternative (if a bit less elegant) is to have an `Injector`
>> injected. Tang forks that off of the injector used to create the
>> object. Hence, it has all the parameters.
>>
>> If you can, just depending on your other objects is the nicer solution,
>> though.
>>
>> Markus
>>
>> On Wed, Oct 14, 2015 at 10:18 PM, Tobin Baker <td...@cs.washington.edu>
>> wrote:
>> > That makes sense. It'll require a lot of refactoring, but I'll see if I
>> can
>> > get this approach to work. Thanks.
>> >
>> > On Wed, Oct 14, 2015 at 12:45 PM, Markus Weimer <ma...@weimo.de> wrote:
>> >
>> >> Can't you just depend on an instance of those classes in your Task's
>> >> constructor? The Task gets instantiated via Tang on an Injector that
>> >> has all the context and task configuration available.
>> >>
>> >> Markus
>> >>
>> >> On Wed, Oct 14, 2015 at 9:37 PM, Tobin Baker <tdbaker@cs.washington.edu
>> >
>> >> wrote:
>> >> > I should mention that the reason I'm looking for an alternative is
>> that I
>> >> > can't figure out how to extract default values for optional parameters
>> >> in a
>> >> > ConfigurationModule by constructing an injector from a serialized
>> >> > Configuration and calling getNamedInstance. getNamedInstance
>> apparently
>> >> > doesn't extract default values at all for missing variables in a
>> >> > Configuration, and I'm wondering if the only way to access default
>> values
>> >> > from an injector is to use it to instantiate an object with an
>> injectable
>> >> > constructor (getInstance).
>> >> >
>> >> > On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <
>> tdbaker@cs.washington.edu
>> >> >
>> >> > wrote:
>> >> >
>> >> >> I need to instantiate several objects from my Task implementation,
>> and
>> >> I'd
>> >> >> like to be able to inject constructor parameters for those objects
>> using
>> >> >> the ambient configuration (Task + Context). I've been passing around
>> >> >> serialized configuration and creating injectors to instantiate each
>> >> >> parameter explicitly and pass it into the (non-injectable)
>> constructor,
>> >> but
>> >> >> there must be a better way. Basically, how do I access the ambient
>> >> >> configuration for a Task and use it to instantiate another object
>> with
>> >> an
>> >> >> injectable constructor?
>> >> >>
>> >> >> Thanks,
>> >> >> Tobin
>> >> >>
>> >> >>
>> >>
>>

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Tobin Baker <td...@cs.washington.edu>.
If I try to use the injected Injector inside my constructor, I get this:

java.util.ConcurrentModificationException: Detected attempt to use Injector
from within an injected constructor!

Presumably I could use the Injector outside the constructor (haven't tried
that yet)?

On Wed, Oct 14, 2015 at 1:30 PM, Markus Weimer <ma...@weimo.de> wrote:

> An alternative (if a bit less elegant) is to have an `Injector`
> injected. Tang forks that off of the injector used to create the
> object. Hence, it has all the parameters.
>
> If you can, just depending on your other objects is the nicer solution,
> though.
>
> Markus
>
> On Wed, Oct 14, 2015 at 10:18 PM, Tobin Baker <td...@cs.washington.edu>
> wrote:
> > That makes sense. It'll require a lot of refactoring, but I'll see if I
> can
> > get this approach to work. Thanks.
> >
> > On Wed, Oct 14, 2015 at 12:45 PM, Markus Weimer <ma...@weimo.de> wrote:
> >
> >> Can't you just depend on an instance of those classes in your Task's
> >> constructor? The Task gets instantiated via Tang on an Injector that
> >> has all the context and task configuration available.
> >>
> >> Markus
> >>
> >> On Wed, Oct 14, 2015 at 9:37 PM, Tobin Baker <tdbaker@cs.washington.edu
> >
> >> wrote:
> >> > I should mention that the reason I'm looking for an alternative is
> that I
> >> > can't figure out how to extract default values for optional parameters
> >> in a
> >> > ConfigurationModule by constructing an injector from a serialized
> >> > Configuration and calling getNamedInstance. getNamedInstance
> apparently
> >> > doesn't extract default values at all for missing variables in a
> >> > Configuration, and I'm wondering if the only way to access default
> values
> >> > from an injector is to use it to instantiate an object with an
> injectable
> >> > constructor (getInstance).
> >> >
> >> > On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <
> tdbaker@cs.washington.edu
> >> >
> >> > wrote:
> >> >
> >> >> I need to instantiate several objects from my Task implementation,
> and
> >> I'd
> >> >> like to be able to inject constructor parameters for those objects
> using
> >> >> the ambient configuration (Task + Context). I've been passing around
> >> >> serialized configuration and creating injectors to instantiate each
> >> >> parameter explicitly and pass it into the (non-injectable)
> constructor,
> >> but
> >> >> there must be a better way. Basically, how do I access the ambient
> >> >> configuration for a Task and use it to instantiate another object
> with
> >> an
> >> >> injectable constructor?
> >> >>
> >> >> Thanks,
> >> >> Tobin
> >> >>
> >> >>
> >>
>

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Markus Weimer <ma...@weimo.de>.
An alternative (if a bit less elegant) is to have an `Injector`
injected. Tang forks that off of the injector used to create the
object. Hence, it has all the parameters.

If you can, just depending on your other objects is the nicer solution, though.

Markus

On Wed, Oct 14, 2015 at 10:18 PM, Tobin Baker <td...@cs.washington.edu> wrote:
> That makes sense. It'll require a lot of refactoring, but I'll see if I can
> get this approach to work. Thanks.
>
> On Wed, Oct 14, 2015 at 12:45 PM, Markus Weimer <ma...@weimo.de> wrote:
>
>> Can't you just depend on an instance of those classes in your Task's
>> constructor? The Task gets instantiated via Tang on an Injector that
>> has all the context and task configuration available.
>>
>> Markus
>>
>> On Wed, Oct 14, 2015 at 9:37 PM, Tobin Baker <td...@cs.washington.edu>
>> wrote:
>> > I should mention that the reason I'm looking for an alternative is that I
>> > can't figure out how to extract default values for optional parameters
>> in a
>> > ConfigurationModule by constructing an injector from a serialized
>> > Configuration and calling getNamedInstance. getNamedInstance apparently
>> > doesn't extract default values at all for missing variables in a
>> > Configuration, and I'm wondering if the only way to access default values
>> > from an injector is to use it to instantiate an object with an injectable
>> > constructor (getInstance).
>> >
>> > On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <tdbaker@cs.washington.edu
>> >
>> > wrote:
>> >
>> >> I need to instantiate several objects from my Task implementation, and
>> I'd
>> >> like to be able to inject constructor parameters for those objects using
>> >> the ambient configuration (Task + Context). I've been passing around
>> >> serialized configuration and creating injectors to instantiate each
>> >> parameter explicitly and pass it into the (non-injectable) constructor,
>> but
>> >> there must be a better way. Basically, how do I access the ambient
>> >> configuration for a Task and use it to instantiate another object with
>> an
>> >> injectable constructor?
>> >>
>> >> Thanks,
>> >> Tobin
>> >>
>> >>
>>

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Tobin Baker <td...@cs.washington.edu>.
That makes sense. It'll require a lot of refactoring, but I'll see if I can
get this approach to work. Thanks.

On Wed, Oct 14, 2015 at 12:45 PM, Markus Weimer <ma...@weimo.de> wrote:

> Can't you just depend on an instance of those classes in your Task's
> constructor? The Task gets instantiated via Tang on an Injector that
> has all the context and task configuration available.
>
> Markus
>
> On Wed, Oct 14, 2015 at 9:37 PM, Tobin Baker <td...@cs.washington.edu>
> wrote:
> > I should mention that the reason I'm looking for an alternative is that I
> > can't figure out how to extract default values for optional parameters
> in a
> > ConfigurationModule by constructing an injector from a serialized
> > Configuration and calling getNamedInstance. getNamedInstance apparently
> > doesn't extract default values at all for missing variables in a
> > Configuration, and I'm wondering if the only way to access default values
> > from an injector is to use it to instantiate an object with an injectable
> > constructor (getInstance).
> >
> > On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <tdbaker@cs.washington.edu
> >
> > wrote:
> >
> >> I need to instantiate several objects from my Task implementation, and
> I'd
> >> like to be able to inject constructor parameters for those objects using
> >> the ambient configuration (Task + Context). I've been passing around
> >> serialized configuration and creating injectors to instantiate each
> >> parameter explicitly and pass it into the (non-injectable) constructor,
> but
> >> there must be a better way. Basically, how do I access the ambient
> >> configuration for a Task and use it to instantiate another object with
> an
> >> injectable constructor?
> >>
> >> Thanks,
> >> Tobin
> >>
> >>
>

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Markus Weimer <ma...@weimo.de>.
Can't you just depend on an instance of those classes in your Task's
constructor? The Task gets instantiated via Tang on an Injector that
has all the context and task configuration available.

Markus

On Wed, Oct 14, 2015 at 9:37 PM, Tobin Baker <td...@cs.washington.edu> wrote:
> I should mention that the reason I'm looking for an alternative is that I
> can't figure out how to extract default values for optional parameters in a
> ConfigurationModule by constructing an injector from a serialized
> Configuration and calling getNamedInstance. getNamedInstance apparently
> doesn't extract default values at all for missing variables in a
> Configuration, and I'm wondering if the only way to access default values
> from an injector is to use it to instantiate an object with an injectable
> constructor (getInstance).
>
> On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <td...@cs.washington.edu>
> wrote:
>
>> I need to instantiate several objects from my Task implementation, and I'd
>> like to be able to inject constructor parameters for those objects using
>> the ambient configuration (Task + Context). I've been passing around
>> serialized configuration and creating injectors to instantiate each
>> parameter explicitly and pass it into the (non-injectable) constructor, but
>> there must be a better way. Basically, how do I access the ambient
>> configuration for a Task and use it to instantiate another object with an
>> injectable constructor?
>>
>> Thanks,
>> Tobin
>>
>>

Re: Constructing other objects from a REEF Task using ambient configuration

Posted by Tobin Baker <td...@cs.washington.edu>.
I should mention that the reason I'm looking for an alternative is that I
can't figure out how to extract default values for optional parameters in a
ConfigurationModule by constructing an injector from a serialized
Configuration and calling getNamedInstance. getNamedInstance apparently
doesn't extract default values at all for missing variables in a
Configuration, and I'm wondering if the only way to access default values
from an injector is to use it to instantiate an object with an injectable
constructor (getInstance).

On Wed, Oct 14, 2015 at 12:24 PM, Tobin Baker <td...@cs.washington.edu>
wrote:

> I need to instantiate several objects from my Task implementation, and I'd
> like to be able to inject constructor parameters for those objects using
> the ambient configuration (Task + Context). I've been passing around
> serialized configuration and creating injectors to instantiate each
> parameter explicitly and pass it into the (non-injectable) constructor, but
> there must be a better way. Basically, how do I access the ambient
> configuration for a Task and use it to instantiate another object with an
> injectable constructor?
>
> Thanks,
> Tobin
>
>