Now that we have our service bus queue all set up, and our configuration
settings working properly, we need to go
into the Web Roll, Controllers, and HomeController.cs.
In addition to the service bus, and service bus messaging, we also
need to make one final reference,
and that's to the system runtime serialization.
So let's go ahead and say reference, add reference, and we're going to check off
version 4L.
We will use this later, when we go ahead
and we use our client to connect to the queue.
Now let's add two new class variables.
We're going to create a new string, and
I'm going to call this, the connection string.
And this is going to be a cloud configuration manager I mentioned earlier.
This is what's going to get all of the settings out
of those cloud service configuration files that we set up previously.
And if you recall, we set it up as the Microsoft Service Bus connection string.
We do this so that we've securely set everything up and we can
easily switch between connection strings, depending
on our local or our cloud environments.
Next, I'm going to create another string, and this
is going to be the name of the queue.
So we will call this, the sign ups queue. If you recall, our web role
is actually going to accept somebody's email address to sign up for a newsletter.
So this queue, will be all of the email addresses who have signed up.
Next, let's move down to the newsletter method we added previously and
we're going to create a new variable for the name space manager.
So, were going to pull up name space manager,
create from a connection string and that connection string,
is the connection string we've created previously at the top as a class variable.
Make sure we use a capital.
Now that we have our name space manager set up,
we need to check to see if a queue exists
.So we're going to check if the queue exists and the
queue name is the queue name variable we've set up.
So if it doesn't exist, we're going to go ahead and create it.
Now since we've already created this queue earlier.
It should go ahead and find it, so what I'm going to do
is I'm going to add a break point right here, and when we run
this, we'll make sure its picking up that our queue has already been created.If
we go back over to the portal, you'll see our Que is right here.
Univs/g, service bus ,queue currently set to max
size one gigabyte, lets go ahead and run this.
Lets enter our email address say signup,
we'll immediately hit our newsletter method,
we're going to check if the queue exists.
Remember the queue name is sign ups.
We'll go ahead and check.
The queue does not exist, so we're going to
go ahead and create it and then continue.
Let's move back to our portal We'll look at our queues.
And notice now we have a new Signups queue.
If I click on Sign Ups queue, you'll see
the configuration is set to the default date and times.
We're going to
go ahead and change that inside of code next.
So let's go down to Visual Studio.
Stop this and I'm going to add a new class that I
call the queue description, get rid of my break point for you.
And I'm going to say that's the qd and we're
going to say new, new description, and the queue name.
And we're going to set it's max size in megabytes
Equal to 2048. Now this is basically the max size of the
queue setting it to two gigabytes. And then the default message time to live,
we're going to give it a new time span here and I'm going to say five minutes.
So I have the max TTL is now five minutes.
Once we have done that, and I am going to check to
see if the queue exists, rather than the queue
name, I'm going to actually send it the queue description.
Let me save this.
Go back into the portal and delete the Signups queue.
Say Yes. OK, you can see it doesn't exist.
We're going to go over into code.
We'll run this. Let's enter our email address.
Hit Signup.
We get our conformation. Move back to the portal.
Refresh.
And you'll see we have a Signups queue set to two gigabytes of maximum size.
Excellent, everything is working.
If we hit Configure, we should also see the
default message time to live is set to five minutes.
Just like we set in code.
So now we have a working queue.
Let's go ahead and add some messages to it.
So once we've created the queue, now we need to send to the queue.
So I'm going to use something
called the queue client.
And we're going to create this from the connection string.
And we'll notice here, we're going to take two parameters,
the connection string that we previously set up, As well
as queue name, so the previous connection string was connection
string , and then the path we called queue name.
Just make sure that connection string is spelled properly.
Yes it is, connection string.
And now, I want to create a
message and so we're going to do this with an email property.
So let's create a new variable.
This is going to be something called a brokered message.
And we're going to set one of the properties
of that brokered message to be equal to email.
So this is a key value pair that you're seeing here.
And we're going to set it to the email that's
come across as an argument, into our newsletter method.
And then we're going to go ahead and
on the queue client, we're going to tell it to send that brokered message.
So let's go ahead and run this.
Let's go ahead and enter our email address, hit Signup.
We'll move back over to the portal, hit Sign ups, and
you'll notice Sign ups now has a current size of 153 bytes.
And the queue length is one.
And what's important to remember here is, even though my
maximum size is two gigabytes, that is not message size.
There's no limit to the number of messages held in a queue,
but there is a limit on the size of the messages itself.
So the maxium size is 256 K.
So you have to make sure what you're sending across.
Something like an email address is very, very easy to actually
use in a queue and is well under the maximum limit.
Now, maybe we want to see what's actually in that queue.
So if we look at connection information.
I'm going to go back all the way up
to our service bus and get the connection information
You going to see default issuer and default key
so lets go into Visual Studio stop our project.
We'll go over to the database explorer were we are
currently for Azure storage and we're going to add a new
service bus, so lets say Add new connection, I'm going to
say Manual enter so lets go ahead and grab our issuer.
Our namespace if
you remember is univspqns2, our owner is owner, and our default key input here.
So hit OK, and you'll see, here's our service bus.
Here's our queue, here's our signups queue.
We can update, send a test message, receive
a message, as well as get the same properties.
So you can see we still get an active message.
And these are all the properties of time to live that we set in code.
Next,
I'm going to show you how to set up
the worker role to take messages off this queue.