Git Product home page Git Product logo

firefly's Introduction

What is Firefly?

Build Status Maven Central License

Firefly framework is an asynchronous Java web framework. It helps you create a web application Easy and Quickly. It provides MVC framework, asynchronous HTTP Server/Client, asynchronous TCP Server/Client and many other useful components for developing web applications, protocol servers, etc. That means you can easy deploy your web without any other java web containers, in short, it's containerless. It taps into the fullest potential of hardware using SEDA architecture, a highly customizable thread model.

Firefly core provides functionality for things like:

  • Writing TCP clients and servers
  • Writing HTTP clients and servers
  • Writing WebSocket clients and servers
  • Writing web application with MVC framework and template engine
  • Database access

Event driven

The Firefly APIs are largely event-driven. It means that when things happen in Firefly that you are interested in, Firefly will call you by sending you events.

Some example events are:

  • some data has arrived on a socket
  • an HTTP server has received a request

Firefly handles a lot of concurrencies using just a small number of threads, so don't block Firefly thread, you must manage blocking call in the standalone thread pool.

With a conventional blocking API the calling thread might block when:

  • Thread.sleep()
  • Waiting on a Lock
  • Waiting on a mutex or monitor
  • Doing a long-lived database operation and waiting for a result
  • Call blocking I/O APIs

In all the above cases, when your thread is waiting for a result it can’t do anything else - it’s effectively useless.

It means that if you want a lot of concurrencies using blocking APIs, then you need a lot of threads to prevent your application grinding to a halt.

Threads have overhead regarding the memory they require (e.g. for their stack) and in context switching.

For the levels of concurrency required in many modern applications, a blocking approach just doesn’t scale.

Quick start

Add maven dependency in your pom.xml.

<dependency>
    <groupId>com.fireflysource</groupId>
    <artifactId>firefly</artifactId>
    <version>4.9.5</version>
</dependency>

<dependency>
    <groupId>com.fireflysource</groupId>
    <artifactId>firefly-slf4j</artifactId>
    <version>4.9.5</version>
</dependency>

Add log configuration file "firefly-log.xml" to the classpath.

<?xml version="1.0" encoding="UTF-8"?>
<loggers xmlns="http://www.fireflysource.com/loggers"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.fireflysource.com/loggers http://www.fireflysource.com/loggers.xsd">
    <logger>
        <name>firefly-system</name>
        <level>INFO</level>
        <path>${log.path}</path>
    </logger>

    <logger>
        <name>firefly-monitor</name>
        <level>INFO</level>
        <path>${log.path}</path>
    </logger>
</loggers>

Create a HTTP server (Java version)

public class HelloHTTPServer {
    public static void main(String[] args) {
        $.httpServer()
         .router().get("/").handler(ctx -> ctx.end("hello world!"))
         .listen("localhost", 8080);
    }
}

Create a HTTP client (Java version)

public class HelloHTTPClient {
    public static void main(String[] args) {
        $.httpClient().get("http://localhost:8080/").submit()
         .thenAccept(res -> System.out.println(res.getStringBody()));
    }
}

Create WebSocket server and client (Java version)

public static void main(String[] args) {
    SimpleWebSocketServer server = $.createWebSocketServer();
    server.webSocket("/helloWebSocket")
          .onConnect(conn -> conn.sendText("OK."))
          .onText((text, conn) -> System.out.println("The server received: " + text))
          .listen("localhost", 8080);

    SimpleWebSocketClient client = $.createWebSocketClient();
    client.webSocket("ws://localhost:8080/helloWebSocket")
          .onText((text, conn) -> System.out.println("The client received: " + text))
          .connect()
          .thenAccept(conn -> conn.sendText("Hello server."));
}

Firefly also supports to create HTTP server/client using Kotlin DSL.

Add maven dependency in your pom.xml

<dependency>
    <groupId>com.fireflysource</groupId>
    <artifactId>firefly-kotlin-ext</artifactId>
    <version>4.9.5</version>
</dependency>

Create a HTTP server (Kotlin DSL version)

fun main(args: Array<String>) {
    HttpServer {
        router {
            httpMethod = HttpMethod.GET
            path = "/"

            asyncHandler {
                end("hello world!")
            }
        }
    }.listen("localhost", 8080)
}

Create a HTTP client (Kotlin coroutine asynchronous client)

fun main(args: Array<String>) = runBlocking {
    val msg = firefly.httpClient().get("http://localhost:8080").asyncSubmit().stringBody
    println(msg)
}

Create WebSocket server and client (Kotlin version)

fun main(args: Array<String>) {
    val server = firefly.createWebSocketServer()
    server.webSocket("/helloWebSocket")
          .onConnect { conn -> conn.sendText("OK.") }
          .onText { text, conn -> println("The server received: " + text) }
          .listen("localhost", 8080)

    val client = firefly.createWebSocketClient()
    client.webSocket("ws://localhost:8080/helloWebSocket")
          .onText { text, conn -> println("The client received: " + text) }
          .connect()
          .thenAccept { conn -> conn.sendText("Hello server.") }
}

More detailed information, please refer to the

Contact information

E-mail: [email protected]
QQ Group: 126079579

Treat author to a cup of coffee:

Get the red envelope:

firefly's People

Contributors

hypercube1024 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.