https://github.com/saicone/delivery4j
Java library facade to send messages using mutliple data delivery concepts
https://github.com/saicone/delivery4j
activemq hikari java kafka nats polling postgres postgresql rabbitmq redis sql
Last synced: 4 days ago
JSON representation
Java library facade to send messages using mutliple data delivery concepts
- Host: GitHub
- URL: https://github.com/saicone/delivery4j
- Owner: saicone
- License: mit
- Created: 2024-01-06T20:36:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-04-02T00:02:16.000Z (7 days ago)
- Last Synced: 2026-04-02T11:52:20.042Z (6 days ago)
- Topics: activemq, hikari, java, kafka, nats, polling, postgres, postgresql, rabbitmq, redis, sql
- Language: Java
- Homepage:
- Size: 522 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Delivery4j
Java library facade for multiple data delivery concepts.
There are multiple ways to transfer data between Java applications, this library offers an easy way to connect with them using common methods.
Currently supporting the brokers:
* [ActiveMQ](https://github.com/apache/activemq) using topic producers and consumers.
* [Kafka](https://github.com/apache/kafka) using empty-key records to producers.
* [NATS](https://github.com/nats-io/nats.java) using subject subscription.
* [PostgreSQL](https://github.com/pgjdbc/pgjdbc) using `LISTEN` and `NOTIFY` statement.
* [RabbitMQ](https://github.com/rabbitmq/rabbitmq-java-client) using queue and consumer via exchange.
* [Redis](https://github.com/redis/jedis) using publish and subscribe (also compatible with [KeyDB](https://github.com/Snapchat/KeyDB)).
* SQL polling (not a real broker, but can be used as one).
* [Valkey](https://github.com/valkey-io/valkey-java) using publish and subscribe (same as Redis, but with an older API).
PostgreSQL and SQL are also compatible with [Hikari](https://github.com/brettwooldridge/HikariCP).
## Dependency
Delivery4j contains the following artifacts:
* `delivery4j` - The main project.
* `broker-activemq` - ActiveMQ broker.
* `broker-kafka` - Kafka broker.
* `broker-nats` - NATS broker.
* `broker-postgresql` - PostgreSQL broker using plain Java connections.
* `broker-postgresql-hikari` - PostgreSQL broker using Hikari library.
* `broker-postgresql-hikari-java8` - PostgreSQL broker using Hikari 4.x (for Java 8).
* `broker-rabbitmq` - RabbitMQ broker.
* `broker-redis-jedis` - Redis broker using [jedis](https://github.com/redis/jedis) library.
* `broker-sql` - SQL broker using plain Java connections.
* `broker-sql-hikari` - SQL broker using Hikari library.
* `broker-sql-hikari-java8` - SQL broker using Hikari 4.x (for Java 8).
* `broker-valkey` - Valkey broker.
* `extension-caffeine` - Extension to detect and use Caffeine cache on MessageChannel.
* `extension-guava` - Extension to detect and use Guava cache on MessageChannel.
build.gradle
```groovy
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.saicone.delivery4j:delivery4j:1.1.5'
}
```
build.gradle.kts
```kotlin
repositories {
maven("https://jitpack.io")
}
dependencies {
implementation("com.saicone.delivery4j:delivery4j:1.1.5")
}
```
pom.xml
```xml
Jitpack
https://jitpack.io
com.saicone.delivery4j
delivery4j
1.1.5
compile
```
## Usage
How to use Delivery4j library.
### Broker
Using brokers is pretty simple, you just need to create a broker instance (depending on the implementation) and set a consumer.
```java
Broker broker = // Create instance from any implementation
// Subscribe to channels
broker.subscribe("hello:world", "myChannel1");
broker.setConsumer((channel, data) -> {
// do something
});
// Start connection
broker.start();
// Send data
byte[] data = ...;
broker.send("myChannel1", data);
```
Some brokers require to convert bytes to String and viceversa, Base64 is used by default.
```java
Broker broker = // Create instance from any implementation
broker.setCodec(new ByteCodec<>() {
@Override
public @NotNull String encode(byte[] src) {
// convert bytes to String
}
@Override
public byte[] decode(@NotNull String src) {
// convert String to bytes
}
});
```
Some brokers have blocking operations or repetitive tasks, it's suggested to implement your own executor.
```java
Broker broker = // Create instance from any implementation
broker.setExecutor(new DelayedExecutor() {
@Override
public @NotNull MyTaskObject execute(@NotNull Runnable command) {
// run task and return itself
}
@Override
public @NotNull MyTaskObject execute(@NotNull Runnable command, long delay, @NotNull TimeUnit unit) {
// run delayed task and return itself
}
@Override
public @NotNull MyTaskObject execute(@NotNull Runnable command, long delay, long period, @NotNull TimeUnit unit) {
// run repetitive task and return itself
}
@Override
public void cancel(@NotNull MyTaskObject unused) {
// cancel task
}
});
```
And also a logging instance to log information about connection and exceptions, by default it use the best available implementation.
It uses a number terminology for logging levels:
1. Error
2. Warning
3. Information
4. Debug
```java
Broker broker = ...;
// --- Using existing logger instance
Logger logger = ...;
broker.setLogger(LogFilter.valueOf(logger, 3)); // using 3 as max logging level
broker.setLogger(LogFilter.valueOf(logger, () -> 3)); // you can also supply the max level dynamically
// --- Creating a logger instance from name
broker.setLogger(LogFilter.valueOf("LogName", 3));
broker.setLogger(LogFilter.valueOf("LogName", () -> 3));
// --- Creating a logger instance from class
broker.setLogger(LogFilter.valueOf(MyObject.class, 3));
broker.setLogger(LogFilter.valueOf(MyObject.class, () -> 3));
```
### Messenger
Probably the reason why you are here, it's a simple usage of brokers to send and receive multi-line String messages.
First you need to extend AbstractMessenger and provide a broker.
```java
public class Messenger extends AbstractMessenger {
@Override
protected Broker loadBroker() {
// Create instance from any implementation
}
}
```
And then use the Messenger.
```java
Messenger messenger = new Messenger();
// Start connection
messenger.start();
// Send multi-line message to channel
messeger.send("myChannel1", "Hello", "World");
// Subscribe to channel
messenger.subscribe("myChannel1").consume((channel, lines) -> {
// do something
});
```
The subscribed message channels can have a cache instance to avoid receive outbound messages, by default it use the best available implementation.
```java
Messenger messenger = new Messenger();
// Subscribe to channel
MessageChannel channel = messenger.subscribe("myChannel1").consume((channel, lines) -> {
// do something
});
// Cache message IDs
channel.cache(true);
// Cache with provided expiration
channel.cache(20, TimeUnit.SECONDS);
```
And also can have an end-to-end encryption.
```java
Messenger messenger = new Messenger();
// Subscribe to channel
MessageChannel channel = messenger.subscribe("myChannel1").consume((channel, lines) -> {
// do something
});
// Your key
SecretKey key = ...;
channel.encryptor(Encryptor.of(key));
```