Tutorial: How to leverage Service Bus features using the AppFabric SDK for Java Developers
 
 
 
Overview
This tutorial explains how developers can easily use the  Services SDK to write Java applications that use the Message Buffer feature of Service Bus provided by AppFabric.

For more information about AppFabric Service Bus please refer here.

The AppFabric SDK for Java developers provides a class called MessageBuffer for performing various operations associated to Message Buffer such as,
  • Creating a Message Buffer on AppFabric Service Bus
  • Sending a message to Message Buffer
  • Retrieving a message from Message Buffer
  • Locking and Retrieving a message from Message Buffer
  • Retrieving Message Buffer policy
  • Deleting a Message Buffer
 
The following sections describe how to leverage above functionality with usage examples. To try out usage examples described in following sections please follow the installation instructions first, if you haven't already done so.
 
Go to Top
 
Creating a Message Buffer
Following code snippet defines a TestMessageBuffer class that demonstrates how to use the SDK to create a message.
 
   import com.persistent.appfabric.acs.Credentials;
   import com.persistent.appfabric.acs.Credentials.TOKEN_TYPE;
   import com.persistent.appfabric.sb.MessageBuffer;
   import com.persistent.appfabric.sb.MessageBufferPolicy;

   public class TestMessageBuffer {
        public static void main(String[] args) {
            MessageBufferPolicy messageBufferPolicy = new MessageBufferPolicy
                ("Required", "None", "PT20M", 10);
	    Credentials credentials;
	    try {
		credentials = new Credentials(TOKEN_TYPE.SharedSecretToken, 
                     issuerName, issuerKey);
		MessageBuffer msgBuffer = new MessageBuffer(credentials, 
                     serviceNamespace);
		// Create message buffer  
		msgBuffer.createMessageBuffer("TestMessageBuffer", 
                     messageBufferPolicy);
	} catch (Exception e) {
		e.printStackTrace();	
      }
    }
  }

 
The authentication aspects required for creation of a Message Buffer are managed by the Credential class. For creation of a message buffer, the token type needs to be passed as SharedSecretToken, and the issuer name and issuer key to be used are provided by AppFabric.
 
Creation of a message buffer requires message buffer policy to be defined that is used to control various aspects of the message buffer's behavior. MessageBufferPolicy class provides functionality associated to message buffer policy. In the above example, message buffer policy states that the message buffer requires authentication, has no transport protection, expires after 20 minutes and can have maximum 10 messages.
 
 
Go to Top
 
Send a Message to Message Buffer
The following code snippet defines a TestSendMessage class that demonstrates how to send a message to message buffer named, ‘TestMessageBuffer’. Note that, it assumes that the ‘TestMessageBuffer’ is already created on Service Bus. To understand how to create a message buffer please refer to the code snippet "Creating a Message Buffer"
 
   import com.persistent.appfabric.acs.Credentials;
   import com.persistent.appfabric.acs.Credentials.TOKEN_TYPE;
   import com.persistent.appfabric.sb.MessageBuffer;
   import com.persistent.appfabric.sb.MessageBufferPolicy;

   public class TestSendMessage {
      public static void main(String[] args) {
          MessageBufferPolicy messageBufferPolicy = new MessageBufferPolicy
              ("Required", "None", "PT20M", 10);
	  Credentials credentials;
	  try {
		credentials = new Credentials(TOKEN_TYPE.SharedSecretToken, 
                     issuerName, issuerKey);
		MessageBuffer msgBuffer = new MessageBuffer(credentials, 
                     serviceNamespace);
		msgBuffer.sendMessage("TestMessageBuffer", "This is a test 
                     message.");	
             } catch (Exception e) {
		e.printStackTrace();	
         }
      }
   }

 
Running this code snippet generates following output:
 
 
Go to Top
 
Retrieve a Message from Message Buffer
 
The following code snippet defines a TestRetrieveMessage class that demonstrates how to retrieve a message from a message buffer named, ‘TestMessageBuffer’. Note that, it assumes that the ‘TestMessageBuffer’ is already created on Service Bus. To understand how to create a message buffer please refer to the code snippet for 'Creating a Message Buffer'
 
   import com.persistent.appfabric.acs.Credentials;
   import com.persistent.appfabric.acs.Credentials.TOKEN_TYPE;
   import com.persistent.appfabric.sb.MessageBuffer;


   public class TestRetrieveMessage {
       public static void main(String[] args) {
           Credentials credentials;
           try {
		credentials = new Credentials(TOKEN_TYPE.SharedSecretToken, 
                     issuerName, issuerKey);
		MessageBuffer msgBuffer;
		msgBuffer = new MessageBuffer(credentials, serviceNamespace);
		String retrievedMessage = msgBuffer.retrieveMessage
                     ("TestMessageBuffer");
	       } catch (Exception e) {
		     e.printStackTrace();
	   }
       }
   }


 
Running this code snippet generates output similar to following provided a message exists on the message buffer. In this case, the message retrieved is, “This is a test message”:
 
 
Go to Top
 
Retrieving Message Buffer Policy
 
MessageBuffer policy controls various aspects of the message buffer’s behavior. It is encapsulated in a class named MessageBufferPolicy provided by AppFabric SDK for Java developers. Message buffer policy associated to a message buffer can be obtained using the getPolicy() method of the MessageBuffer class as demonstrated in the code snippet below:
 
   import com.persistent.appfabric.acs.Credentials;
   import com.persistent.appfabric.acs.Credentials.TOKEN_TYPE;
   import com.persistent.appfabric.sb.MessageBuffer;

   public class TestPolicy {
      public static void main(String[] args) {
	  try {
		credentials = new Credentials(TOKEN_TYPE.SharedSecretToken, 
                     issuerName, issuerKey);
		MessageBuffer msgBuffer;
		msgBuffer = new MessageBuffer(credentials, serviceNamespace);
		String policy = msgBuffer.getPolicy("TestMessageBuffer");
 	      } catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	  }
      }
   }

 
Running this code snippet generates output similar to following, provided the message buffer exists:
 
 
Go to Top
 
Lock and Retrieve a Message
 
To retrieve the first unlocked message from a message buffer and lock it, the MessageBuffer class provides the peekLock() method. This method will return information about the message like message content, message URI, lock duration and lock URI. The following code snippet demonstrates how to use peekLock(). Note that it assumes that the ‘TestMessageBuffer’ is already created on Service Bus. To understand how to create a message buffer please refer to the code snipet for 'Creating a Message Buffer'.
 
   import com.persistent.appfabric.acs.Credentials;
   import com.persistent.appfabric.acs.Credentials.TOKEN_TYPE;
   import com.persistent.appfabric.sb.LockedMessageInfo;
   import com.persistent.appfabric.sb.MessageBuffer;

   public class TestPeekLock{
        public static void main(String[] args) {
	    try { credentials = new Credentials(TOKEN_TYPE.SharedSecretToken, 
              issuerName, issuerKey);
		MessageBuffer msgBuffer;
		msgBuffer = new MessageBuffer(credentials, serviceNamespace);
		LockedMessageInfo messageInfo = msgBuffer.peekLock
                    ("TestMessageBuffer");
		
	       } catch (Exception e) {
		e.printStackTrace();
	   }
       }
   }	
 
Running this code snippet generates output similar to following provided a message exists on the message buffer:
 
Go to Top
 
Deleting a Message Buffer
The following code snippet demonstrates how to delete an existing message buffer.
 
   import com.persistent.appfabric.acs.Credentials;
   import com.persistent.appfabric.acs.Credentials.TOKEN_TYPE;
   import com.persistent.appfabric.sb.MessageBuffer;

   public class DeleteMessageBuffer{
      public static void main(String[] args) {
	 try { credentials = new Credentials(TOKEN_TYPE.SharedSecretToken, 
              issuerName, issuerKey);
		MessageBuffer msgBuffer;
		msgBuffer = new MessageBuffer(credentials, serviceNamespace);
		msgBuffer.deleteMessageBuffer("TestMessageBuffer");
		
             } catch (Exception e) {
		e.printStackTrace();
	 }
      }
   }	

 
Provided the message buffer exists, running this code snippet generates output similar to following:
 
 
Go to Top