Java 8 Azure服务总线队列上的消息数量

huangapple go评论75阅读模式
英文:

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项目,如下所示:-

Java 8 Azure服务总线队列上的消息数量

代码:

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>

确保为客户端应用程序分配了所有者角色分配

Java 8 Azure服务总线队列上的消息数量

在Azure门户中,我在我的ServiceBus队列中有10条消息,如下所示:-

Java 8 Azure服务总线队列上的消息数量

它成功运行并提供了Azure服务总线队列上的消息计数。请参考下面:-

输出:
Java 8 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:-

Java 8 Azure服务总线队列上的消息数量

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 = &quot;&lt;your_client_id&gt;&quot;;
        String domain = &quot;&lt;your-tenant_id&gt;&quot;;
        String secret = &quot;&lt;&gt;your_client_secret&gt;&quot;;
        String subscriptionId = &quot;&lt;your_subscription_id&gt;&quot;;
        String resourceGroupName = &quot;your_resource_group_name&quot;;
        String namespaceName = &quot;&lt;your_servicebus_name&gt;&quot;;
        String queueName = &quot;&lt;your_queue_name&gt;&quot;;

        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(&quot;Message count: &quot; + messageCount);
    }
}

Dependencies:

&lt;dependencies&gt;
     &lt;dependency&gt;
          &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;
          &lt;artifactId&gt;azure&lt;/artifactId&gt;
          &lt;version&gt;1.41.4&lt;/version&gt;
     &lt;/dependency&gt;
&lt;/dependencies&gt;

Make sure to assign role assignment for the client application as owner ,

Java 8 Azure服务总线队列上的消息数量

In the Azure portal, I have 10 messages in my ServiceBus Queue as below:-

Java 8 Azure服务总线队列上的消息数量

It ran successfully and gave the number of messages count on an Azure service bus queue. Refer below:-

Output:
Java 8 Azure服务总线队列上的消息数量

Reference:
Check this MS Doc to know more about sending messages to Azure Service Bus.

huangapple
  • 本文由 发表于 2023年5月17日 22:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定