Sending a Message

Once you’ve created a message, there are three ways to send it with the Message Bus:

Directly with MessageBus

To send a message directly with MessageBus, follow these steps:

  1. Get a MessageBus reference:

    @Reference
    private MessageBus _messageBus;
    
  2. Create a message. For example:

    Message message = new Message();
    message.put("myId", 12345);
    message.put("someAttribute", "abcdef");
    
  3. Call the MessageBus reference’s sendMessage 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:

  1. Create a SingleDestinationMessageSenderFactory reference:

    @Reference
    private SingleDestinationMessageSenderFactory _messageSenderFactory;
    
  2. Create a SingleDestinationMessageSender by calling the SingleDestinationMessageSenderFactory reference’s createSingleDestinationMessageSender method with the message’s destination:

    SingleDestinationMessageSender messageSender = 
       _messageSenderFactory.createSingleDestinationMessageSender("myDestinationName");
    
  3. Create a message. For example:

    Message message = new Message();
    message.put("myId", 12345);
    message.put("someValue", "abcdef");
    
  4. Send the message by calling the SingleDestinationMessageSender instance’s send 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:

  1. Get a SingleDestinationMessageSenderFactory reference:

    @Reference
    private SingleDestinationMessageSenderFactory _messageSenderFactory;
    
  2. Create a SingleDestinationSynchronousMessageSender by calling the SingleDestinationMessageSenderFactory reference’s createSingleDestinationSynchronousMessageSender method with the destination and operating mode. Note that this example uses the DEFAULT mode:

    SingleDestinationSynchronousMessageSender messageSender = 
        _messageSenderFactory.createSingleDestinationSynchronousMessageSender(
            "myDestinationName", SynchronousMessageSender.Mode.DEFAULT);
    
  3. Create a message. For example:

    Message message = new Message();
    message.put("myId", 12345);
    message.put("someValue", "abcdef");
    
  4. Send the message by calling the SingleDestinationSynchronousMessageSender instance’s send 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;
}

Creating a Message

Sending Messages Across a Cluster

Using the Message Bus

« Creating a MessageSending Messages Across a Cluster »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0