Wysyłanie wiadomości

Komunikat JMS można wysłać z poziomu bezstanowego komponentu sesyjnego:

@Resource(lookup = "myConnection")
ConnectionFactory connectionFactory;

@Resource(lookup = "myQueue")
Destination inboundQueue;

public void sendMessage(String text) {
    try {
        Connection connection =
            connectionFactory.createConnection();
        Session session =
            connection.createSession(false,
                                     Session.AUTO_ACKNOWLEDGE);
        MessageProducer messageProducer =
            session.createProducer(inboundQueue);
        TextMessage textMessage =
            session.createTextMessage(text);
        messageProducer.send(textMessage);
    } catch (JMSException ex) {
        // ...
    }
}

W powyższym kodzie:

  • ConnectionFactory jest administrowanym obiektem JMS używanym do tworzenia połączenia z dostawcą JMS. Zamiast tego obiektu ...

Get Java EE 6. Leksykon kieszonkowy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.