001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.runtime.pubsub;
020
021import java.util.function.BiConsumer;
022
023/**
024 * Publish/Subscribe Service.
025 * <p>
026 * This service allows cross-instance notifications through simple messages sent to topics.
027 *
028 * @since 9.1
029 */
030public interface PubSubService {
031
032    /**
033     * Publishes a message to the given topic.
034     *
035     * @param topic the topic
036     * @param message the message
037     */
038    void publish(String topic, byte[] message);
039
040    /**
041     * Registers a subscriber for the given topic.
042     * <p>
043     * The subscriber must deal with the message without delay and return immediately, usually by storing it in a
044     * thread-safe datastructure.
045     *
046     * @param topic the topic
047     * @param subscriber the subscriber, who will receive the topic and a {@code byte[]} message
048     */
049    void registerSubscriber(String topic, BiConsumer<String, byte[]> subscriber);
050
051    /**
052     * Unregisters a subscriber for the given topic.
053     *
054     * @param topic the topic
055     * @param subscriber the subscriber
056     */
057    void unregisterSubscriber(String topic, BiConsumer<String, byte[]> subscriber);
058
059}