Looking for an Expert Development Team? Take two weeks Trial! Try Now

How to implement Custom Service in Hibernate Framework?


Technology:  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.

  1. Service can be directly instantiated and then pass to serviceRegistry, or ServiceInitiator can be pass to serviceRegistry.
  2. 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.

Service Dependencies:

Services can declare other service dependencies using two approaches.

  1. @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.

  2. 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.

public interface EventPublishingService extends Service { public void publish(Event theEvent); }

Now let`s create a implementation of the above service using 2nd approach using ServiceRegistryAwareService.

public class EventPublishingServiceImpl implements EventPublishingService, Configurable, Startable, Stoppable, ServiceRegistryAwareService { private ServiceRegistryImplementor serviceRegistry; private String jmsConnectionFactoryName; private String destinationName; private Connection jmsConnection; private Session jmsSession; private MessageProducer publisher; @Override public void injectServices(ServiceRegistryImplementor serviceRegistry) { this.serviceRegistry = serviceRegistry; } public void configure(Map configurationValues) { this.jmsConnectionFactoryName = configurationValues.get( JMS_CONNECTION_FACTORY_NAME_SETTING ); this.destinationName = configurationValues.get( JMS_DESTINATION_NAME_SETTING ); } @Override public void start() { final JndiService jndiService = serviceRegistry.getService( JndiService.class ); final ConnectionFactory jmsConnectionFactory = jndiService.locate( jmsConnectionFactoryName ); this.jmsConnection = jmsConnectionFactory.createConnection(); this.jmsSession = jmsConnection.createSession( true, Session.AUTO_ACKNOWLEDGE ); final Destination destination = jndiService.locate( destinationName ); this.publisher = jmsSession.createProducer( destination ); } @Override public void publish(Event theEvent) { publisher.send( theEvent ); } @Override public void stop() { publisher.close(); jmsSession.close(); jmsConnection.close(); } }

Now let`s create service initiator used to create service instance (as a service binding).

public class EventPublishingServiceInitiator implements StandardServiceInitiator<EventPublishingService> { public static EventPublishingServiceInitiator INSTANCE = new EventPublishingServiceInitiator(); public Class<EventPublishingService> getServiceInitiated() { return EventPublishingService.class; } @Override public EventPublishingService initiateService(Map configurationValues, ServiceRegistryImplementor registry) { return new EventPublishingServiceImpl(); } }

And we can pass this serviceInitiator to serviceRegistry to manage it.

StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .addInitiator( EventPublishingServiceInitiator.INSTANCE ) .build();


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.

Read More:

DMCA Logo do not copy