Custom ServiceContributor not being used

Hi,

So the automatic prefetching tool that I’ve been working on has soon been migrated to 5.3.0.Final. There is a problem still though, and it has to do with the custom service that the tool relies on. For some reason it does not get instantiated, causing an UnknownServiceException in my test suite. We are injecting the service with a ServiceContributor, which should, in theory, take care of all the instantiation of the service. However, this does not occur. When I debug the contributor, the breakpoint is never triggered. Is there something I need to do in order to make this happen? I checked in the Integrations guide and it seems like this should be the way to automatically inject a service.

This is the code of the service, contributor, etc:

@SuppressWarnings("unused")
@AutoService(ServiceContributor.class)
public class AutofetchServiceContributor implements ServiceContributor {

	@Override
	public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
		serviceRegistryBuilder.addInitiator( AutofetchServiceInitiator.INSTANCE );
	}
}

public interface AutofetchService extends Service, Serializable {

    ExtentManager getExtentManager();
}

final class AutofetchServiceImpl implements AutofetchService {

    private final ExtentManager extentManager;

    
    AutofetchServiceImpl() {
        this.extentManager = new ExtentManager();
    }

    @Override
    public ExtentManager getExtentManager() {
        return extentManager;
    }
}

final class AutofetchServiceInitiator implements StandardServiceInitiator<AutofetchService> {

    static final AutofetchServiceInitiator INSTANCE = new AutofetchServiceInitiator();

    @Override
    public AutofetchService initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
        return new AutofetchServiceImpl();
    }

    @Override
    public Class<AutofetchService> getServiceInitiated() {
        return AutofetchService.class;
    }
}

@SuppressWarnings("unused")
@AutoService(Integrator.class)
public class AutofetchIntegrator implements Integrator {

	@Override
	public void integrate(
			Metadata metadata,
			SessionFactoryImplementor sessionFactory,
			SessionFactoryServiceRegistry serviceRegistry) {
		BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
		integrateEventListeners( serviceRegistry );
	}

	@Override
	public void disintegrate(
			SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
	}

	private static void integrateEventListeners(ServiceRegistry serviceRegistry) {
		final ExtentManager extentManager = serviceRegistry.getService( AutofetchService.class ).getExtentManager(); //fails here

		EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
		eventListenerRegistry.setListeners( EventType.LOAD, new AutofetchLoadListener( extentManager ) );

		eventListenerRegistry.setListeners(
				EventType.INIT_COLLECTION,
				new AutofetchInitializeCollectionListener( extentManager )
		);
	}
}

Any ideas what might be the problem? Why is my Contributor never used?

Did you provide the META-INF/service configuration as well?

2 Likes

I entered it incorrectly. It’s solved now, thanks again Vlad. However, I wonder now if I provide this in the repo and let’s say that someone wants to use the tool for their project and they add the jar/updates their maven dependency, will they need to add this file manually to their META-INF or will it just find the service automatically?

It has to be embedded in your jar and then it can be auto-discovered.

1 Like