Skip to content
This repository was archived by the owner on Aug 18, 2019. It is now read-only.

Configuration (Guice)

David Fuelling edited this page Jul 7, 2016 · 3 revisions

Configuration using Annotations

To utilize the a default configuration of the ShardedCounterService with Guice, add the following methods to one of your Guice modules:

@Provides
@RequestScoped
public Ofy provideOfy(OfyFactory fact)
{
	return fact.begin();
}

@Provides
@RequestScoped
public MemcacheService provideMemcacheService()
{
	return MemcacheServiceFactory.getMemcacheService();
}

// The entire app can have a single ShardedCounterServiceConfiguration, though making
// this request-scoped would allow the config to vary per-request
@Provides
@Singleton
public ShardedCounterServiceConfiguration provideShardedCounterServiceCoonfiguration()
{
	return new ShardedCounterServiceConfiguration.Builder().withNumInitialShards(2).build();
}

// Be safe and make this RequestScoped though not technically needed to be RequestScoped 
// since ShardedCounterServiceConfiguration and MemcacheService are immutable or utilize 
// thread-local internally.
@Provides
@RequestScoped
public ShardedCounterService provideShardedCounterService(MemcacheService memcacheService, ShardedCounterServiceConfiguration config)
{
	return new ShardedCounterService(memcacheService, config);
}

Don't forget to wire Objectify into Guice:

public class OfyFactory extends ObjectifyFactory
{
	/** Register our entity types */
	public OfyFactory()
	{
		final long registrationStartTime = System.currentTimeMillis();
		
		// ///////////////////
		// Register Entities
		// ///////////////////

		// ShardedCounter Entities
		register(Counter.class);
		register(CounterShard.class);
                    register(CounterShardOperationData.class);

		OfyFactory.logger.info("Objectify Class Registration took "
			+ (System.currentTimeMillis() - registrationStartTime) + " millis");
	}

	@Override
	public Ofy begin()
	{
		return new Ofy(this);
	}
}

For a more complete example of wiring Guice and Objectify, see the Motomapia Source.

Programmatic Configuration without Annotations

To utilize the default configuration of ShardedCounterService with Guice (without using Guice Annotations), add the following classes to your project to create Providers for the ShardedCounterServiceConfiguration, MemcacheService, and ShardedCounterService:

public class MemcacheServiceProvider implements Provider<MemcacheService>
{
	@Override
	public MemcacheService get()
	{
		return MemcacheServiceFactory.getMemcacheService();
	}
}

public class ShardedCounterServiceConfigurationProvider implements
		Provider<ShardedCounterServiceConfiguration>
{
	@Override
	public ShardedCounterServiceConfiguration get()
	{
		return new ShardedCounterServiceConfiguration.Builder().withNumInitialShards(3).build();
	}
}

public class ShardedCounterServiceProvider implements Provider<ShardedCounterService>
{
	private final ShardedCounterServiceConfiguration config;
	private final MemcacheService memcacheService;

	/**
	 * Required-args Constructor.
	 * 
	 * @param config
	 * @param memcacheService
	 */
	@Inject
	public ShardedCounterServiceProvider(final ShardedCounterServiceConfiguration config,
			final MemcacheService memcacheService)
	{
		this.config = config;
		this.memcacheService = memcacheService;
	}

	@Override
	public ShardedCounterService get()
	{
		return new ShardedCounterService(memcacheService, config);
	}
}

Finally, wire everything together in the configure() method of one of your Guice modules:

bind(MemcacheService.class).toProvider(MemcacheServiceProvider.class).in(RequestScoped.class);
// The entire app can have a single ShardedCounterServiceConfiguration, though making
// this request-scoped would allow the config to vary per-request
bind(ShardedCounterServiceConfiguration.class).toProvider(ShardedCounterServiceConfigurationProvider.class);
bind(ShardedCounterService.class).toProvider(ShardedCounterServiceProvider.class).in(RequestScoped.class);

Clone this wiki locally