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

Add EmbulkService#newInjector #239

Merged
merged 1 commit into from Jul 23, 2015

Conversation

muga
Copy link
Contributor

@muga muga commented Jul 22, 2015

To override binding, this method is useful like:

class MyEmbulkService extends EmbulkService
{
    @Override
    Injector newInjector(ImmutableList<Module> modules)
    {
      return Guice.createInjector(Modules.override(modules).with(new MyExecModule()));
    }
}

@frsyuki
Copy link
Contributor

frsyuki commented Jul 22, 2015

why don't you override EmbulkService#getAdditionalModules?

@muga
Copy link
Contributor Author

muga commented Jul 22, 2015

Since Modules.override method requires all of loading modules, getAdditionalModules is not enough to get all of them. My override sample is following:

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.util.Modules;

public class InjectorTest
{
    public interface InfA { void doIt(); }

    public interface InfB { void doIt(); }

    public interface InfC { void doIt(); }

    public static class ConcA
            implements InfA
    {
        @Inject
        public ConcA(InfB b, InfC c)
        {
            b.doIt();
            c.doIt();
        }

        public void doIt() { System.out.println("hellp A"); }
    }

    public static class ConcB
            implements InfB
    {
        public void doIt() { System.out.println("hello B"); }
    }

    public static class ConcC
            implements InfC
    {
        public void doIt() { System.out.println("hello C"); }
    }

    public static class ConcC2
            implements InfC
    {
        public void doIt() { System.out.println("hello C2"); }
    }

    public static class ProdMod1
            implements Module
    {
        public void configure(Binder binder)
        {
            binder.bind(InfA.class).to(ConcA.class);
            binder.bind(InfB.class).to(ConcB.class);
        }
    }

    public static class ProdMod2
            implements Module
    {
        public void configure(Binder binder) {
            binder.bind(InfC.class).to(ConcC.class);
        }
    }

    public static class ProdMod3
            implements Module
    {
        public void configure(Binder binder) {
            binder.bind(InfC.class).to(ConcC2.class);
        }
    }

    public static void main(String[] args) throws Exception
    {
        /*
        // outout:
        // hello B
        // hello C
        // hello A
        Injector injector = Guice.createInjector(new ProdMod1(), new ProdMod2());
        ConcA a = injector.getInstance(ConcA.class);
        a.doIt();
         */

        /*
         // output:
         // Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:
         //
         // 1) No implementation for InjectorTest$InfB was bound.
         // while locating InjectorTest$InfB
         // for parameter 0 at InjectorTest$ConcA.<init>(InjectorTest.java:21)
         // while locating InjectorTest$ConcA
         //
         // 1 error
         // at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1042)
         // at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1001)
         // at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
        Injector injector2 = Guice.createInjector(Modules.override(new ProdMod2()).with(new ProdMod3()));
        ConcA a2 = injector2.getInstance(ConcA.class);
        a2.doIt();
         */

        // outout:
        // hello B
        // hello C2
        // hello A
        Injector injector3 = Guice.createInjector(Modules.override(new ProdMod1(), new ProdMod2()).with(new ProdMod3()));
        //Injector injector3 = Guice.createInjector(Modules.override(new ProdMod2(), new ProdMod1()).with(new ProdMod3()));
        ConcA a3 = injector3.getInstance(ConcA.class);
        a3.doIt();
    }
}

@frsyuki
Copy link
Contributor

frsyuki commented Jul 22, 2015

I think that calling newInjector at constructor can lead bugs.

@muga Does this work instead?: 1a6df48

@muga
Copy link
Contributor Author

muga commented Jul 23, 2015

Thank you for fixing. LocalThreadExecutor works fine. I will also check the behavior of MapReduceExecutor.

@muga
Copy link
Contributor Author

muga commented Jul 23, 2015

I checked the behavior of MapReduceExecutor. This fix works fine.

@muga
Copy link
Contributor Author

muga commented Jul 23, 2015

GJ 👍

frsyuki added a commit that referenced this pull request Jul 23, 2015
@frsyuki frsyuki merged commit 2fbeca5 into master Jul 23, 2015
frsyuki added a commit that referenced this pull request Jul 23, 2015
add EmbulkService.overrideModules so that apps can use Modules.override [#239]
@frsyuki frsyuki deleted the add_newinjector_method_to_embulkservice branch July 23, 2015 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants