Run jetty on winows azure-

Source: Internet
Author: User

Run jetty on Windows azure. (-- Java, JSP)

The fast CGI support provided by Windows azure allows you to conveniently run phpProgramNative code support features can also easily run other things you want to run, of course, there are some restrictions.

For details, see executing native code in Windows azure and the development environment.

Http://msdn.microsoft.com/en-us/library/dd573362.aspx

 

Effect: http://singtel.cloudapp.net: 9527/

1. Prepare materials

Both JRE and jetty can be downloaded from Google.

Here we use the jre6 64bit and jetty7 zip packages.

2. Upload JRE and jetty.

These two items can also be packed and uploaded to the project, but the file is too large. upload them to blob first. A simple upload program:

---

 Upload to blob  Static   Void Main (String [] ARGs ){ Const   String Config = @" ************* "; Cloudstorageaccount Acct = cloudstorageaccount. parse (config); cloudblobclient blobclient = Acct. createcloudblobclient (); var Container = blobclient. getcontainerreference (" Jre6 "); Container. createifnotexist (); upload (container );} Private   Static   Void Upload (cloudblobcontainer container) {list < String > Failed = New List < String > (); String Basedir = @" C: \ temp \ jre6 \ "; Var files = getfiles (basedir ); Foreach ( String File In Files ){ Try { String Filename = file. Replace (basedir, String . Empty); console. writeline (filename); var blob = container. getblobreference (filename); blob. deleteifexists (); Using (Filestream sr = file. Open (file, filemode. Open) {blob. uploadfromstream (SR);} console. writeline (filename +" -OK "); Thread. Sleep (100 );} Catch {Failed. Add (File );}} Foreach (VAR item In Failed) {console. writeline (item +"-Failed ");} Console. writeline (" All OK "); Console. readkey ();}

---

Upload JRE and Jetty to blob.

Use azure storage Explorer
 
3. Create a cloud project with a worker role.

Serviceconfiguration. cscfg

---

Serviceconfiguration<Serviceconfiguration servicename ="Jettyserver"Xmlns ="Http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"> <Role name ="Jettyworker"> <Instances COUNT ="1"/> <Configurationsettings> <setting name ="Diagnosticsconnectionstring"Value="Defaultendpointsprotocol = HTTPS; accountname = **; accountkey = **"/> </Configurationsettings> </role> </serviceconfiguration>

---

Servicedefinition. csdef

---

Servicedefinition <Servicedefinition name =" Jettyserver "Xmlns =" Http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition "> <Workerrole name =" Jettyworker "Enablenativecodeexecution =" True "> <Configurationsettings> <setting name =" Diagnosticsconnectionstring "/> </Configurationsettings> <endpoints> <inputendpoint name =" Jetty7 "Protocol =" TCP "Port ="9527 "/> </Endpoints> <localresources> <localstorage name =" Jetty7home "Cleanonrolerecycle =" False "Sizeinmb =" 1024 "/> </Localresources> </workerrole> </servicedefinition>

---

Download the program on blob to localstorage in worker role and configure to run jetty.

Jettyworker. CS

---

 Jettyworker workerrole  Using System; Using System. Collections. Generic; Using System. diagnostics; Using System. LINQ; Using System. net; Using System. text; Using System. Threading; Using Microsoft. windowsazure. diagnostics; Using Microsoft. windowsazure. serviceruntime; Namespace Jettyworker { Public   Class Workerrole: roleentrypoint { Public   Override  Void Run (){ // This is a sample worker implementation. Replace with your logic. Trace. writeline (" Jettyworker entry point called "," Information "); While ( True ) {Trace. writeline (" Working "," Information "); Thread. Sleep (1000*60*20 );}} Public   Override  Bool Onstart (){ // Set the maximum number of concurrent connections Servicepointmanager. defaultconnectionlimit = 12; // Diagnosticmonitor. Start ("diagnosticsconnectionstring "); Diagnosticmonitorconfiguration diagobj = diagnosticmonitor. getdefaultinitialconfiguration (); diagobj. Logs. scheduledtransferperiod = timespan. fromminutes (1); diagnosticmonitor. Start (" Diagnosticsconnectionstring ", Diagobj); runjetty = New Runjetty (); String Resourcedir = roleenvironment. getlocalresource ("Jetty7home "). Rootpath; String Port = roleenvironment. currentroleinstance. instanceendpoints [" Jetty7 "]. Ipendpoint. Port. tostring (); runjetty. startjetty (resourcedir, Port ); // For information on handling configuration changes              // See the msdn topic at http://go.microsoft.com/fwlink? Linkid = 166357. Roleenvironment. Changing + = roleenvironmentchanging; Return   Base . Onstart ();} Private   Void Roleenvironmentchanging ( Object Sender, roleenvironmentchangingeventargs e ){ // If a configuration setting is changing              If (E. Changes. Any (change => change Is Roleenvironmentconfigurationsettingchange )){ // Set E. Cancel to true to restart this role instance E. Cancel = True ;}}}}

---

Runjetty. CS

---

 Runjetty. CS Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. diagnostics; Using Microsoft. windowsazure; Using Microsoft. windowsazure. storageclient; Using System. IO; Using System. Threading; Namespace Jettyworker { Public  Class Runjetty { Const   String Jrename =" Jre6 "; Const   String Jettyname =" Jetty7 "; Const   String Stacct = @" Defaultendpointsprotocol = HTTPS; accountname =; accountkey = "; Public   Void Startjetty ( String Jettylocation, String Jettyport) {trace. writeline (" Startjetty... begin "); Trace. writeline (" Jettylocation: "+ Jettylocation +" | Jettyport: "+ Jettyport); trace. writeline (" Initjettyfiles... begin "); Initjettyfiles (jettylocation); trace. writeline (" Initjettyfiles... end "); Trace. writeline (" Startjettyprocess... begin "); Startjettyprocess (jettylocation, jettyport); trace. writeline ("Startjettyprocess... end "); Trace. writeline (" Startjetty... end ");} Private   Void Initjettyfiles ( String Jettylocation) {cloudstorageaccount storageaccount = cloudstorageaccount. parse (stacct); cloudblobclient blobclient = storageaccount. createcloudblobclient (); trace. writeline (" Download jetty begin "); Var Container = blobclient. getcontainerreference (jettyname); container. createifnotexist (); download (container, jettylocation); trace. writeline (" Download jetty end "); Trace. writeline (" Download JRE begin "); Var containerjdk = blobclient. getcontainerreference (jrename); containerjdk. createifnotexist (); download (containerjdk, jettylocation); trace. writeline (" Download JRE end ");} Private   Static   Void Download (cloudblobcontainer container, String Basedir) {blobrequestoptions option = New Blobrequestoptions (); option. useflatbloblisting = True ; Var blobs = container. listblobs (option ); If (! Directory. exists (basedir) directory. createdirectory (basedir); List <ilistblobitem> failedfiles = New List <ilistblobitem> (); Foreach (Var file In Blobs ){ Try {Downloadsingalfile (container, basedir, file );} Catch {Failedfiles. Add (File); trace. writeline (" Failed: "+ File. Uri. tostring ());}} Int Trycount = 0; While (Failedfiles. Count> 0 | trycount> 5) {trace. writeline (" Try failed files. "); Trycount ++; trace. writeline (" Try failed files. "+ Trycount ); For ( Int I = failedfiles. Count-1; I>-1; I --){ Try {Downloadsingalfile (container, basedir, failedfiles [I]); failedfiles. removeat (I );} Catch {Trace. writeline (" Failed: "+ Failedfiles [I]. Uri. tostring ());}}}} Private   Static   Void Downloadsingalfile (cloudblobcontainer container, String Basedir, ilistblobitem file ){ String Filepath = basedir + file. Uri. pathandquery;String Temp = filepath. substring (filepath. lastindexof ('/'), filepath. Length-filepath. lastindexof ('/'); temp = filepath. Replace (temp, String . Empty ); If (Temp. Trim (). Length = 0) Return ; If (! Directory. exists (temp) directory. createdirectory (temp ); If (File. exists (filepath) file. delete (filepath); var blob = container. getblobreference (file. uri. tostring (); blob. downloadtofile (filepath); thread. sleep (50 );} Private   Void Startjettyprocess ( String Jettylocation, String Jettyport) {process newproc = New Process (); streamreader SR; String Returndetails; Try { String Jettyhome = jettylocation + @" \ "+ Jettyname; String Jrehome = jettylocation + @" \ "+ Jrename; newproc. startinfo. useshellexecute =True ; Newproc. startinfo. redirectstandardoutput = False ; Newproc. startinfo. filename = jrehome +" \ Bin \ java.exe "; Newproc. startinfo. Arguments = String . Format (" -Djetty. Port = {0}-djetty. Home = \ "{1} \"-jar \ "{1} \ Start. Jar \" ", Jettyport, jettyhome); newproc. enableraisingevents = False ; Newproc. Start (); trace. writeline (" Service started... "); // Sr = newproc. standardoutput;                 // Returndetails = Sr. readtoend ();                  // Trace. writeline ("information:" + returndetails ); } Catch (Exception ex) {returndetails = ex. Message; trace. writeline (ex. Message );}}}}

---

4. Release a program to view trace information and execution results.


PS: here is just a record of running jetty on the Windows azure platform. the Demo code is messy and there are still many things to consider, such as monitoring the running status of jetty. however, with native code support, localstorage support, you can run some programs you want on the platform, such as MySQL (http://microsoftpdc.com/Sessions/SVC51)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.