Skip to content

Commit 9b759be

Browse files
gavinkingSanne
authored andcommitted
introduce new vertx package
moved @Sanne's Vertx instance management to a new package defined as an extension point
1 parent eb5b63b commit 9b759be

File tree

11 files changed

+122
-87
lines changed

11 files changed

+122
-87
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/boot/impl/ReactiveServiceRegistryInitializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.hibernate.reactive.service.initiator.ReactiveJdbcEnvironmentInitiator;
88
import org.hibernate.reactive.service.initiator.ReactiveQueryTranslatorFactoryInitiator;
99
import org.hibernate.reactive.service.initiator.ReactiveTransactionCoordinatorBuilderInitiator;
10-
import org.hibernate.reactive.service.initiator.VertxInitiator;
10+
import org.hibernate.reactive.vertx.impl.VertxInstanceInitiator;
1111
import org.hibernate.service.spi.ServiceContributor;
1212

1313
/**
@@ -19,7 +19,7 @@ public class ReactiveServiceRegistryInitializer implements ServiceContributor {
1919

2020
@Override
2121
public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
22-
serviceRegistryBuilder.addInitiator( VertxInitiator.INSTANCE );
22+
serviceRegistryBuilder.addInitiator( VertxInstanceInitiator.INSTANCE );
2323
serviceRegistryBuilder.addInitiator( DummyConnectionProviderInitiator.INSTANCE );
2424
serviceRegistryBuilder.addInitiator( ReactiveJdbcEnvironmentInitiator.INSTANCE );
2525
serviceRegistryBuilder.addInitiator( ReactiveTransactionCoordinatorBuilderInitiator.INSTANCE );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/ReactiveConnectionPool.java

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
* {@link org.hibernate.boot.registry.StandardServiceInitiator}
1515
* or from code-based Hibernate configuration by calling
1616
* {@link org.hibernate.boot.registry.StandardServiceRegistryBuilder#addService}.
17+
*
18+
* <pre>
19+
* new StandardServiceRegistryBuilder()
20+
* .applySettings( properties )
21+
* .addService( ReactiveConnectionPool.class, new MyReactiveConnectionPool() )
22+
* .build();
23+
* </pre>
1724
*/
1825
public interface ReactiveConnectionPool extends Service {
1926

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientPool.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.hibernate.reactive.cfg.ReactiveSettings;
1313
import org.hibernate.reactive.pool.ReactiveConnection;
1414
import org.hibernate.reactive.pool.ReactiveConnectionPool;
15-
import org.hibernate.reactive.service.VertxService;
15+
import org.hibernate.reactive.vertx.VertxInstance;
1616
import org.hibernate.reactive.util.impl.JdbcUrlParser;
1717
import org.hibernate.service.spi.Configurable;
1818
import org.hibernate.service.spi.ServiceRegistryAwareService;
@@ -31,6 +31,8 @@
3131

3232
/**
3333
* A pool of reactive connections backed by a Vert.x {@link Pool}.
34+
* The {@code Pool} itself is backed by an instance of {@link Vertx}
35+
* obtained via the {@link VertxInstance} service.
3436
*/
3537
public class SqlClientPool implements ReactiveConnectionPool, ServiceRegistryAwareService, Configurable, Stoppable, Startable {
3638

@@ -66,8 +68,7 @@ public void start() {
6668
pool = (Pool) o;
6769
}
6870
} else {
69-
final VertxService vertxService = serviceRegistry.getService( VertxService.class );
70-
Vertx vertx = vertxService.getVertx();
71+
Vertx vertx = serviceRegistry.getService( VertxInstance.class ).getVertx();
7172
pool = configurePool( configurationValues, vertx );
7273
}
7374

hibernate-reactive-core/src/main/java/org/hibernate/reactive/service/ExternallyProvidedVertxService.java

-28
This file was deleted.

hibernate-reactive-core/src/main/java/org/hibernate/reactive/service/VertxService.java

-16
This file was deleted.

hibernate-reactive-core/src/main/java/org/hibernate/reactive/service/initiator/VertxInitiator.java

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.hibernate.reactive.vertx;
2+
3+
import io.vertx.core.Vertx;
4+
import org.hibernate.service.Service;
5+
6+
/**
7+
* Used by {@link org.hibernate.reactive.pool.impl.SqlClientPool}
8+
* to obtain an instance of {@link Vertx}. The default instance is
9+
* {@link org.hibernate.reactive.vertx.impl.DefaultVertxInstance}.
10+
*
11+
* A program may integrate a custom {@link VertxInstance}
12+
* with Hibernate Reactive by contributing a new service using a
13+
* {@link org.hibernate.boot.registry.StandardServiceInitiator}
14+
* or from code-based Hibernate configuration by calling
15+
* {@link org.hibernate.boot.registry.StandardServiceRegistryBuilder#addService}.
16+
*
17+
* <pre>
18+
* new StandardServiceRegistryBuilder()
19+
* .applySettings( properties )
20+
* .addService( VertxInstance.class, () -> myVertx )
21+
* .build();
22+
* </pre>
23+
*
24+
* @see org.hibernate.reactive.vertx.impl.ProvidedVertxInstance
25+
*/
26+
@FunctionalInterface
27+
public interface VertxInstance extends Service {
28+
29+
/**
30+
* Obtain the instance of {@link Vertx}.
31+
*/
32+
Vertx getVertx();
33+
34+
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/service/SelfmanagingVertxService.java renamed to hibernate-reactive-core/src/main/java/org/hibernate/reactive/vertx/impl/DefaultVertxInstance.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
package org.hibernate.reactive.service;
1+
package org.hibernate.reactive.vertx.impl;
22

33
import io.vertx.core.Vertx;
4+
import org.hibernate.reactive.vertx.VertxInstance;
45
import org.hibernate.service.spi.Startable;
56
import org.hibernate.service.spi.Stoppable;
67

78
/**
8-
* This will start a new Vertx instance on demand, then ensure it's closed when
9-
* the SessionFactory is closed.
10-
* To use an external Vertx instance you can inject an alternative implementation
11-
* in the bootstrap registry.
9+
* A singleton instance of {@link Vertx} that is created on
10+
* demand and destroyed automatically along with the Hibernate
11+
* {@link org.hibernate.SessionFactory#close() session factory}.
12+
*
13+
* @see ProvidedVertxInstance if you need to a different instance
1214
*
1315
* @author Sanne Grinovero <[email protected]>
1416
*/
15-
public final class SelfmanagingVertxService implements VertxService, Stoppable, Startable {
17+
public final class DefaultVertxInstance implements VertxInstance, Stoppable, Startable {
1618

1719
private Vertx vertx;
1820

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hibernate.reactive.vertx.impl;
2+
3+
import io.vertx.core.Vertx;
4+
import org.hibernate.reactive.vertx.VertxInstance;
5+
6+
import java.util.Objects;
7+
8+
/**
9+
* An implementation of {@link VertxInstance} which allows the client
10+
* to provide an instance of {@link Vertx} whose lifecyle is managed
11+
* extrenally to Hibernate Reactive. The {@code ProvidedVertxInstance}
12+
* must be registered with explicitly Hibernate by calling
13+
* {@link org.hibernate.boot.registry.StandardServiceRegistryBuilder#addService}.
14+
* Hibernate will destroy the {@code Vertx} instance on shutdown.
15+
*
16+
* <pre>
17+
* new StandardServiceRegistryBuilder()
18+
* .applySettings( properties )
19+
* .addService( VertxInstance.class, new ProvidedVertxInstance( vertx ) )
20+
* .build();
21+
* </pre>
22+
*/
23+
public final class ProvidedVertxInstance implements VertxInstance {
24+
25+
private final Vertx vertx;
26+
27+
public ProvidedVertxInstance(Vertx vertx) {
28+
Objects.requireNonNull( vertx );
29+
this.vertx = vertx;
30+
}
31+
32+
@Override
33+
public Vertx getVertx() {
34+
return vertx;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.hibernate.reactive.vertx.impl;
2+
3+
import org.hibernate.boot.registry.StandardServiceInitiator;
4+
import org.hibernate.reactive.vertx.VertxInstance;
5+
import org.hibernate.service.spi.ServiceRegistryImplementor;
6+
7+
import java.util.Map;
8+
9+
/**
10+
* Factory for the default implementation of {@link VertxInstance}.
11+
*/
12+
public final class VertxInstanceInitiator implements StandardServiceInitiator<VertxInstance> {
13+
14+
public static final VertxInstanceInitiator INSTANCE = new VertxInstanceInitiator();
15+
16+
@Override
17+
public VertxInstance initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
18+
return new DefaultVertxInstance();
19+
}
20+
21+
@Override
22+
public Class<VertxInstance> getServiceInitiated() {
23+
return VertxInstance.class;
24+
}
25+
26+
}

hibernate-reactive-core/src/test/java/org/hibernate/reactive/testing/TestingRegistryRule.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.hibernate.reactive.testing;
22

33
import io.vertx.core.Vertx;
4-
import org.hibernate.reactive.service.ExternallyProvidedVertxService;
5-
import org.hibernate.reactive.service.VertxService;
4+
import org.hibernate.reactive.vertx.VertxInstance;
5+
import org.hibernate.reactive.vertx.impl.ProvidedVertxInstance;
66
import org.hibernate.service.Service;
77
import org.hibernate.service.ServiceRegistry;
88
import org.hibernate.service.spi.ServiceBinding;
@@ -35,10 +35,10 @@ public ServiceRegistryImplementor getServiceRegistry() {
3535
//TODO extract into its own class and evolve as necessary?
3636
private static class Registry implements ServiceRegistryImplementor {
3737

38-
private final ExternallyProvidedVertxService vertxService;
38+
private final ProvidedVertxInstance vertxService;
3939

4040
public Registry(Vertx vertx) {
41-
this.vertxService = new ExternallyProvidedVertxService( vertx );
41+
this.vertxService = new ProvidedVertxInstance( vertx );
4242
}
4343

4444
@Override
@@ -68,7 +68,7 @@ public ServiceRegistry getParentServiceRegistry() {
6868

6969
@Override
7070
public <R extends Service> R getService(Class<R> serviceRole) {
71-
if ( serviceRole == VertxService.class )
71+
if ( serviceRole == VertxInstance.class )
7272
return (R) vertxService;
7373
else {
7474
throw new IllegalArgumentException( "This is a mock service - need to explicitly handle any service we might need during testing" );

0 commit comments

Comments
 (0)