Skip to content
This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
trevorbernard edited this page Feb 10, 2013 · 5 revisions

jzmq-api

Jzmq-api is a Java ØMQ API for abstracting the various implementations of ZeroMQ Message Transport Protocol. The goal of this project is to create a high level, modern, Java idiomatic API for ØMQ.

Getting Started

Jzmq-api is a Maven project and snapshot artifacts are published to OSS Sonatype and released versions will eventually be released to Central. If you are using Maven, add the following configuration to your pom.xml file.

<dependency>
    <groupId>org.zeromq</groupId>
    <artifactId>jzmq-api</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</dependency>            
<repositories>
  <repository>
    <id>sonatype-nexus-snapshots</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  </repository>
-</repositories>

ØMQ

ØMQ is a high performance, asynchronous, messaging library, which allows you to programmatically create complex networking components, simply over a variety of transports.

Publish-Subscribe Connection

import org.zeromq.ContextFactory;
import org.zeromq.api.Context;
import org.zeromq.api.Socket;
import org.zeromq.api.SocketType;

public class PublishSubscribe {
    public static void main(String[] args) throws Exception {
        Context ctx = ContextFactory.createContext(1);
        Socket publisher = ctx.buildSocket(SocketType.PUB).bind("inproc://publisher");
        Socket subscriber = ctx.buildSocket(SocketType.SUB)
            .asSubscribable().subscribe("H".getBytes()).connect("inproc://publisher");
        publisher.send("Hello".getBytes());
        System.out.println(new String(subscriber.receive()));
        ctx.close();
    }
}

Push-Pull (Pipeline) Connection

import org.zeromq.ContextFactory;
import org.zeromq.api.Context;
import org.zeromq.api.Socket;
import org.zeromq.api.SocketType;

public class PushPull {
    public static void main(String[] args) throws Exception {
        Context ctx = ContextFactory.createContext(1);
        Socket puller = ctx.buildSocket(SocketType.PULL).bind("inproc://pipeline");
        Socket pusher = ctx.buildSocket(SocketType.PUSH).connect("inproc://pipeline");
        pusher.send("PING".getBytes());
        byte[] buf = puller.receive();
        System.out.println(new String(buf));
        ctx.close();
    }
}

Request-Reply Connection

import org.zeromq.ContextFactory;
import org.zeromq.api.Context;
import org.zeromq.api.Socket;
import org.zeromq.api.SocketType;

public class RequestReply {
    public static void main(String[] args) throws Exception {
        Context ctx = ContextFactory.createContext(1);
        Socket rep = ctx.buildSocket(SocketType.REP).bind("inproc://pipeline");
        Socket req = ctx.buildSocket(SocketType.REQ).connect("inproc://pipeline");
        req.send("PING".getBytes());
        byte[] buf = rep.receive();
        System.out.println(new String(buf));
        rep.send("PONG".getBytes());
        buf = req.receive();
        System.out.println(new String(buf));
        ctx.close();
    }
}

Exclusive Pair

Fill me in with cool examples