This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Service provider module with public static provider() method

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi =)

I converted the repo below to use maven:
https://github.com/java9-modularity/java9-vertx

The project has one provider that does NOT implement or extends anything.
It's like this:



And he uses it like this:

But then I get the error:

Exception in thread "main" java.util.ServiceConfigurationError: javamodularity.easytext.pagefetch.WikipediaFetcher: Provider javamodularity.easytext.pagefetch.impl.WikipediaFetchImpl could not be instantiated
       at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:581)
       at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:803)
       at java.base/java.util.ServiceLoader$ProviderImpl.get(ServiceLoader.java:721)
       at java.base/java.util.ServiceLoader$3.next(ServiceLoader.java:1394)
       at java.base/java.util.ServiceLoader.findFirst(ServiceLoader.java:1809)
       at javamodularity.easytext.web.Main.main(Main.java:16)
Caused by: java.util.ServiceConfigurationError: io.vertx.reactivex.core.Vertx: javamodularity.easytext.vertx.VertxProvider not a subtype
       at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:588)
       at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1236)
       at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264)
       at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1299)
       at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1384)
       at java.base/java.util.ServiceLoader.findFirst(ServiceLoader.java:1808)
       at javamodularity.easytext.pagefetch.impl.WikipediaFetchImpl.<init>(WikipediaFetchImpl.java:16)
       at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
       at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
       at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
       at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:779)
       ... 4 more

Does this pattern really work? Or Service Provider Interfaces (SPI) only work if we implement some interface?

I also found a little strange that the author did that, because io.vertx.reactivex.core.Vertx is a concrete class.
But I understand that he wanted to only have one instance of that class in the application.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static method provider works.
There is a nice example here:
https://www.logicbig.com/tutorials/core-java-tutorial/modules/service-provider-method.html

Also from the java docs:

If the service provider declares a provider method, then the service loader invokes that method to obtain an instance of the service provider. A provider method is a public static method named "provider" with no formal parameters and a return type that is assignable to the service's interface or class.
In this case, the service provider itself need not be assignable to the service's interface or class.



So there's something wrong in my code ...
 
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message:

Caused by: java.util.ServiceConfigurationError: io.vertx.reactivex.core.Vertx: javamodularity.easytext.vertx.VertxProvider not a subtype


seems to say that javamodularity.easytext.vertx.VertxProvider is not a subtype of io.vertx.reactivex.core.Vertx. Which is true, your code doesn't declare it to implement that interface. And if I understand it correctly, the tutorial which you linked to does include that interface implementation:

 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: And if I understand it correctly, the tutorial which you linked to does include that interface implementation:



Thanks Paul.

I tried something similar but got the same error: "not a subtype":



I will later investigate it further but I already did a small test and SPI works for concrete classes too, eg:


 
reply
    Bookmark Topic Watch Topic
  • New Topic