From 25f99af6660cf0613769bea59a6fb983e0f3c45a Mon Sep 17 00:00:00 2001 From: Artem Sidorkin Date: Tue, 19 Jan 2021 11:26:27 +0300 Subject: [PATCH] Fixed bean scopes. See: https://github.com/eventuate-tram/eventuate-tram-core-quarkus/issues/3 --- .../quarkus/id/IdGeneratorConfiguration.java | 9 ++++----- ...ntuateCommonInMemoryDatabaseConfiguration.java | 7 +++---- .../EventuateCommonInMemoryDatabaseTest.java | 5 ++--- .../DataSourceSelectorConfiguration.java | 7 +++---- ...ventuateCommonJdbcOperationsConfiguration.java | 14 +++++--------- .../jdbc/EventuateQuarkusTransactionTemplate.java | 4 ++-- .../jdbc/EventuateSchemaConfiguration.java | 7 +++---- .../jdbc/sqldialect/SqlDialectConfiguration.java | 15 +++++++-------- 8 files changed, 29 insertions(+), 39 deletions(-) diff --git a/eventuate-common-quarkus-id/src/main/java/io/eventuate/common/quarkus/id/IdGeneratorConfiguration.java b/eventuate-common-quarkus-id/src/main/java/io/eventuate/common/quarkus/id/IdGeneratorConfiguration.java index cee64c5..d3ad6f5 100755 --- a/eventuate-common-quarkus-id/src/main/java/io/eventuate/common/quarkus/id/IdGeneratorConfiguration.java +++ b/eventuate-common-quarkus-id/src/main/java/io/eventuate/common/quarkus/id/IdGeneratorConfiguration.java @@ -1,17 +1,16 @@ package io.eventuate.common.quarkus.id; import io.eventuate.common.id.ApplicationIdGenerator; -import io.eventuate.common.id.IdGenerator; import io.eventuate.common.id.DatabaseIdGenerator; +import io.eventuate.common.id.IdGenerator; import org.eclipse.microprofile.config.inject.ConfigProperty; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Produces; +import javax.inject.Singleton; import java.util.Optional; -@ApplicationScoped +@Singleton public class IdGeneratorConfiguration { - @Produces + @Singleton public IdGenerator idGenerator(@ConfigProperty(name = "eventuate.outbox.id") Optional id) { return id .map(DatabaseIdGenerator::new) diff --git a/eventuate-common-quarkus-in-memory-database/src/main/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseConfiguration.java b/eventuate-common-quarkus-in-memory-database/src/main/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseConfiguration.java index 42695a9..6dd607f 100755 --- a/eventuate-common-quarkus-in-memory-database/src/main/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseConfiguration.java +++ b/eventuate-common-quarkus-in-memory-database/src/main/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseConfiguration.java @@ -4,18 +4,17 @@ import io.eventuate.common.inmemorydatabase.EventuateInMemoryDataSourceBuilder; import javax.annotation.Priority; -import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Alternative; import javax.enterprise.inject.Instance; -import javax.enterprise.inject.Produces; +import javax.inject.Singleton; import javax.sql.DataSource; import java.util.stream.Collectors; -@ApplicationScoped +@Singleton @Alternative @Priority(0) public class EventuateCommonInMemoryDatabaseConfiguration { - @Produces + @Singleton public DataSource dataSource(Instance scripts) { return new EventuateInMemoryDataSourceBuilder(scripts.stream().collect(Collectors.toList())).build(); } diff --git a/eventuate-common-quarkus-in-memory-database/src/test/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseTest.java b/eventuate-common-quarkus-in-memory-database/src/test/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseTest.java index adfd150..ef096a3 100755 --- a/eventuate-common-quarkus-in-memory-database/src/test/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseTest.java +++ b/eventuate-common-quarkus-in-memory-database/src/test/java/io/eventuate/common/quarkus/inmemorydatabase/EventuateCommonInMemoryDatabaseTest.java @@ -4,7 +4,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import javax.enterprise.inject.Instance; import javax.inject.Inject; import javax.sql.DataSource; @@ -12,10 +11,10 @@ public class EventuateCommonInMemoryDatabaseTest { @Inject - Instance dataSource; + DataSource dataSource; @Test public void testThatInMemoryDataSourceIsUsed() { - Assertions.assertTrue(dataSource.get().getClass().getName().contains("EmbeddedDataSource")); + Assertions.assertTrue(dataSource.getClass().getName().contains("EmbeddedDataSource")); } } diff --git a/eventuate-common-quarkus-jdbc-test-configuration/src/main/java/io/eventuate/common/quarkus/jdbc/test/configuration/DataSourceSelectorConfiguration.java b/eventuate-common-quarkus-jdbc-test-configuration/src/main/java/io/eventuate/common/quarkus/jdbc/test/configuration/DataSourceSelectorConfiguration.java index bcb0ce2..ad3b280 100755 --- a/eventuate-common-quarkus-jdbc-test-configuration/src/main/java/io/eventuate/common/quarkus/jdbc/test/configuration/DataSourceSelectorConfiguration.java +++ b/eventuate-common-quarkus-jdbc-test-configuration/src/main/java/io/eventuate/common/quarkus/jdbc/test/configuration/DataSourceSelectorConfiguration.java @@ -3,19 +3,18 @@ import org.eclipse.microprofile.config.inject.ConfigProperty; import javax.annotation.Priority; -import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Alternative; import javax.enterprise.inject.Instance; -import javax.enterprise.inject.Produces; import javax.inject.Named; +import javax.inject.Singleton; import javax.sql.DataSource; -@ApplicationScoped +@Singleton @Alternative @Priority(1) public class DataSourceSelectorConfiguration { - @Produces + @Singleton public DataSource defaultDatasource(@ConfigProperty(name = "eventuateDatabase") String db, @Named("mysql") Instance mysql, @Named("postgres") Instance postgres, diff --git a/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/EventuateCommonJdbcOperationsConfiguration.java b/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/EventuateCommonJdbcOperationsConfiguration.java index 5c7dd5a..2b02993 100755 --- a/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/EventuateCommonJdbcOperationsConfiguration.java +++ b/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/EventuateCommonJdbcOperationsConfiguration.java @@ -6,29 +6,25 @@ import io.eventuate.common.jdbc.EventuateSqlException; import io.eventuate.common.jdbc.EventuateTransactionTemplate; import io.eventuate.common.jdbc.sqldialect.SqlDialectSelector; -import io.quarkus.arc.DefaultBean; import org.eclipse.microprofile.config.inject.ConfigProperty; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Any; import javax.enterprise.inject.Instance; -import javax.enterprise.inject.Produces; -import javax.inject.Named; +import javax.inject.Singleton; import javax.sql.DataSource; import java.sql.SQLException; import java.util.Optional; -@ApplicationScoped +@Singleton public class EventuateCommonJdbcOperationsConfiguration { - @Produces + @Singleton public EventuateCommonJdbcOperations eventuateCommonJdbcOperations(EventuateJdbcStatementExecutor eventuateJdbcStatementExecutor, SqlDialectSelector sqlDialectSelector, @ConfigProperty(name = "eventuateDatabase") String dbName) { return new EventuateCommonJdbcOperations(eventuateJdbcStatementExecutor, sqlDialectSelector.getDialect(dbName, Optional.empty())); } - @Produces + @Singleton public EventuateJdbcStatementExecutor eventuateJdbcStatementExecutor(Instance dataSource) { return new EventuateCommonJdbcStatementExecutor(() -> { try { @@ -39,7 +35,7 @@ public EventuateJdbcStatementExecutor eventuateJdbcStatementExecutor(Instance eventuateDatabaseSchema) { return new EventuateSchema(eventuateDatabaseSchema.orElse(null)); } diff --git a/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/sqldialect/SqlDialectConfiguration.java b/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/sqldialect/SqlDialectConfiguration.java index fb8ad7a..43845b1 100755 --- a/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/sqldialect/SqlDialectConfiguration.java +++ b/eventuate-common-quarkus-jdbc/src/main/java/io/eventuate/common/quarkus/jdbc/sqldialect/SqlDialectConfiguration.java @@ -8,37 +8,36 @@ import io.eventuate.common.jdbc.sqldialect.SqlDialectSelector; import org.eclipse.microprofile.config.inject.ConfigProperty; -import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Instance; -import javax.enterprise.inject.Produces; +import javax.inject.Singleton; import java.util.Optional; import java.util.stream.Collectors; -@ApplicationScoped +@Singleton public class SqlDialectConfiguration { - @Produces + @Singleton public MySqlDialect mySqlDialect() { return new MySqlDialect(); } - @Produces + @Singleton public PostgresDialect postgreSQLDialect() { return new PostgresDialect(); } - @Produces + @Singleton public MsSqlDialect msSqlDialect() { return new MsSqlDialect(); } - @Produces + @Singleton public DefaultEventuateSqlDialect defaultSqlDialect(@ConfigProperty(name = "eventuate.current.time.in.milliseconds.sql") Optional customCurrentTimeInMillisecondsExpression) { return new DefaultEventuateSqlDialect(customCurrentTimeInMillisecondsExpression.orElse(null)); } - @Produces + @Singleton public SqlDialectSelector sqlDialectSelector(Instance dialects) { return new SqlDialectSelector(dialects.stream().collect(Collectors.toList())); }