このエントリは2023/03/24現在の情報に基づいています。将来の機能追加や変更に伴い、記載内容からの乖離が発生する可能性があります。
以前、以下のようなエントリを書いた。
Announcement: Azure Active Directory backed authentication for JMS 2.0 API on Azure Service Bus
https://azure.microsoft.com/updates/announcment-azure-active-directory-backed-authentication-for-jms-20-api-on-azure-service-bus/
Announcing Azure Active Directory backed authentication for JMS 2.0 API on Azure Service Bus
https://techcommunity.microsoft.com/t5/messaging-on-azure-blog/announcing-azure-active-directory-backed-authentication-for-jms/ba-p/3771409
ドキュメントは以下。
Azure Service Bus JMS 2.0 開発者ガイド / Azure Service Bus JMS 2.0 developer guide
https://learn.microsoft.com/azure/service-bus-messaging/jms-developer-guide
試してみる
HTTPリクエストを受けて返す簡単なアプリケーションを作成し、この中にService Busへメッセージを送信するロジックを追加してみる。
Token取得はいつものこれ。
DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();
送信側の例はこんな感じ。
var connectionFactory = new ServiceBusJmsConnectionFactory(
new DefaultAzureCredentialBuilder().build(),
System.getenv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE"),
new ServiceBusJmsConnectionFactorySettings());
String queueMessage = String.format(format, msg, new Date());
try (var jmsContext = connectionFactory.createContext()) {
TextMessage textMessage = jmsContext.createTextMessage(queueMessage);
jmsContext.createProducer().send(jmsContext.createQueue(QUEUE_NAME), queueMessage);
}
catch (JMSRuntimeException | JMSException e) {
e.printStackTrace();
}
違いは、ServiceBusJmsConnectionFactoryを呼び出す際の引数。接続文字列の場合と少々違う。
あとは、Managed Identityを有効にして、テストを実施。今回はSystem assigned managed identityを使ったのでClient IDを指定していないが、User assigned managed identityの場合はClient IDの指定が必要。
今回試したところ、動作はしたものの、一部の環境ではうまく動作しなかったので、その件については別途調査を継続している。