{{announcement.body}}
{{announcement.title}}

Sending a Message to Azure Service Bus Queue/Topic Using TIBCO BW 6.x

DZone 's Guide to

Sending a Message to Azure Service Bus Queue/Topic Using TIBCO BW 6.x

In this article, we will walk through how to send messages on an Azure Service Bus queue/topic.

· Integration Zone ·
Free Resource

In this article, we will walk through how to send messages on an Azure Service Bus queue/topic.

Basic Prerequisites

  • Create an Azure Service Bus queue/topic: For this, you can refer to the Microsoft Azure docs to understand what is Azure queue/topic and how we can create them in the Azure portal. Here's a link you can refer to Create Azure ServiceBus queue/topic. Or, you can refer to another Dzone Article by Jitendra Bafna where he has explained the same: Jitendra Article.
  • Get these below details (which you will be already having after following above step) using which we will create Shared Access Signature (SAS) for our Service Bus queue/topic:
    • Namespace name: In my case it's (testazurenamespace)
    • Key name: By default, it is (RootManageSharedAccessKey)
    • Key-value: This will be your primary key

Shared Access Signatures

Shared Access Signature(SAS) is the primary security mechanism for service bus messaging. They act as an authorization mechanism for accessing a service bus queue/topic. For more detail, on SAS you can refer to the following Microsoft Azure link SAS Azure doc

Creating SAS Token

There are many ways to create SAS token for accessing Azure service bus queue/topic. In this article, we will be using a basic Java program that will help us to create SAS token

Java
 




xxxxxxxxxx
1
82


 
1
package com.rudra.sastokengenerator;
2
 
          
3
import java.io.UnsupportedEncodingException;
4
import java.net.URLEncoder;
5
import java.security.InvalidKeyException;
6
import java.security.NoSuchAlgorithmException;
7
import java.util.Base64;
8
import java.util.Base64.Encoder;
9
import javax.crypto.Mac;
10
import javax.crypto.spec.SecretKeySpec;
11
import java.util.*;
12
 
          
13
@SuppressWarnings("unused")
14
public class SASTokenGenerator {
15
static String finalToken;
16
private static String result;
17
    
18
    public static String main(String s){
19
        setResult(finalToken);
20
        return finalToken;
21
    }
22
    
23
        public SASTokenGenerator() {
24
        
25
        }
26
    
27
    public static String GetSASToken(String resourceUri, String keyName, String key)
28
      {
29
          long epoch = System.currentTimeMillis()/1000L;
30
          int week = 60*60*24*7;
31
          String expiry = Long.toString(epoch + week);
32
 
          
33
          String sasToken = null;
34
          try {
35
              String stringToSign = URLEncoder.encode(resourceUri, "UTF-8") + "\n" + expiry;
36
              String signature = getHMAC256(key, stringToSign);
37
              sasToken = "SharedAccessSignature sr=" + URLEncoder.encode(resourceUri, "UTF-8") +"&sig=" +
38
                      URLEncoder.encode(signature, "UTF-8") + "&se=" + expiry + "&skn=" + keyName;
39
          } catch (UnsupportedEncodingException e) {
40
 
          
41
              e.printStackTrace();
42
          }
43
 
          
44
          return sasToken;
45
      }
46
 
          
47
    public static String getHMAC256(String key, String input) {
48
        Mac sha256_HMAC = null;
49
        String hash = null;
50
        try {
51
            sha256_HMAC = Mac.getInstance("HmacSHA256");
52
            SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
53
            sha256_HMAC.init(secret_key);
54
            Encoder encoder = Base64.getEncoder();
55
 
          
56
            hash = new String(encoder.encode(sha256_HMAC.doFinal(input.getBytes("UTF-8"))));
57
 
          
58
        } catch (InvalidKeyException e) {
59
            e.printStackTrace();
60
        } catch (NoSuchAlgorithmException e) {
61
            e.printStackTrace();
62
        } catch (IllegalStateException e) {
63
            e.printStackTrace();
64
        } catch (UnsupportedEncodingException e) {
65
            e.printStackTrace();
66
        }
67
 
          
68
        return hash;
69
    }
70
 
          
71
    public static String getResult() {
72
        return result;
73
    }
74
 
          
75
    public static void setResult(String result) {
76
        SASTokenGenerator.result = result;
77
    }
78
}



In the above program we will be calling GetSASToken function which accepts 3 parameters whose composition will look like below:

  1. resourceUri : {Namespace}.servicebus.windows.net/{queue_name} , example (testazurenamespace.servicebus.windows.net/testazurequeue)
  2. keyName : RootManageSharedAccessKey
  3. key: primary key value obtained

Before we move on to the actual TIBCO BW code, we will be generating a certificate for our REST Azure service bus queue/topic endpoint that we will be hitting. In my case, the endpoint is: https://testazurenamespace.servicebus.windows.net/testazurequeue/message 

In the end, the "/message" URI is mandatory to be used in any Azure endpoint

To generate a certificate from URL you can refer the following link generate a certificate from the endpoint

certificate status

In my case, I got 3 .cer files which I combined in a single .jks file to be used in my code.

Finally, Our TIBCO BW Process Will Look Like

TIBCO BW Process

JavaInvoke Activity Will Refer to the Java Program as Below

JavaInvoke

Input tab will have our details as below:

JavaInvoke

I have altered the key value for security reason, you have to use your Primary Key value here.

The output of JavaInvoke activity will be our SAS token that we will be further using in SendHTTPRequest for authorization purposes to send messages.

SendHTTPRequest Activity Will Have the Following Configuration

SendHTTPRequest

The HTTP Client will be used to configure the certificate as below:

HTTP Client

KeyStore will finally have the certificate configured as below:

KeyStore

Finally when we will proceed with providing the inputs in the Input tab of SendHTTPRequest as below:

SendHTTPRequest

Topics:
access signatures, azure, azure service bus, integration, java, tibco, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}