/** Java version
* "A palindromic number is a number (in some base ) that is the same when written forwards or backwards"
Task: take any positive integer of two digits or more, reverse the digits, and add to the original number
then test if the result is palindromic number.
* @param x: 1st number
* @return boolean
*/
public static boolean ISPalindromicNumber(int x)
{
StringBuffer sby = (new StringBuffer(""+x)).reverse();
int z = x+Integer.parseInt(sby.toString());
// now test if z is a palindromic number
StringBuffer sbz = (new StringBuffer(""+z)).reverse();
int z1 = Integer.parseInt(sbz.toString());
boolean rc = z==z1;
System.out.println("ISPalindromicNumber for "+x+": "+rc);
return rc;
}
/**
* C# version
**/
public static int Reverse(int x)
{
string s = ""+x;
int len = s.Length;
// can also use Array.Reverse, etc.
StringBuilder sb = new StringBuilder(len);
for (int i = len - 1; i >= 0; i--)
sb.Append(s[i]);
return int.Parse(sb.ToString());
}
// e.g. x=56; 56+65=121 -> true
public static bool IsPalindromicNumber(int x)
{
int z = x + Reverse(x);
// now test if z is a palindromic number
int z1 = Reverse(z);
bool rc = z==z1;
Console.WriteLine("ISPalindromicNumber for "+x+": "+rc);
return rc;
}
A simple and real MSMQ/DotNet use case...
A web admin page sends some lengthy processing request (e.g. bulk emailing; mortgage application). So after admin user clicked a 'send' button, the page posts request data to a ASPX/ASMX Url, displays a processing in progress message(or animated gif) and using a Async JavaScript to check server's progress. Meanwhile, the admin user is continuing on other tasks without blocking - a better user experience. Meanwhile, the server ASPX/ASMX receives the request, sends a MSMQ message and returns immediately (non-blocking). At this point, the watch dog(agent)* (SQL job, .Net service, etc.) calls into ASPX/ASM watch dog function on a predefined interval. The working thread (invoked by the agent) in ASPX/ASMX checks the MSMQ queue to process pending messages and updates request status(with session object,DB persist,etc.) afterward so that the Ajax client can inform back to the admin user on the same web page. So here's C# snippet copied from my own (not from any company) working code. Like always with me, there are at least a couple of other ways to do this. *Note: I don't recommend to use any long/forever running thread in Web App under IIS 6.0+. I prefer using SQL job for better control and easy deployment whenever possible.
In general, there are many choices for async processing (EAI, which is another topic). MSMQ is often easy & cheap way to go.
// make sure the Q is created at web app's startup
protected void Application_Start(object sender, EventArgs e)
{
try
{
if (!MessageQueue.Exists(MQ_PATH))
MessageQueue.Create(MQ_PATH);
}
catch (Exception ex)
{
Log(ex.Message);
}
}
// from ASPX/ASMX code behind in response to admin page's request/post...after security verify
public string SendBulkEmails(string from, string subject, string content)
{
MyQueueing(MQTypeID.email, new string[] {from, subject, content});
// if prefer, create/use custom request class/obj instead 'string[]'
return "Request is received and being procesed.";
}
// our generic request msg
protected void MyQueueing(MQTypeID qType,object data)
{
MessageQueue q = null;
try
{
q = new MessageQueue(Autochart.Global.MQ_PATH);
q.Send(data, ((int)qType).ToString());
}
catch (Exception ex)
{
Log(ex);
}
finally
{
if (q != null) q.Close();
}
}
// called from watch dog function invoked by house keeping agent
protected void ProcessMyQueue()
{
Debug.WriteLine("ProcessMyQueue");
MessageQueue q = null;
try
{
q = new MessageQueue(Autochart.Global.MQ_PATH);
((XmlMessageFormatter)q.Formatter).TargetTypes = m_MessageTypes;
// m_MessageTypes = new Type[] {typeof(string[])}; // add others...
// ticks per sec: 1000,000,000 / 100; 10th a sec (1000000000 / 100) / 10 = 1000000 ticks
TimeSpan ts = new TimeSpan(1000000); // 1=100 nanosec
MessageEnumerator e = q.GetMessageEnumerator2();
while (e.MoveNext())
{
Message m = e.RemoveCurrent(ts);
if (m == null) continue; // till next round
object data = m.Body;
MQTypeID t = (MQTypeID)int.Parse(m.Label);
switch (t)
{
case MQTypeID.email:
string[] req = data as string[];
_SendBulkEmails(req[0], req[1], req[2]); // from, subject, content
break;
case MQTypeID.reports:
break;
default:
Trace("ProcessMyQueue.Unknown_MQ_type: " + t);
break;
}
}
}
catch (Exception ex)
{
Log(ex);
}
finally
{
if (q != null) q.Close();
}
}