21.2.2. メッセージ送信
設定が完了したら、 JMS
TopicPublisher と TopicSession をコンポーネントにインジェクトすることができます。
@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);
}
}
}
あるいは、 キューの場合は次のようになります。
@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);
}
}
}