azure - 将 Azure 服务总线与 Android 连接

标签 azure azure-java-sdk

我编写了一个简单的 java 程序(jdk 1.7),它列出了我的所有服务总线主题并将每个主题的名称打印到标准输出:

try {
    String namespace = "myservicebus"; // from azure portal
    String issuer = "owner";  // from azure portal
    String key = "asdjklasdjklasdjklasdjklasdjk";  // from azure portal
            
    Configuration config = ServiceBusConfiguration.configureWithWrapAuthentication(
        namespace, 
        issuer,
        key, 
        ".servicebus.windows.net", 
        "-sb.accesscontrol.windows.net/WRAPv0.9"); 
             
    ServiceBusContract service = ServiceBusService.create(config);
    ListTopicsResult result = service.listTopics();
    List<TopicInfo> infoList = result.getItems();
    for(TopicInfo info : infoList) {
        System.out.println( info.getPath());
    }
} catch (Exception e) {
    e.printStackTrace();
}

现在,我尝试在一个简单的 Android 项目(Android 4.2)中运行这个示例,但它不起作用。 运行时总是抛出以下错误:

java.lang.RuntimeException: Service or property not registered:  com.microsoft.windowsazure.services.serviceBus.ServiceBusContract

有人成功建立了从 Android 设备(或模拟器)到 Azure 服务总线的连接吗?

Microsoft Azure-Java-SDK 不支持 Android 项目吗?

提前致谢

最佳答案

此错误是由于生成的 apk 不包含(删除)ServiceLoader 信息(在 META-INF/services 下)造成的。您可以测试自己从生成的 jar 中删除它,并看到出现相同的错误。文档中说现在支持它,但我发现使用它有问题。

http://developer.android.com/reference/java/util/ServiceLoader.html

您可以使用 ant 手动将数据包含在 apk 中

Keep 'META-INF/services'-files in apk

经过 10 个小时的调试,手动删除类,包括 META-INF/services 等,我发现 Azure SDK 使用了一些 Android 不支持的类 (javax.ws.*),任何解决方法都对我有用。

因此,我建议在 Android 环境中使用 REST API,请在下面找到我用于向该主题发送消息的源代码。

private static String generateSasToken(URI uri) {
    String targetUri;
    try {
        targetUri = URLEncoder
        .encode(uri.toString().toLowerCase(), "UTF-8")
        .toLowerCase();

        long expiresOnDate = System.currentTimeMillis();
        int expiresInMins = 20; // 1 hour
        expiresOnDate += expiresInMins * 60 * 1000;
        long expires = expiresOnDate / 1000;
        String toSign = targetUri + "\n" + expires;

        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = sasKey.getBytes("UTF-8");
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);
        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(toSign.getBytes("UTF-8"));

        // using Apache commons codec for base64
//      String signature = URLEncoder.encode(
//      Base64.encodeBase64String(rawHmac), "UTF-8");
        String rawHmacStr = new String(Base64.encodeBase64(rawHmac, false),"UTF-8");
        String signature = URLEncoder.encode(rawHmacStr, "UTF-8");

        // construct authorization string
        String token = "SharedAccessSignature sr=" + targetUri + "&sig="
        + signature + "&se=" + expires + "&skn=" + sasKeyName;
        return token;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public static void Send(String topic, String subscription, String msgToSend) throws Exception {

        String url = uri+topic+"/messages";

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);

        // Add header
        String token = generateSasToken(new URI(uri));
        post.setHeader("Authorization", token);
        post.setHeader("Content-Type", "text/plain");
        post.setHeader(subscription, subscription);
        StringEntity input = new StringEntity(msgToSend);
        post.setEntity(input);

        System.out.println("Llamando al post");
        HttpResponse response = client.execute(post);
        System.out.println("Response Code : " 
                + response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() != 201)
            throw new Exception(response.getStatusLine().getReasonPhrase());

}

更多信息请参阅 REST API Azure 信息。

关于azure - 将 Azure 服务总线与 Android 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15311060/

相关文章:

azure - 如何在运行 Azure 流分析查询时获取系统时间

Azure 身份 : Custom token validation to validate issuers in a multi tenant app

azure - Runbook 作为 azure 帐户运行 Powershell

javascript - 使用 AAD MSAL 登录 - 登录已在进行中

Azure Java 程序在尝试检索托管服务标识凭据时被阻止

azure - 如何使用 az cli 命令访问私有(private) AKS 集群?

java - ServiceBusSessionReceiverAsyncClient 在关闭期间抛出 IllegalStateException

Azure documentDB - 具有指定 ID 或名称的资源已存在

java - Azure 存储 Java API : Account does not support HTTP

azure - 使用java驱动程序异步写入documentdb