21.2.2. Sending messages
Once configuration is complete, you can inject a JMS
TopicPublisher
and TopicSession
into any component:
@Name("stockPriceChangeNotifier") public class StockPriceChangeNotifier { @In private TopicPublisher stockTickerPublisher; @In private TopicSession topicSession; public void publish(StockPrice price) { try { stockTickerPublisher.publish(topicSession .createObjectMessage(price)); } catch (Exception ex) { throw new RuntimeException(ex); } } }
Or, to work with a queue:
@Name("paymentDispatcher") public class PaymentDispatcher { @In private QueueSender paymentQueueSender; @In private QueueSession queueSession; public void publish(Payment payment) { try { paymentQueueSender.send(queueSession.createObjectMessage(payment)); } catch (Exception ex) { throw new RuntimeException(ex); } } }