Services are classes that provide a pluggable implementation of various functionality, in particular, they define specific functionality and then implement those service agreement interfaces. The interface refers to as the role of service, this implementation class is known as a service implementation.
All Services in Hibernate are expected to implement org.hibernate.service.Service interface, which is a marker interface (interface with no methods), Hibernate uses this internally for safety purpose. Optionally services also implement org.hibernate.service.spi.Startable and org.hibernate.service.spi.Stoppable interfaces to receive the notifications being start, and stopped. org.hibernate.service.spi.Manageable marks the service as manageable in JMX delivered the JMX integration is allowed.
For example, org.hibernate.engine.jdbc.connections.spi.ConnectionProvider is one of the Hibernate Service, there will be multiple implementations of the service, and this Hibernate Service provides functionality to get the connection, closing the connection.
Hibernate always references the service (like org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) rather than implementations in consuming the service functionality, because of this fact ConnectionProvider implementations could be plugged in.
ServiceRegistry is the class where we are providing the service class implementations to the specified service. ServiceRegistry is the class which manages all Services, service scope, and other service dependencies.
ServiceRegistries are hierarchical, a serviceRegistry may have parent serviceRegistry. Services in one registry can consume services from same registry, or from parent registries.
Service Binding: Service can be associated with serviceRegistry using org.hibernate.service.spi.ServiceBinding interface. The particular convention between a ServiceBinding and the ServiceRegistry signifies by the org.hibernate.service.spi.ServiceBinding.ServiceLifecycleOwner interface.
There are two ways to assistant a Facility with a ServiceRegistry.
- Service can be directly instantiated and then pass to serviceRegistry, or ServiceInitiator can be pass to serviceRegistry.
- ServiceRegistry implementations (those using the org.hibernate.service.internal.AbstractServiceRegistryImpl convenience base implementation) register bindings through calls to the overloaded AbstractServiceRegistryImpl#createServiceBinding method accepting either a Service instance or a ServiceInitiator instance.
Services can declare other service dependencies using two approaches.
@org. hibernate.service.spi.InjectService: Any method on the service implementation class accepting a single parameter and annotated with @InjectService is considered requesting injection of another service.
By default, injected services consider essential, if dependent service is not available they will throw an error. If the dependent service is optional then mark this as optional using the required attribute to false.
org.hibernate.service.spi.ServiceRegistryAwareService: If the service implement org.hibernate.service.spi.ServiceRegistryAwareService interface, which has the injectServices method.
During the startup, Hibernate will inject org.hibernate.service.ServiceRegistry instance to service implementation, and it can use to locate the other available services.
Developing Custom Services:
Here, we are going to create EventPublishService, manage this service using serviceRegistry. Our application used to send Hibernate events to JMS Topic.
Now let`s create a implementation of the above service using 2nd approach using ServiceRegistryAwareService.
Now let`s create service initiator used to create service instance (as a service binding).
And we can pass this serviceInitiator to serviceRegistry to manage it.
In this article, Java Web Application Development Services Company explains a way to integrate this service to serviceRegistry and how to specify other service dependencies in multiple ways.