我现在正在尝试在Quarkus应用程序中配置自定义ConfigSource。像其他许多手册一样,我创建了自己的 DatabaseSourceConfig 并实现了 org.eclipse.microprofile.config.spi.ConfigSource 接口。我在以下位置注册了ConfigSource:
/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource
有我的ConfigSource:
public class DatabaseConfigSource implements ConfigSource {
private DataSource dataSource;
public DatabaseConfigSource() {
try {
dataSource = (DataSource) new InitialContext().lookup("openejb:Resource/config-source-database");
} catch (final NamingException e) {
throw new IllegalStateException(e);
}
}
@Override
public Map<String, String> getProperties() {
// Implementing Method
}
@Override
public String getValue(final String propertyName) {
// Implementing Method
}
@Override
public String getName() {
return DatabaseConfigSource.class.getSimpleName();
}
}
但是由于JNDI名称,这不适用于Quarkus。我需要使用CDI。我正在尝试使用类似这样的东西:
@Inject
@io.quarkus.agroal.DataSource("my_connection")
AgroalDataSource usersDataSource;
并在application.properties中声明此连接,但这对我没有帮助。我一直在获取NULL异常。 也许有人有想法,如何不使用JNDI命名空间就可以在那里建立数据库连接?
答案 0 :(得分:1)
我自己找到了一些答案,也许对其他人也有用。 就像@Janmartiška所说,CDI的启动时间比ConfigSource晚,这就是为什么我看不到任何通过CDI注入连接的方法的原因。 我创建了一些HibernateUtil类:
package org.myproject.config;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.myproject.entities.ConfigurationsEntity;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
try {
Properties props = new Properties();
props.setProperty("hibernate.connection.url", "jdbc:mysql://[db-host]:[db-port]/db_name");
props.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver");
props.setProperty("hibernate.connection.username", "username");
props.setProperty("hibernate.connection.password", "password");
props.setProperty("hibernate.current_session_context_class", "thread");
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
Configuration configuration = new Configuration();
configuration.addProperties(props);
configuration.addAnnotatedClass(ConfigurationsEntity.class);
System.out.println("Hibernate Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
System.out.println("Hibernate serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) sessionFactory = buildSessionFactory();
return sessionFactory;
}
}
比我在SourceConfig中使用它的时间
package org.myproject.config;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.myproject.entities.ConfigurationsEntity;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RegisterForReflection
public class DatabaseSourceConfig implements ConfigSource {
public SessionFactory sessionFactory;
public Session currentSession;
public DatabaseSourceConfig() {
sessionFactory = HibernateUtil.getSessionFactory();
this.checkFactoryConnection();
}
public void checkFactoryConnection() {
if (currentSession == null || (currentSession != null && !currentSession.isOpen())) {
try {
currentSession = sessionFactory.getCurrentSession();
} catch (NullPointerException e) {
currentSession = sessionFactory.openSession();
}
}
}
@Override
public Map<String, String> getProperties() {
// Implementing Method
}
@Override
public String getValue(String propertyName) {
this.checkFactoryConnection();
ConfigurationsEntity conf = new ConfigurationsEntity();
currentSession.beginTransaction();
try {
Query query = currentSession.createNamedQuery("Configuration.selectOne", ConfigurationsEntity.class);
query.setParameter("name", propertyName);
conf = (ConfigurationsEntity) query.getSingleResult();
currentSession.getTransaction().commit();
} catch (Exception ex) {
currentSession.getTransaction().rollback();
}
return conf.getValue();
}
@Override
public String getName() {
return DatabaseSourceConfig.class.getSimpleName();
}
}
现在我可以在其他类中使用ConfigSource了:
@Inject
@ConfigProperty(name = "[property-name-like-in-db]")
public String someProperty;
经过进一步的研究,发现ConfigSource无法访问CDi和application.properties。这就是为什么只剩下以上述方式建立与数据库的连接的原因。 但是,我对示例进行了一些编辑。我从数据库缓存了属性,并创建了一个@ApplicationScoped Bean,它每5分钟对数据库进行一次查询,以查看“ updated_at”属性之一的时间戳是否比Bean加载的属性的时间戳晚。
但是,我不得不说,根据Quarkus和Apache开发人员的说法-这违反了“不变部署”,并且不打算在运行时更改应用程序设置。因此,取决于您是否在应用程序中编写它。
答案 1 :(得分:0)
您可以通过
获取数据源。AgroalDataSource dataSource = Arc.container()
.instance(AgroalDataSource.class, new DataSource.DataSourceLiteral("my_connection"))
.get();
不过,我认为您需要在构造函数之外的其他地方执行此操作,因为ConfigSource实例是在CDI完全启动之前创建的。您可以缓存获取的数据源实例,然后避免多次执行。