storm-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Petr Janeček <JanecekP...@seznam.cz>
Subject Re: Storm development : Using dependency injection
Date Sat, 04 Feb 2017 00:07:39 GMT
"I can pass factories to my constructor and instantiate object in 
prepare method of my bolt"
We're doing exactly this, plus the trick with a serializable lambda in 
Java 8. Therefore, using these types:

// when you don't need to look at config
interface Factory<T> extends Supplier<T>, Serializable {
     @Override
     public T get();
}

// when you do
interface ConfigFactory<T> extends Function<Map<String, Object>, T>, 
Serializable {
     @Override
     public T apply(Map<String, Object> stormConf);
}

we create a lambda that we pass through the constructor and then let it 
supply us an instance on the other side in the `prepare()` method. It 
still requires a few extra fields that are named "somethingFactory" and 
are only used for instantiation, but overall that's fine as the whole 
thing is testable and looks clean enough.

The previous alternative we were using was not passing through anything, 
and then in the `prepare()` method we'd just call:

databaseConnectionPool = StaticDbPoolFactory.create(stormConf);

and let the static factory decide which instance it would give to us. 
This works, but is nowhere near as good nor clean as the newer approach. 
It requires the `StaticDbPoolFactory` to know about tests, it makes 
dependencies implicit instead of explicit etc.

PJ

Dne 4. 2. 2017 v 0:01 Stephen Powis napsal(a):
> Also interested to know if anyone has come up with a clean pattern for 
> this.
>
> On Thu, Feb 2, 2017 at 11:37 PM, Bastien DINE 
> <bastien.dine@coservit.com <mailto:bastien.dine@coservit.com>> wrote:
>
>     Hi everyone,
>
>     I’m trying to develop my new topologies using a proper design
>     pattern,  to achieve :
>
>     -Reusability of class
>
>     -Unit testing / at least functional
>
>     oBe able to mock database interaction through interfaces
>
>     I worked a lot with PHP & Symfony which is a great framework to
>     achieve those goals using dependency injection pattern
>
>     I want to apply it to Storm topology development, but here is my
>     problem :
>
>     How can I pass dependency in constructor (e.g Cassandra provider,
>     or id resolver, or even object hydrator), the bolt are
>     instantiated when calling “prepare” method
>
>     If I’m using a DI framework (like google Guice), how can I Mixed
>     it with storm topology builder ?
>
>     One idea :
>
>     I think I can pass factories to my constructor and instantiate
>     object in prepare method of my bolt
>
>     But I’m not sure if it a good way to do it..
>
>     Did  anybody ever experience it ?
>
>     Does anyone have some best practices to develop topologies ?
>     (regarding code engineering and organization)
>
>     Thanks in advance,
>
>     Regards
>
>     Bastien
>
>


Mime
View raw message