GithubHelp home page GithubHelp logo

rabbitmqevents's Introduction

RabbitMQEvents Documentation

Initialization

To start using RabbitMQEvents, you need initialize the necessary RabbitManager, RabbitEventManager and create an EventBus. Below is an example:

// Initialize RabbitMQ manager
public RabbitManager rabbitManager = new RabbitManager("guest", "guest", "127.0.0.1", 5672);

// Initialize RabbitMQ event manager and event bus
public RabbitEventManager rabbitEventManager;
public RabbitEventBus<RabbitEvent> rabbitEventBus;

public void initialize() {
    this.rabbitManager.onChannelInitialized(() -> {
        // Initialize event manager and event bus when the RabbitMQ channel is ready
        this.rabbitEventManager = new RabbitEventManager("events", this.rabbitManager.getChannel());
        this.rabbitEventBus = EventBus.rabbitEventBus(this.rabbitEventManager, RabbitEvent.class);
    }, 10, 250);
}

Creating Custom Events

Define custom events by implementing the RabbitEvent interface. Here's an example:

public class TestEvent implements RabbitEvent {
    private final @NotNull UUID uuid;

    public TestEvent(@NotNull UUID uuid) {
        this.uuid = uuid;
    }

    public @NotNull UUID getUUID() {
        return this.uuid;
   }
}

Creating Event Configuration

Every event needs it's own configuration class where you can customize the behavior of event publishing and consumption. Create a configuration class implementing the RabbitEventConfigFactory or RabbitEventConfig interface. Here's an example:

public class TestEventConfig implements RabbitEventConfigFactory<TestEvent> {

    @Override
    public @NotNull RabbitEventConfig<TestEvent> config() {
        return new RabbitEventConfigBuilder<TestEvent>()
                .exchangeName("event.test")
                .exchangeType(BuiltinExchangeType.FANOUT)
                .exchangeRoutingKey("test.#")
                .properties(new AMQP.BasicProperties().builder()
                        .expiration("10000")
                        .build()
                )
                .toJson(event -> new JSONObject()
                        .put("uuid", event.getUUID())
                )
                .fromJson(jsonObject -> new TestEvent(UUID.fromString(jsonObject.getString("uuid"))))
                .build();
    }
}

Cancellable events

Events can be cancellable with priorities in mind. Just implement from Cancellable interface or extend from AbstractCancellable class

Subscribing to Events

Subscribe to events with a dedicated event subscriber class:

public class TestEventSubscriber implements EventSubscriber<TestEvent> {
    @Override
    public void onEvent(@NotNull TestEvent event) {
        System.out.println("EVENT UUID = " + event.getUUID());
    }

    @Override
    public int postOrder() {
        return PostOrder.NORMAL;
    }

    @Override
    public boolean acceptsCancelled() {
        return false;
    }
}

This will create the following queues and exchanges

image image

Publishing Events

Publish events using the event manager:

rabbitEventManager.publish(new TestEvent(uuid), new TestEventConfig().config());

Closing Connections

Ensure all connections are closed when they are no longer needed:

rabbitManager.close();

Example Usage

Explore a complete example of RabbitMQEvents used in a Minecraft Spigot plugin here.

rabbitmqevents's People

Contributors

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