NoMQ

Not a Message Queue (but a simple drop-in event queue)

View the Project on GitHub wassgren/NoMQ

NoMQ build status

NoMQ is short for Not a Message Queue. It is a distributed event queue that is based on Java 8 and Hazelcast. NoMQ requires NO installation, simply add the jar to your project.

Prerequisites

Features

Getting started

Triggering an event requires a handle to the NoMQ-instance, a payload that is sent to all subscribers and a name of the event known as the event type.

If you want to subscribe to events in the NoMQ cluster you need to register an EventSubscriber. The following code starts a NoMQ-instance and registers an event subscriber that simply echoes the event id on System.out. Events can be published by any node in the cluster, they will arrive at all event subscribers in the same order.

NoMQ noMQ = NoMQBuilder.builder()
    .subscribe(e -> System.out.println(e.id()))
    .build()
    .start();

// Publish an event asynchronously
noMQ.publishAsync("myEvent", "Some payload".getBytes());

The payload for an event is always a byte array. This may seem like a strict limitation so if you need to dispatch richer objects the solution to this is to use a Converter. The code below converts a String to a byte array.

noMQ.publishAsync("myEvent", "Some payload", str -> str.getBytes());

Subscription of events is done via EventSubscribers. Subscribers are registered during setup and simply implements the method onEvent.

public interface EventSubscriber {
    void onEvent(Event event);
}

The event interface is straightforward, it contains a generated unique id, the event type and the payload provided by the publisher of the event.

public interface Event {
    String id();
    String type();
    byte[] payload();
}

If you want to subscribe to the payload in some other format it is possible to use a Converter for subscriptions as well. The PayloadSubscriber is used together with a Converter.

public interface PayloadSubscriber<T> {
    void onPayload(T payload);
}

To register the PayloadSubscriber and Converter use the following code:

// Register payload subscriber and converter,
// the byte[] is converted to a String
NoMQ noMQ = NoMQBuilder.builder()
    .subscribe("myEvent", str -> System.out.println(str), bytes -> new String(bytes))
    .build()
    .start();

// Publish using a converter
noMQ.publishAsync("myEvent", "A string", str -> str.getBytes());

Configuring the cluster

NoMQ is based on Hazelcast - to set up a cluster follow the instructions on the Hazelcast website (http://hazelcast.org/docs/latest/manual/html-single/hazelcast-documentation.html#network-configuration)

Issues and feature requests

NoMQ uses GitHub Issues for feature requests and issue tracking.

License

Copyright 2014 the original author or authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.