activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ChicagoBob123 <b...@bobfx.com>
Subject Re: Problems with configuring and using ActiveMQ with http
Date Fri, 10 Sep 2010 15:06:11 GMT

Dejan,
and everyone else THANK YOU.

I read this many times but did not understand how to implement it. 
But after going through it again and again and doing some trial 
and error I have finally found a combination that works. Let me share some
C# code.. 
This REST protocol really balances things out. It may not be nearly as fast
as native tcp etc.. but 
it allows for C# programmers without any libraries to get data from an
ActiveMQ queue. 

NOTE: I HAD to use clientID. (Maybe I am doing something wrong)
Without it the http GET would hang even though I thought I was using the
cookie info correctly. 
Post works with a text stream as well. So now I have a simple XML http post
and get.
Thanks everyone.  
Now I have more questions but will ask in another thread. 
Here is some C# code that works here. Thanks.

// gets one message from the queue at a time.
void ReadMessageQueue()
{
 // disable button so it can not be called twice 
 // because if there is no data in queue the timeout takes a while
 GetQueue.Enabled = false;
 byte[] buf = new byte[8192];
 String myuri =
"http://localhost:8080/demo/message/abc?type=queue&clientId=myid";  
 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(data);
 myHttpWebRequest.Method = "GET"; 
 // Set the content type of the data being posted. 
 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 
 // I created a global defined CookieContainer object that I reuse every
time I 
 // connect 
 myHttpWebRequest.CookieContainer = CC; 
 WebResponse myHttpWebResponse =
(WebResponse)myHttpWebRequest.GetResponse();
 CC = myHttpWebRequest.CookieContainer;
 // get the response stream
 Stream receiveStream = (Stream)myHttpWebResponse.GetResponseStream();
 // Reads up to 8K characters at a time 
 int count = receiveStream.Read(buf, 0, buf.Length); 
 String TotalMessage;
 TotalMessage = "";
 while (count > 0)  
  {
   String str = Encoding.ASCII.GetString(buf, 0, count);
   TotalMessage += str;
   count = receiveStream.Read(buf, 0, buf.Length); 
  }

 // display returned message to top on screen
 MessageRecieved.Text = TotalMessage + "\r\n" + MessageRecieved.Text;
 // Releases the resources of the response.
 receiveStream.Close();
 // Releases the resources of the Stream.
 myHttpWebResponse.Close();

 // enable button 
 GetQueue.Enabled = true;
}

  void PostMessageToQueue()
  {
   String data = "http://localhost:8080/demo/message/abc?type=queue";
   HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create(data);
   myHttpWebRequest.Method = "POST";

   string postData = "&body=" + MessageToSend.Text;
   ASCIIEncoding encoding = new ASCIIEncoding (); 
   byte[] byte1 = encoding.GetBytes (postData); 

   // Set the content type of the data being posted. 
   myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";   
   // Set the content length of the string being posted. 
   myHttpWebRequest.ContentLength = byte1.Length;
   Stream os = null;
   os = myHttpWebRequest.GetRequestStream();
   os.Write(byte1, 0, byte1.Length);
   os.Close();

   WebResponse myHttpWebResponse =
(WebResponse)myHttpWebRequest.GetResponse();

   Stream receiveStream = (Stream)myHttpWebResponse.GetResponseStream();
   Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

   // Pipes the stream to a higher level stream reader with the required
encoding format. 
   StreamReader readStream = new StreamReader(receiveStream, encode);

   Char[] read = new Char[256];
   // Reads 256 characters at a time.    
   int count = readStream.Read(read, 0, 256);
   List<String> retval = new List<String>();
   while (count > 0)
   {
    // Dumps the 256 characters on a string and displays the string to the
console.
    String str = new String(read, 0, count);
    retval.Add(str);
    count = readStream.Read(read, 0, 256);
   }

   readStream.Close();

   // Releases the resources of the response.
   receiveStream.Close();

   // Releases the resources of the Stream.
   myHttpWebResponse.Close();

  }
-- 
View this message in context: http://activemq.2283324.n4.nabble.com/configuring-and-using-ActiveMQ-with-http-tp2531794p2534590.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Mime
View raw message