All Projects → udaychandra → susel

udaychandra / susel

Licence: Apache-2.0 license
Super charge the module aware service loader in Java 11

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to susel

java11-examples
java 11 example code
Stars: ✭ 90 (+328.57%)
Mutual labels:  java11, jdk11
JavaCertification
This is a full resource guide for my attempt to get Java 11 Certified
Stars: ✭ 67 (+219.05%)
Mutual labels:  jpms, java11
realworld-springboot-java
ReadWorld.io project using spring boot
Stars: ✭ 74 (+252.38%)
Mutual labels:  java11
EZY-server
[EZY | 서버] 우리는 라이프스타일 혁신의 새로운 역사를 쓰고 있습니다.
Stars: ✭ 12 (-42.86%)
Mutual labels:  java11
demo-jigsaw-advent-calendar
An Advent Calendar Demonstrating Jigsaw EA
Stars: ✭ 16 (-23.81%)
Mutual labels:  jpms
Light 4j
A fast, lightweight and more productive microservices framework
Stars: ✭ 3,303 (+15628.57%)
Mutual labels:  java11
Origami
Bukkit/Spigot/Paper based Minecraft server used by Minebench.de | Looking for an 1.17 version? If so most patches are PRd into Paper now, Origami 1.17 will continue once patches that Paper wont accept are necessary.
Stars: ✭ 29 (+38.1%)
Mutual labels:  java11
Keyist-Ecommerce
🔑 A simple ecommerce site powered with Spring Boot + Angular 10 + Ngrx + OAuth2
Stars: ✭ 220 (+947.62%)
Mutual labels:  java11
modern-java-demos
Modern Java: From Java 8 to Java 18. 现代Java案例大全/《Java核心编程》源码
Stars: ✭ 98 (+366.67%)
Mutual labels:  java11
tqrespec
TQRespec - The respec tool for Titan Quest game
Stars: ✭ 59 (+180.95%)
Mutual labels:  jpms
Jdbi
jdbi is designed to provide convenient tabular data access in Java; including templated SQL, parameterized and strongly typed queries, and Streams integration
Stars: ✭ 1,579 (+7419.05%)
Mutual labels:  java11
java9-module-examples
a list of Java 9 module samples to dive into the modular world
Stars: ✭ 25 (+19.05%)
Mutual labels:  serviceloader
http-server
A Java HTTP server in 35MB Docker image
Stars: ✭ 17 (-19.05%)
Mutual labels:  jdk11
kafka-hawk
An application that records stats about consumer group offset commits and reports them as prometheus metrics
Stars: ✭ 13 (-38.1%)
Mutual labels:  java11
atomos
Atomos - A Java Module Framework using OSGi Connect
Stars: ✭ 12 (-42.86%)
Mutual labels:  java11
travels-java-api
An API for travel management. It is built with Java, Spring Boot, and Spring Framework. A toy-project to serve as a theoretical basis for the Medium series of articles I wrote about Java+Spring.
Stars: ✭ 139 (+561.9%)
Mutual labels:  java11
badass-runtime-plugin
Create a custom runtime image of your non-modular application
Stars: ✭ 143 (+580.95%)
Mutual labels:  jpms
java-handles-kata
Java Reflection and Unsafe Alternates - MethodHandle and VarHandle API - Fix broken tests in a Code Kata
Stars: ✭ 15 (-28.57%)
Mutual labels:  java11
os-xtoo
The Java and Enlightenment ebuild repository
Stars: ✭ 17 (-19.05%)
Mutual labels:  java11
sudokufx
AR Sudoku grabber and solver using JavaCV, JavaFX and Scala
Stars: ✭ 64 (+204.76%)
Mutual labels:  jdk11

CircleCI

Susel

Super Service Loader: Super charge the module aware service loader in Java 11. Susel is a light weight library that helps one build Java module system native applications.

Introduction

Service loader mechanism of Java, introduced in Java 6 and revised in Java 9, is used to locate and load services. A service is a well known interface or class (usually abstract). A service provider is a concrete implementation of a service. The ServiceLoader class is a facility to load service providers that implement a given service. A Java native module can declare that it uses a specific service in its module description (module-info.java). The module can then use the ServiceLoader to locate and load the service providers deployed in the run time environment.

Susel builds on this service loading mechanism and provides a few useful features. Susel enables you to specify required or optional service references (through annotations) that a a given service provider might need to operate. Partly inspired by OSGI, Susel provides an "activate" annotation that can be used by a service provider to do some initiation work or to read configuration from a global context map passed to it by Susel.

Basic Usage

If you are using a build tool like Maven or Gradle, add the following dependency to access Susel API:

  • Maven pom.xml

     <dependency>
       <groupId>io.github.udaychandra.susel</groupId>
       <artifactId>susel</artifactId>
       <version>0.1.2</version>
     </dependency>
  • Gradle build.gradle

     dependencies {
       compile 'io.github.udaychandra.susel:susel:0.1.2'
     }

Note that Susel requires Java 11

Begin by defining a service interface.

package com.example.svc;

public interface HelloService {
    String hello(String id);
}

Create a service provider by implementing the service interface.

package com.example.svc.name;

public class HelloNameService implements HelloService {
    private UserService userService;
    
    @ServiceReference(cardinality = Cardinality.ONE)
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public String hello(String id) {
        return "Hello " + userService.getName(id);
    }
}

Update the module descriptor (module-info.java)

module com.example.svc.name {
    exports com.example.svc.name;

    requires com.example.svc;

    provides com.example.svc.HelloService 
        with com.example.svc.name.HelloNameService;
}

Now, a consuming client of the service interface can delegate the lookup, preparation and loading to Susel. Say the module descriptor of the client looks something like this:

module com.example.client {
    exports com.example.client;

    requires com.example.svc;
    requires io.github.udaychandra.susel;

    uses com.example.svc.HelloService;
}

The client can then activate Susel once and start loading services. For example, to load the HelloService one can do this in the application's main method:

Susel.activate(Map.of());
var helloService = Susel.get(HelloService.class);

Susel will take care of loading a service provider that implements the hello service. When it finds the HelloNameService, it will ensure that at least one service provider that implements the required UserService reference is found and injected, activates HelloNameService and returns the now ready to use HelloNameService.

Susel relies on the presence of a metadata file (META-INF/susel.metadata) in a module to figure out the service references used by a service provider, their cardinalities and the method that should be invoked to activate the service provider. This metadata file should be generated during compile time.

There's a gradle plugin that can automate the generation and packaging of these metadata files. The plugin calls the Susel tool which can be manually invoked as well.

A maven plugin is in the works

Development

This is a community project. All contributions are welcome.

To start contributing, do the following:

  • Install JDK 11
  • Fork or clone the source code
  • Run the build using the gradle wrapper
gradlew clean build

License

Apache License 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].