简体   繁体   中英

How to make a Java project 'extendable' by developers?

I'm working on a server engine, and I am not sure in what form to distribute it. It is quite modular and it uses interfaces/abstract classes.

Should it be:

  • a library (no entry-point, write your own main() and call new Server().setSomeHandler(myHandler).run() )

  • a binary (executable entrypoint with config file where you can inject JAR with handlers)

  • something else?

Basically, the developer should be able to completely extend or change the way the server works. I don't like the idea of making it a library because it should be a platform by itself, a whole server system.

Providing a programmatic way is more versatile than just an executable. And both ways aren't mutually exclusive. After all, even if a developer provides a handler, internally you probably still need to call something like the setter in your example to actually use that handler. Exposing that API shouldn't be too difficult. You could still provide a small launcher application that loads some config file and wraps it in some API calls, if that is needed.

The more important question would be, are there predefined extension points where developers can plug in their own implementations or is everything completely modular and exchangeable?

For a simple way to provide implementations of predefined interfaces, you can use the ServiceLoader/SPI mechanism. You can built a basic plug-in system with it.

If you want to create a platform, something like OSGi seems more appropriate. Here you could define APIs/SPIs for fine grained components/services. Developers could then provide their own modules that extend the server or even replace your default modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM