JVM is in development for v1. Interested in contributing or chatting with us?Get in touch!
JVM - queue.send()
Send tasks to a queue.
import io.nitric.Nitric;
import io.nitric.resources.QueuePermission;
public class Application {
public static void main(String[] args) {
var queue = Nitric.INSTANCE.queue("batch").with(QueuePermission.Send);
var payload = Map.of("message", "hello");
queue.send(payload);
Nitric.INSTANCE.run();
}
}
Parameters
- Name
tasks
- Required
- Required
- Type
- Task or List<Task>
- Description
A task or an array of tasks to send to the queue.
Examples
Send a task to a queue
import io.nitric.Nitric;
import io.nitric.resources.QueuePermission;
public class Application {
public static void main(String[] args) {
var queue = Nitric.INSTANCE.queue("batch").with(QueuePermission.Send);
var payload = Map.of("message", "payloads can be any serializable object");
queue.send(payload);
Nitric.INSTANCE.run();
}
}
Send multiple tasks to a queue
import io.nitric.Nitric;
import io.nitric.resources.QueuePermission;
import java.util.List;
public class Application {
public static void main(String[] args) {
var queue = Nitric.INSTANCE.queue("batch").with(QueuePermission.Send);
var payloads = List.of(
Map.of(
"type", "SMS",
"to", "+172000000000",
"subject", "Notification",
"message", "A text message from Nitric"
),
Map.of(
"type", "Email",
"to", "hello@example.com",
"subject", "Notification",
"message", "A notification from Nitric",
)
);
queue.send(payloads);
Nitric.INSTANCE.run();
}
}
Dealing with failures
In rare cases when sending tasks to a queue some tasks might fail to be sent. The response from send()
will include an array of any tasks that failed to send. You can process this array to retry or log the error.
var failed = queue.send(tasks);
failed.forEach((task) -> {
System.out.println(task);
});