Once you’ve created a message, there are three ways to send it with the Message Bus:
- Directly with
MessageBus
- Asynchronously with
SingleDestinationMessageSender
- Synchronously with
SynchronousMessageSender
Directly with MessageBus
To send a message directly with
MessageBus
,
follow these steps:
-
Get a
MessageBus
reference:@Reference private MessageBus _messageBus;
-
Create a message. For example:
Message message = new Message(); message.put("myId", 12345); message.put("someAttribute", "abcdef");
-
Call the
MessageBus
reference’ssendMessage
method with the destination and message:_messageBus.sendMessage("myDestinationName", message);
Here’s a class that contains this example:
@Component(
immediate = true,
service = SomeServiceImpl.class
)
public class SomeServiceImpl {
...
public void sendSomeMessage() {
Message message = new Message();
message.put("myId", 12345);
message.put("someAttribute", "abcdef");
_messageBus.sendMessage("myDestinationName", message);
}
@Reference
private MessageBus _messageBus;
}
Asynchronously with SingleDestinationMessageSender
The
SingleDestinationMessageSender
interface wraps the Message Bus to send messages asynchronously. Follow these
steps to use this interface to send asynchronous messages:
-
Create a
SingleDestinationMessageSenderFactory
reference:@Reference private SingleDestinationMessageSenderFactory _messageSenderFactory;
-
Create a
SingleDestinationMessageSender
by calling theSingleDestinationMessageSenderFactory
reference’screateSingleDestinationMessageSender
method with the message’s destination:SingleDestinationMessageSender messageSender = _messageSenderFactory.createSingleDestinationMessageSender("myDestinationName");
-
Create a message. For example:
Message message = new Message(); message.put("myId", 12345); message.put("someValue", "abcdef");
-
Send the message by calling the
SingleDestinationMessageSender
instance’ssend
method with the message:messageSender.send(message);
Here’s a class that contains this example:
@Component(
immediate = true,
service = SomeServiceImpl.class
)
public class SomeServiceImpl {
...
public void sendSomeMessage() {
SingleDestinationMessageSender messageSender =
_messageSenderFactory.createSingleDestinationMessageSender("myDestinationName");
Message message = new Message();
message.put("myId", 12345);
message.put("someValue", "abcdef");
messageSender.send(message);
}
@Reference
private SingleDestinationMessageSenderFactory _messageSenderFactory;
}
Synchronously with SynchronousMessageSender
SynchronousMessageSender
sends a message to the Message Bus and blocks until receiving a response or the
response times out. A SynchronousMessageSender
has these
operating modes:
DEFAULT
: Delivers the message in a separate thread and also provides timeouts,
in case the message is not delivered properly.
DIRECT
: Delivers the message in the same thread of execution and blocks until
it receives a response.
Follow these steps to send a synchronous message with
SynchronousMessageSender
:
-
Get a
SingleDestinationMessageSenderFactory
reference:@Reference private SingleDestinationMessageSenderFactory _messageSenderFactory;
-
Create a
SingleDestinationSynchronousMessageSender
by calling theSingleDestinationMessageSenderFactory
reference’screateSingleDestinationSynchronousMessageSender
method with the destination and operating mode. Note that this example uses theDEFAULT
mode:SingleDestinationSynchronousMessageSender messageSender = _messageSenderFactory.createSingleDestinationSynchronousMessageSender( "myDestinationName", SynchronousMessageSender.Mode.DEFAULT);
-
Create a message. For example:
Message message = new Message(); message.put("myId", 12345); message.put("someValue", "abcdef");
-
Send the message by calling the
SingleDestinationSynchronousMessageSender
instance’ssend
method with the message:messageSender.send(message);
Here’s a class that contains this example:
@Component(
immediate = true,
service = SomeServiceImpl.class
)
public class SomeServiceImpl {
...
public void sendSomeMessage() {
Message message = new Message();
message.put("myId", 12345);
message.put("someAttribute", "abcdef");
SingleDestinationSynchronousMessageSender messageSender =
_messageSenderFactory.createSingleDestinationSynchronousMessageSender(
"myDestinationName", SynchronousMessageSender.Mode.DEFAULT);
messageSender.send(message);
}
@Reference
private SingleDestinationMessageSenderFactory _messageSenderFactory;
}