osgi - guice - select different providers while running -
i want change provider i'm using @ runtime without having stop jvm. example, isn't i'm trying do, idea same: say, want switch amazon s3 google cloud storage in middle of running application.
is can within guice?
i have have jars available @ runtime , configure modules @ startup. then, later once application started, i'd have use provider can determine instance inject @ startup , later on when changes.
or, better restart application after updating configuration , system proceed configuration , if needs change, application again need restarted.
would osgi here?
you don't need extra: guice can out-of-the-box. but... you'll have use provider
s instead of direct instance.
in module
bind(cloud.class) .annotatedwith(names.named("google")) .to(googlecloud.class); bind(cloud.class) .annotatedwith(names.named("amazon")) .to(amazoncloud.class); bind(cloud.class) .toprovider(switchingcloudprovider.class);
somewhere
class switchingcloudprovider implements provider<cloud> { @inject @named("google") provider<cloud> googlecloudprovider; @inject @named("amazon") provider<cloud> amazoncloudprovider; @inject configuration configuration; // used switch "commander" public cloud get() { switch(configuration.getcloudname()) { case "google": return googlecloudprovider.get(); case "amazon": return amazoncloudprovider.get(); default: // whatever want, exception. } } }
or in provider method in module
@provides cloud providecloud( @named("google") provider<cloud> googlecloudprovider, @named("amazon") provider<cloud> amazoncloudprovider, configuration configuration) { switch(configuration.getcloudname()) { case "google": return googlecloudprovider.get(); case "amazon": return amazoncloudprovider.get(); default: // whatever want, exception. } }
usage
class foo { @inject provider<cloud> cloudprovider; // not inject cloud directly or won't changes come up. public void bar() { cloud cloud = cloudprovider.get(); // use cloud } }
Comments
Post a Comment