Tutorial: How to leverage Service Bus features using SDK
 
 
 
Overview
This tutorial explains how developers can easily use the AppFabric SDK to write PHP applications that use the Message Buffer feature of the Service Bus provided by AppFabric.
 
For more information about AppFabric Service Bus kindly refer to MSDN documenation.
 
AppFabric SDK for PHP developers provide a class called MessageBufferClient for performing various operations associated to Message Buffer such as,
  • Creating a Message Buffer on AppFabric 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
 
The following code snippet demonstrates how to use the SDK to create a message buffer.
 
   try 
   {	
        $policy = new MessageBufferPolicy();
	$messageBufferName = "salesopphp" ;
        $messageBufferUri = DotNetServicesEnvironment::getMessageBufferUri
            ("http", ACS_TRUSTED_SERVICE,$messageBufferName) ;
			
	$scope = new Scope(SHARED_SECRET);
	$scope->setIssuerName(ACS_TRUSTED_ISSUER);
	$scope->setIssuerSecret(ACS_TRUSTED_SECRET_KEY);
	$scope->setServiceNamespaceDomain(ACS_TRUSTED_SERVICE);
	$scope->setServiceURI(REQUEST_URI_STR);
				
	//create an object of web proxy @see HttpWebProxy 	
	$httpWebProxy = new HttpWebProxy(HTTP_PROXY, HTTP_PORT); 	
	//create object of MessageBufferClient class
	$client = new MessageBufferClient
             ($messageBufferUri,$scope,$policy,$httpWebProxy);  
	//create Message buffer 
	$response = $client->createMessageBuffer($policy);		
	echo $response;	
   }  
   catch(Exception $e) 
   {
        throw($e);
   }
 
The authentication aspects required for creation of a Message Buffer are managed by the Scop class. For creation of a message buffer, the token type needs to be passed as SharedSecretToken, and the issuer name and issuer secret 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.
 
Running this code snippet generates following output,
 
 
Go to Top
 
Send a Message to Message Buffer
 
The following code snippet demonstrates how to send a message to message buffer named, ‘salesopphp’. Note that, if message buffer with the same name is exists on the service bus then it will not create new message buffer. To understand how to create a message buffer please refer to Creating a Message Buffer
 
  try
  {
        $policy = new MessageBufferPolicy();			
	$messageBufferName = "salesopphp";
        $messageBufferUri = DotNetServicesEnvironment::getMessageBufferUri
            ("http" ,ACS_TRUSTED_SERVICE,$messageBufferName);
	$scope = new Scope(SHARED_SECRET);	 
	$scope->setIssuerName(ACS_TRUSTED_ISSUER);
	$scope->setIssuerSecret(ACS_TRUSTED_SECRET_KEY);
	$scope->setServiceNamespaceDomain(ACS_TRUSTED_SERVICE);
	$scope->setServiceURI(REQUEST_URI_STR);

	//create an object of web proxy @see HttpWebProxy
        $httpWebProxy = new HttpWebProxy(HTTP_PROXY, HTTP_PORT);
        $client = new MessageBufferClient
             ($messageBufferUri,$scope,$policy,$httpWebProxy);		

       //create messqge buffer 
       $client->createMessageBuffer($policy);

      //send message to message buffer 
      $client->sendMessage("Send Message");
  } 
  catch (Exception $e)
  {
	throw($e);
  }

 
Running this code snippet will send message to message buffer
 
Go to Top
 
Retrieve a Message from Message Buffer
 
Following code snippet demonstrates how to retrieve a message from a message buffer named, ‘salesopphp’.
 
  try
  {
       $policy = new MessageBufferPolicy();
       $messageBufferName = "salesopphp"  ;
       $messageBufferUri = DotNetServicesEnvironment::getMessageBufferUri
            ("http" ,ACS_TRUSTED_SERVICE,$messageBufferName) ;
		
       $scope = new Scope(SHARED_SECRET);	 
       $scope->setIssuerName(ACS_TRUSTED_ISSUER);
       $scope->setIssuerSecret(ACS_TRUSTED_SECRET_KEY);
       $scope->setServiceNamespaceDomain(ACS_TRUSTED_SERVICE);
       $scope->setServiceURI(REQUEST_URI_STR);

       //create an object of web proxy @see HttpWebProxy
       $httpWebProxy = new HttpWebProxy(HTTP_PROXY, HTTP_PORT);
       $client = new MessageBufferClient  
           ($messageBufferUri,$scope,$policy,$httpWebProxy);

      //create messqge buffer 
      $client->createMessageBuffer($policy);

      //send message to message buffer 
      $client->sendMessage("Send Message");
      echo $client->getMessage();
   } 
   catch (Exception $e)
   {
      throw($e);
   }

 
Running this code snippet generates output similar to following provided a message exists on the message buffer. In this case, the message retrieved is, “Send Message”:
 
 
Go to Top
 
Retrieving Message Buffer Policy
 
Message buffer policy controls various aspects of the message buffer’s behavior. It is encapsulated in a class named MessageBufferPolicy provided by AppFabric SDK for PHP 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:
 
  try
  {
        $policy = new MessageBufferPolicy();
	$messageBufferName = "salesopphp";
        $messageBufferUri = DotNetServicesEnvironment::getMessageBufferUri
           ("http", ACS_TRUSTED_SERVICE,$messageBufferName);	
	$scope = new Scope(SHARED_SECRET);
	$scope->setIssuerName(ACS_TRUSTED_ISSUER);
	$scope->setIssuerSecret(ACS_TRUSTED_SECRET_KEY);
	$scope->setServiceNamespaceDomain(ACS_TRUSTED_SERVICE);
	$scope->setServiceURI(REQUEST_URI_STR);
	//create an object of web proxy @see HttpWebProxy		
	$httpWebProxy = new HttpWebProxy(HTTP_PROXY, HTTP_PORT);
	
	//create object of MessageBufferClient class
	$client = new MessageBufferClient
            ($messageBufferUri,$scope,$policy,$httpWebProxy); 

	//create Message buffer 
	$response = $client->createMessageBuffer($policy);

	//Get Message buffer policy
	echo $client->getPolicy($policy);
   }
   catch (Exception $e)
   {
	throw $e;
   }

 
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, MessageBufferClient class provides the peekLock() method. This method will return information about the message like message content, message URI, lock duration and lock URI. Following code snippet demonstrates how to use peekLock().
 
   try
   {	
        $policy = new MessageBufferPolicy();			
	$messageBufferName ="salesopphp";
        $messageBufferUri = DotNetServicesEnvironment::getMessageBufferUri
              ("http" ,ACS_TRUSTED_SERVICE,$messageBufferName);		 

	$scope = new Scope(SHARED_SECRET);		 
	$scope->setIssuerName(ACS_TRUSTED_ISSUER);
	$scope->setIssuerSecret(ACS_TRUSTED_SECRET_KEY);
	$scope->setServiceNamespaceDomain(ACS_TRUSTED_SERVICE);
	$scope->setServiceURI(REQUEST_URI_STR);

	//create an object of web proxy @see HttpWebProxy		
	$httpWebProxy = new HttpWebProxy(HTTP_PROXY, HTTP_PORT);
	
	$client = new MessageBufferClient
             ($messageBufferUri,$scope,$policy,$httpWebProxy);
	$client->createMessageBuffer($policy);
	$client->sendMessage("Hello World..!");
	$response = $client->peekLock();
	echo "Message URI:".$response['X-Ms-Message-Location'];
	echo "Lock Id :".$response['X-Ms-Lock-Id'];
	echo "Lock URI:".$response['X-Ms-Lock-Location'];
   } 
   catch(Exception $e)
   {
        throw($e);
   }



 
Running this code snippet generates following output contains Message URI, Lock Id and Lock URI :
 
 
Go to Top
 
Deleting a Message Buffer
 
   try
   {
        $policy = new MessageBufferPolicy();
        $messageBufferName = "salesopphp" ;
        $messageBufferUri = DotNetServicesEnvironment::getMessageBufferUri
             ("http" ,ACS_TRUSTED_SERVICE,$messageBufferName) ;
				
        $scope = new Scope(SHARED_SECRET);
	$scope->setIssuerName(ACS_TRUSTED_ISSUER);
	$scope->setIssuerSecret(ACS_TRUSTED_SECRET_KEY);
	$scope->setServiceNamespaceDomain(ACS_TRUSTED_SERVICE);
	$scope->setServiceURI(REQUEST_URI_STR);
	
        //create an object of web proxy @see HttpWebProxy
        $httpWebProxy = new HttpWebProxy(HTTP_PROXY, HTTP_PORT);
	$client = new MessageBufferClient
             ($messageBufferUri,$scope,$policy,$httpWebProxy);   

        //create message buffer 
	$client->createMessageBuffer($policy);

        //delete message buffer
	$client->DeleteMessageBuffer($policy);	
   } 
   catch (Exception $e)
   {
        throw($e);
   }

 
Provided the message buffer exists, running this code snippet will delete message buffer specified.
 
Go to Top