Skip to content

AmayaFramework/amaya-jetty

Repository files navigation

amaya-jetty amaya-jetty

The amaya-server implementation is based on jetty-server.

Getting Started

To install it, you will need:

  • Java 11+
  • Maven/Gradle

Features

  • Full implementation of amaya-server
  • Convenient interfaces for http request handling
  • Full access to jetty configuration

Installing

Gradle dependency

dependencies {
    implementation group: 'com.github.romanqed', name: 'amaya-jetty', version: '1.0.0'
}

Maven dependency

<dependency>
    <groupId>io.github.amayaframework</groupId>
    <artifactId>amaya-jetty</artifactId>
    <version>1.0.0</version>
</dependency>

Examples

Hello world

import io.github.amayaframework.jetty.JettyServerFactory;

public class Main {
    public static void main(String[] args) throws Throwable {
        var factory = new JettyServerFactory();
        var server = factory.create();
        server.setHandler(ctx -> {
            var req = ctx.getRequest();
            var rsp = ctx.getResponse();
            rsp.getWriter().write("Hello, " + req.getQueryParameter("user"));
        });
        server.bind(8080);
        server.start();
    }
}

Custom thread pool (virtual threads)

import io.github.amayaframework.jetty.JettyServerFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) throws Throwable {
        var factory = new JettyServerFactory(() -> {
            var ret = new QueuedThreadPool();
            ret.setVirtualThreadsExecutor(Executors.newVirtualThreadPerTaskExecutor());
            return ret;
        });
        var server = factory.create();
        server.setHandler(ctx -> {
            var req = ctx.getRequest();
            var rsp = ctx.getResponse();
            rsp.getWriter().write("Hello, " + req.getQueryParameter("user"));
        });
        server.bind(8080);
        server.start();
    }
}

Custom jetty server instance

import io.github.amayaframework.jetty.JettyFactory;
import io.github.amayaframework.jetty.JettyServerFactory;
import io.github.amayaframework.options.OptionSet;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.session.SessionHandler;

public class Main {
    public static void main(String[] args) throws Throwable {
        var factory = new JettyServerFactory(new MyServerFactory());
        var server = factory.create();
        server.setHandler(ctx -> {
            var req = ctx.getRequest();
            var rsp = ctx.getResponse();
            rsp.getWriter().write("Hello, " + req.getQueryParameter("user"));
        });
        server.bind(8080);
        server.start();
    }
}

final class MyServerFactory implements JettyFactory {

    @Override
    public Server create(Handler handler, OptionSet options) {
        // Just ignore options for example
        return create(handler);
    }

    @Override
    public Server create(Handler handler) {
        var ret = new Server();
        var sessionHandler = new SessionHandler();
        sessionHandler.setHandler(handler);
        ret.setHandler(sessionHandler);
        return ret;
    }
}

Built With

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

About

The amaya-server implementation is based on jetty-server.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages