Junior — Middle
What methods of asynchronous message exchange do you know?
sobes.tech AI
Answer from AI
Asynchronous message exchange in Java is typically implemented using the following methods:
-
Message Queues — for example, JMS (Java Message Service), where messages are placed in a queue and consumed asynchronously.
-
Event-driven programming — using listeners and event handlers that respond to incoming messages.
-
CompletableFuture and Future — for asynchronous task execution and result processing without blocking.
-
Reactive Streams and libraries (RxJava, Project Reactor) — enabling working with data streams asynchronously and reactively.
-
WebSocket and asynchronous HTTP requests — for real-time message exchange between client and server.
Example of using JMS to send a message:
// Sending a message to the queue
QueueConnection connection = ...;
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("exampleQueue");
QueueSender sender = session.createSender(queue);
TextMessage message = session.createTextMessage("Hello, async world!");
sender.send(message);