Skip to content

Commit

Permalink
changes to the way ConnectionProvider is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
smadappa committed Jun 15, 2017
1 parent 248c6db commit ea202ea
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
39 changes: 34 additions & 5 deletions evcache-client/src/com/netflix/evcache/EVCacheModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,69 @@
import javax.annotation.PreDestroy;

import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Singleton;
import com.netflix.archaius.api.annotations.ConfigurationSource;
import com.netflix.evcache.connection.DIConnectionModule;
import com.netflix.evcache.connection.IConnectionBuilder;
import com.netflix.evcache.event.hotkey.HotKeyListener;
import com.netflix.evcache.event.throttle.ThrottleListener;
import com.netflix.evcache.pool.EVCacheClientPoolManager;
import com.netflix.evcache.pool.EVCacheNodeList;
import com.netflix.evcache.pool.eureka.DIEVCacheNodeListProvider;

@Singleton
@SuppressWarnings("deprecation")
public class EVCacheModule extends AbstractModule {

public EVCacheModule() {
}

@Singleton
@ConfigurationSource("evcache")
public static class EVCacheModuleConfigLoader {

@Inject
public EVCacheModuleConfigLoader(Injector injector, EVCacheModule module) {
if(injector.getExistingBinding(Key.get(IConnectionBuilder.class)) == null) {
module.install(new DIConnectionModule());
}
}
}


@Override
protected void configure() {
// Make sure connection factory provider Module is initialized in your Module when you init EVCacheModule
install(new DIConnectionModule());
// Make sure connection factory provider Module is initialized in your Module when you init EVCacheModule
bind(EVCacheModuleConfigLoader.class).asEagerSingleton();
bind(EVCacheNodeList.class).toProvider(DIEVCacheNodeListProvider.class);
bind(EVCacheClientPoolManager.class).asEagerSingleton();

bind(HotKeyListener.class).asEagerSingleton();
bind(ThrottleListener.class).asEagerSingleton();
}


@Inject
EVCacheClientPoolManager manager;

@PostConstruct
public void init() {
EVCacheClientPoolManager.getInstance().initAtStartup();
if(manager != null) {
manager.initAtStartup();
} else {
EVCacheClientPoolManager.getInstance().initAtStartup();
}
}

@PreDestroy
public void shutdown() {
EVCacheClientPoolManager.getInstance().shutdown();
if(manager != null) {
manager.shutdown();
} else {
EVCacheClientPoolManager.getInstance().shutdown();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ public DIConnectionModule() {
}

@Override
// Make sure this is done
protected void configure() {
if(getProvider(IConnectionBuilder.class) == null ) {
bind(IConnectionBuilder.class).toProvider(DIConnectionFactoryBuilderProvider.class);
}
bind(IConnectionBuilder.class).toProvider(DIConnectionFactoryBuilderProvider.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ public <T> Builder enableExceptionPropagation() {
* Returns a newly created {@code EVCache} based on the contents of the
* {@code Builder}.
*/
@SuppressWarnings("deprecation")
public EVCache build() {
if (_poolManager == null) {
_poolManager = EVCacheClientPoolManager.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.netflix.evcache.pool.EVCacheClientPoolManager;
import com.netflix.evcache.pool.EVCacheKetamaNodeLocatorConfiguration;
import com.netflix.evcache.pool.EVCacheNodeLocator;
import com.netflix.evcache.pool.ServerGroup;
import com.netflix.evcache.util.EVCacheConfig;

import net.spy.memcached.BinaryConnectionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ public class EVCacheClientPoolManager {
public EVCacheClientPoolManager(IConnectionBuilder connectionFactoryprovider, EVCacheNodeList evcacheNodeList) {
instance = this;

try {
ConfigurationManager.loadCascadedPropertiesFromResources("evcache");
} catch (IOException e) {
log.info("Default evcache configuration not loaded", e);
}

this.connectionFactoryProvider = connectionFactoryprovider;
this.evcacheNodeList = evcacheNodeList;
this.evcacheEventListenerList = new ArrayList<EVCacheEventListener>();
Expand Down Expand Up @@ -142,6 +136,12 @@ public List<EVCacheEventListener> getEVCacheEventListeners() {
@Deprecated
public static EVCacheClientPoolManager getInstance() {
if (instance == null) {
try {
ConfigurationManager.loadCascadedPropertiesFromResources("evcache");
} catch (IOException e) {
log.info("Default evcache configuration not loaded", e);
}

new EVCacheClientPoolManager(new ConnectionFactoryBuilder(), new SimpleNodeListProvider());
if (!EVCacheConfig.getInstance().getDynamicBooleanProperty("evcache.use.simple.node.list.provider", false).get()) {
if(log.isDebugEnabled()) log.debug("Please make sure EVCacheClientPoolManager is injected first. This is not the appropriate way to init EVCacheClientPoolManager."
Expand Down

0 comments on commit ea202ea

Please sign in to comment.