Backend messaging system for Java applications.
Messenger 2.1.0 requires Java 21 or newer.
Add JitPack to your repositories and select the module you need:
repositories {
mavenCentral()
maven("https://jitpack.io")
}
dependencies {
implementation("com.github.opentownlab.Messenger:messenger-api:2.1.0")
// implementation("com.github.opentownlab.Messenger:messenger-redis:2.1.0")
// implementation("com.github.opentownlab.Messenger:messenger-rabbitmq:2.1.0")
// implementation("com.github.opentownlab.Messenger:messenger-nats:2.1.0")
}// Set our server ID
UUID serverId = UUID.randomUUID();
// Create a new non-redundant PacketService with 4 sending threads and a packet version of 1.
PacketService packetService = new PacketService(4, false, (byte) 1);// Create a new messaging handler implementation
MessagingHandlerImpl handlerImpl = new MessagingHandlerImpl(packetService);
// Create a new server messaging handler implementation - should extend AbstractServerMessagingHandler
AbstractMessagingHandler serverHandler = new ServerMessagingHandlerImpl(serverId, packetService, handlerImpl);
// Add the server messaging handler to the messaging handler implementation
handlerImpl.addHandler(serverHandler);
// Create your own custom messaging handler - should extend AbstractMessagingHandler
AbstractMessagingHandler customHandler = new CustomMessagingHandler(packetService);
// Add the custom messaging handler to the messaging handler implementation
handlerImpl.addHandler(customHandler);// Create a new RabbitMQMessagingService
// The name of this service is "engine1" and uses the RabbitMQ channel "my-data" for all packets
// This service will have no startup delay (ie. will instantly start receiving packets)
// This service will *not* dump packets to disk, although the directory for those dumped packets will be packetDir
// Dumping packets is useful for Zstd training
RabbitMQMessagingService rabbit = RabbitMQMessagingService.builder(packetService, "engine1", "my-data", handlerImpl, 0L, false, packetDir)
.url("127.0.0.1", 5672, "/") // Connect to 127.0.0.1:5672 @ vhost "/"
.credentials("guest", "guest") // With guest user/pass
.timeout(5000) // With a timeout of 5 seconds
.build();
// Queue a packet through the packetservice
packetService.queuePacket(new KeepAlivePacket(serverId));
// Queue several packets through the packetservice
packetService.queuePackets(
new InitializationPacket(serverId, packetService.getPacketVersion())
new CustomDataPacket()
);
// Flush the packetService queue to actually send any packets that have been previously queued
packetService.flushQueue();Packetno longer implementsSerializable; use its ByteBuf codec when serialization is required.AbstractServerMessagingHandlerwill remove any servers that have not broadcastedKeepAlivePackets in 20 seconds.AbstractServerMessagingHandlerrequiresInitializationPackets to be sent on startup, andPacketVersionPacketresponses toPacketVersionRequestPacketsAbstractServerMessagingHandlerprefers handlingShutdownPackets over a missedKeepAlivePacketon shutdownPacketService#queuePacketandPacketService#queuePacketsdo not actually broadcast packets -PacketService#flushQueuewill perform the broadcasting functionality with any packets queued. This allows for batching packets into aMultiPacketautomatically