Tutorial: How to leverage Service Bus features using SDK
 
 
 
Overview
This tutorial explains how developers can easily use the AppFabric Ruby gem to write Ruby 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 Ruby gem provides a class called MessageBuffer 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 Ruby gem to create a message buffer.
 
    msg_buffer = MessageBuffer.new(issuer_name, issuer_key, solution_name, 
         service_name)
    msg_buffer_policy_obj = MessageBufferPolicy.new("Required", "None", 
         "PT5M", 10)
    msg_buffer.create_message_buffer("salesopruby", msg_buffer_policy_obj)
 
For creation of a message buffer, 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, "salesopruby" 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
 
    msg_buffer = MessageBuffer.new(issuer_name, issuer_key, solution_name, 
         service_name)
    msg_buffer_policy_obj = MessageBufferPolicy.new("Required", "None", 
         "PT5M", 10)
    msg_buffer.create_message_buffer("salesopruby", msg_buffer_policy_obj)
    message_text = 'This
        is not a message in salesopruby'
    msg_buffer.send_message('salesopruby', message_text)

 
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, "salesopruby".
 
    msg_buffer = MessageBuffer.new(issuer_name, issuer_key, solution_name, 
         service_name)
    msg_buffer_policy_obj = MessageBufferPolicy.new("Required", "None", 
         "PT5M", 10)
    msg_buffer.create_message_buffer("salesopruby", msg_buffer_policy_obj)
    message_text = 'This
        is not a message in salesopruby'
    msg_buffer.send_message('salesopruby', message_text)
    message = msg_buffer.retrieve_message('salesopruby')

 
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 Ruby gem. Message buffer policy associated to a message buffer can be obtained using the MessageBuffer#policy demonstrated in the code snippet below:
 
    msg_buffer = MessageBuffer.new(issuer_name, issuer_key, solution_name, 
        service_name)
    msg_buffer_policy_obj = MessageBufferPolicy.new("Required", "None", 
        "PT5M", 10)
    msg_buffer.create_message_buffer("salesopruby", msg_buffer_policy_obj)
    policy_msg = msg_buffer.policy("salesopruby")

 
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, MessageBuffer class provides the peek_lock 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 MessageBuffer#peek_lock.
 
   msg_buffer = MessageBuffer.new(issuer_name, issuer_key, solution_name, 
       service_name)
   msg_buffer_policy_obj = MessageBufferPolicy.new("Required", "None", 
       "PT5M", 10)
   msg_buffer.create_message_buffer("salesopruby", msg_buffer_policy_obj)
   message_text = 'This
       is not a message in salesopruby'
   msg_buffer.send_message('salesopruby', message_text)
   locked_message_info = msg_buffer.peek_lock('salesopruby')

 
Running this code snippet generates following output contains Message URI, Lock Id and Lock URI :
 
 
Go to Top
 
Deleting a Message Buffer
 
    msg_buffer = MessageBuffer.new(issuer_name, issuer_key, solution_name, 
        service_name)
    msg_buffer_policy_obj = MessageBufferPolicy.new("Required", "None", 
        "PT5M", 10)
    msg_buffer.create_message_buffer("salesopruby", msg_buffer_policy_obj)
    msg_buffer.delete_message_buffer("salesopruby")

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