-
Notifications
You must be signed in to change notification settings - Fork 13
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
Base DependencyInjection setup #11
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
package org.vss; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.ApplicationPath; | ||
import org.glassfish.hk2.api.ServiceLocator; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.jvnet.hk2.guice.bridge.api.GuiceBridge; | ||
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge; | ||
import org.vss.guice.BaseModule; | ||
|
||
@ApplicationPath("/") | ||
public class VSSApplication extends ResourceConfig { | ||
public VSSApplication() { | ||
|
||
@Inject | ||
public VSSApplication(ServiceLocator serviceLocator) { | ||
packages("org.vss"); | ||
Injector injector = Guice.createInjector(new BaseModule()); | ||
initGuiceIntoHK2Bridge(serviceLocator, injector); | ||
} | ||
|
||
// By default, Jersey framework uses HK2 for dependency injection. | ||
// To use Guice as our dependency injection framework, we provide guice injector to hk2-bridge. | ||
// So that hk2 can query guice injector for creating/injecting objects. | ||
private void initGuiceIntoHK2Bridge(ServiceLocator serviceLocator, Injector injector) { | ||
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator); | ||
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class); | ||
guiceBridge.bridgeGuiceInjector(injector); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.vss.guice; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Provides; | ||
import com.google.inject.Singleton; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
import org.jooq.DSLContext; | ||
import org.jooq.SQLDialect; | ||
import org.jooq.impl.DSL; | ||
import org.vss.KVStore; | ||
import org.vss.impl.postgres.PostgresBackendImpl; | ||
|
||
public class BaseModule extends AbstractModule { | ||
|
||
@Override | ||
protected void configure() { | ||
// Provide PostgresBackend as default implementation for KVStore. | ||
bind(KVStore.class).to(PostgresBackendImpl.class).in(Singleton.class); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
// Provide DSLContext which is to be used by PostgresBackend | ||
public DSLContext provideDSLContext() throws ClassNotFoundException { | ||
// Required to load postgres drivers in tomcat | ||
Class.forName("org.postgresql.Driver"); | ||
return DSL.using(HikariCPDataSource.dataSource, SQLDialect.POSTGRES); | ||
} | ||
} | ||
|
||
// Provide Hikari Connection Pooling configuration for jdbc connection management. | ||
// Hikari is high-performance connection pooling library which will maintain a set of connections | ||
// to the database for us. | ||
// When we provide `HikariCPDataSource` to DSLContext, jOOQ will internally `acquire` and `release` | ||
// connections from pool. | ||
// For HikariCP config, we provide some sane defaults, but they are meant to be changed and tuned. | ||
// For specific parameter functionality, refer to HikariCP docs. | ||
class HikariCPDataSource { | ||
|
||
private static HikariConfig config = new HikariConfig(); | ||
public static HikariDataSource dataSource; | ||
|
||
static { | ||
try (InputStream input = HikariCPDataSource.class.getClassLoader() | ||
.getResourceAsStream("hikariJdbc.properties")) { | ||
Properties hikariJdbcProperties = new Properties(); | ||
hikariJdbcProperties.load(input); | ||
|
||
config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url")); | ||
config.setUsername(hikariJdbcProperties.getProperty("jdbc.username")); | ||
config.setPassword(hikariJdbcProperties.getProperty("jdbc.password")); | ||
|
||
config.setMaximumPoolSize( | ||
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize"))); | ||
config.setMinimumIdle( | ||
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle"))); | ||
config.setConnectionTimeout( | ||
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout"))); | ||
config.setIdleTimeout( | ||
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout"))); | ||
config.setMaxLifetime( | ||
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime"))); | ||
|
||
config.addDataSourceProperty("cachePrepStmts", | ||
hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts")); | ||
config.addDataSourceProperty("prepStmtCacheSize", | ||
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize")); | ||
config.addDataSourceProperty("prepStmtCacheSqlLimit", | ||
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit")); | ||
|
||
dataSource = new HikariDataSource(config); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Unable to read hikariJdbcProperties from resources"); | ||
} | ||
} | ||
G8XSU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private HikariCPDataSource() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Default properties, these are meant to be changed according to application needs. | ||
|
||
jdbc.url=jdbc:postgresql://localhost:5432/postgres | ||
jdbc.username=postgres | ||
jdbc.password= | ||
|
||
# Idle Timeout | ||
hikaricp.minimumIdle=10 | ||
|
||
# Set connectionTimeout to 30 secs | ||
hikaricp.connectionTimeout=30000 | ||
|
||
# Set idle timeout to 10 minutes | ||
hikaricp.idleTimeout=600000 | ||
|
||
# Set Maximum lifetime of a connection to 30minutes | ||
hikaricp.maxLifetime=1800000 | ||
|
||
# Performance Optimizations | ||
hikaricp.maxPoolSize=50 | ||
hikaricp.cachePrepStmts=true | ||
hikaricp.prepStmtCacheSize=250 | ||
hikaricp.prepStmtCacheSqlLimit=2048 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having no idea how this works, could you give a TL; DR context in the commit message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think in this particular case, it would add value to include explaination-docs in code as well, working on adding it.