Giter VIP home page Giter VIP logo

jeromq-toolkit's Introduction

jeromq-toolkit Build Status

Helpers for simplyfied use of jeromq (zeromq for Java)

Maven

<dependency>
  <groupId>org.horsed</groupId>
  <artifactId>jeromq-toolkit</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</dependency>

API

Publisher

Pub pub = new Pub("tcp://127.0.0.1:3000");
pub.send("event", "some event data (e.g. json)");

Asynchronous subscriber

Sub sub = new AsyncSub("tcp://127.0.0.1:3000");   // subscriber that runs in its own thread

sub.on("event", new Handler() {                   // add inline event handler
  @Override public boolean handle(String data) {
    // handle event
  }
});

sub.connect();                                    // always invoke connect :-)

Synchronous subscriber

Sub sub = new SyncSub("tcp://127.0.0.1:3000");    // runs in the thread it is created in and thus blocks this thread

Annotation based event handler

public class MyHandler {
  
  @Subscribe("my-event")
  public void doStuff(String data) {
    // do stuff with event data
  }
  
  @Subscribe(events = {"my-event", "my-other-event"})
  public void doOtherStuff(String data) {
    // do other stuff with event data
  }
}

sub.addHandler(new MyHandler());

Router socket

Router router = new Router("tcp://127.0.0.1:4000") {  // asynchronously handles incoming requests
  @Override public String handle(String request) {
    // handle request
    return "response data (e.g. json)";
  }
};

Request socket

Req req = new Req("tcp://127.0.0.1:4000");         // connect to REP or ROUTER socket

req.onResponse(new ResponseHandler() {
  @Override public void handle(String response) {
    // handle response
  }
});

req.sendRequest("some request data (e.g. json)");  // synchronously waits for the response

Req implements the Lazy-Pirate-Pattern for reliable request/reply messaging: It waits 2500 ms for a response and does 3 retries. After that the request will be aborted with an exception. Req generates an identifier and sends it to the REP/ROUTER socket.

jeromq-toolkit's People

Contributors

martinknopf 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.