英文:
Java 8 Number of messages on Azure service bus queue
问题
I want to get the number of messages on an Azure service bus queue in Java 8. I tried a couple of things but when I want to import these libraries, my Maven can't find the right libraries (jar) like it seems:
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.servicebus.Queue;
import com.microsoft.azure.management.servicebus.ServiceBusNamespace;
import com.microsoft.rest.LogLevel;
What library I need and do you have a helloWorld example to get the number of messages on a queue with Java?
Until now I have this, but my import all gives errors:
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret, AzureEnvironment.AZURE);
Azure azure = Azure.configure().withLogLevel(LogLevel.NONE).authenticate(credentials).withSubscription(subscriptionId);
ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().getByResourceGroup(resourceGroupName, namespaceName);
Queue queue = serviceBusNamespace.queues().getByName(queueName);
int messageCount = queue.inner().messageCount();
System.out.println("Message count: " + messageCount);
Anyway, is this code okay?
英文:
I want to get the number of messages on an Azure servicebus queue in Java 8. I tried a couple of things but when I want to import these libraries, my Maven can't find the right libarys(jar) like it seems:
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.servicebus.Queue;
import com.microsoft.azure.management.servicebus.ServiceBusNamespace;
import com.microsoft.rest.LogLevel;
What library I need and do you have a helloWorld example to get the number of messages on a queue with Java ?
Untill now I have this, but my import all gives errors:
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret, AzureEnvironment.AZURE);
Azure azure = Azure.configure().withLogLevel(LogLevel.NONE).authenticate(credentials).withSubscription(subscriptionId);
ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().getByResourceGroup(resourceGroupName, namespaceName);
Queue queue = serviceBusNamespace.queues().getByName(queueName);
int messageCount = queue.inner().messageCount();
System.out.println("Message count: " + messageCount);
Anyway is this code ok ?
答案1
得分: 1
以下是您要翻译的内容:
使用以下Java代码,我能够在Java 8中获取Azure服务总线队列上的消息数量。
我在我的环境中创建了一个新的Java、Maven项目,如下所示:-
代码:
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.servicebus.Queue;
import com.microsoft.azure.management.servicebus.ServiceBusNamespace;
import com.microsoft.rest.LogLevel;
public class ServiceBusQueueExample {
public static void main(String[] args) throws Exception {
String clientId = "<your_client_id>";
String domain = "<your-tenant_id>";
String secret = "<your_client_secret>";
String subscriptionId = "<your_subscription_id>";
String resourceGroupName = "your_resource_group_name";
String namespaceName = "<your_servicebus_name>";
String queueName = "<your_queue_name>";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret, AzureEnvironment.AZURE);
Azure azure = Azure.configure().withLogLevel(LogLevel.NONE).authenticate(credentials).withSubscription(subscriptionId);
ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().getByResourceGroup(resourceGroupName, namespaceName);
Queue queue = serviceBusNamespace.queues().getByName(queueName);
long messageCount = queue.inner().messageCount();
System.out.println("Message count: " + messageCount);
}
}
依赖项:
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.41.4</version>
</dependency>
</dependencies>
确保为客户端应用程序分配了所有者的角色分配,
在Azure门户中,我在我的ServiceBus队列中有10条消息,如下所示:-
它成功运行并提供了Azure服务总线队列上的消息计数。请参考下面:-
输出:
参考:
查看此MS文档以了解有关将消息发送到Azure Service Bus的更多信息。
英文:
With the below java code, I am able to get the number of messages on an Azure service bus queue in Java 8.
I created a new java, maven project with java 8 in my environment as below:-
Code:
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.servicebus.Queue;
import com.microsoft.azure.management.servicebus.ServiceBusNamespace;
import com.microsoft.rest.LogLevel;
public class ServiceBusQueueExample {
public static void main(String[] args) throws Exception {
String clientId = "<your_client_id>";
String domain = "<your-tenant_id>";
String secret = "<>your_client_secret>";
String subscriptionId = "<your_subscription_id>";
String resourceGroupName = "your_resource_group_name";
String namespaceName = "<your_servicebus_name>";
String queueName = "<your_queue_name>";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret, AzureEnvironment.AZURE);
Azure azure = Azure.configure().withLogLevel(LogLevel.NONE).authenticate(credentials).withSubscription(subscriptionId);
ServiceBusNamespace serviceBusNamespace = azure.serviceBusNamespaces().getByResourceGroup(resourceGroupName, namespaceName);
Queue queue = serviceBusNamespace.queues().getByName(queueName);
long messageCount = queue.inner().messageCount();
System.out.println("Message count: " + messageCount);
}
}
Dependencies:
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.41.4</version>
</dependency>
</dependencies>
Make sure to assign role assignment for the client application as owner ,
In the Azure portal, I have 10 messages in my ServiceBus Queue as below:-
It ran successfully and gave the number of messages count on an Azure service bus queue. Refer below:-
Output:
Reference:
Check this MS Doc to know more about sending messages to Azure Service Bus.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论