azure webjob java_使用 WebJobs 运行后台任务 | Azure Docs
在 Azure 應(yīng)用服務(wù)中使用 WebJobs 運(yùn)行后臺(tái)任務(wù)Run background tasks with WebJobs in Azure App Service
10/19/2020
本文內(nèi)容
本文介紹如何使用 Azure 門戶部署 WebJobs,以便上傳可執(zhí)行文件或腳本。This article shows how to deploy WebJobs by using the Azure portal to upload an executable or script. 有關(guān)如何使用 Visual Studio 開發(fā)和部署 WebJobs 的信息,請(qǐng)參閱使用 Visual Studio 部署 WebJobs。For information about how to develop and deploy WebJobs by using Visual Studio, see Deploy WebJobs using Visual Studio.
概述Overview
WebJobs 是 Azure 應(yīng)用服務(wù)的一項(xiàng)功能,它允許你在與 Web 應(yīng)用、API 應(yīng)用或移動(dòng)應(yīng)用相同的實(shí)例中運(yùn)行程序或腳本。WebJobs is a feature of Azure App Service that enables you to run a program or script in the same instance as a web app, API app, or mobile app. 使用 Web 作業(yè)無需支付額外的費(fèi)用。There is no additional cost to use WebJobs.
重要
Linux 上的應(yīng)用服務(wù)尚不支持 WebJobs。WebJobs is not yet supported for App Service on Linux.
可以結(jié)合 WebJobs 使用 Azure WebJobs SDK 來簡化許多編程任務(wù)。The Azure WebJobs SDK can be used with WebJobs to simplify many programming tasks. For more information, see What is the WebJobs SDK.
Web 作業(yè)類型WebJob types
下表描述了 連續(xù) 和 觸發(fā) Web 作業(yè)之間的差別。The following table describes the differences between continuous and triggered WebJobs.
連續(xù)Continuous
觸發(fā)Triggered
創(chuàng)建 Web 作業(yè)后立即啟動(dòng)。Starts immediately when the WebJob is created. 若要防止作業(yè)終止,程序或腳本通常在無限循環(huán)中執(zhí)行其工作。To keep the job from ending, the program or script typically does its work inside an endless loop. 如果作業(yè)確實(shí)終止,可將其重啟。If the job does end, you can restart it.
僅當(dāng)手動(dòng)觸發(fā)或按計(jì)劃觸發(fā)時(shí)啟動(dòng)。Starts only when triggered manually or on a schedule.
在運(yùn)行 Web 應(yīng)用的所有實(shí)例上運(yùn)行。Runs on all instances that the web app runs on. 可以選擇性地將 Web 作業(yè)限制為單個(gè)實(shí)例。You can optionally restrict the WebJob to a single instance.
在 Azure 選擇用于負(fù)載均衡的單個(gè)實(shí)例上運(yùn)行。Runs on a single instance that Azure selects for load balancing.
支持遠(yuǎn)程調(diào)試。Supports remote debugging.
不支持遠(yuǎn)程調(diào)試。Doesn't support remote debugging.
備注
Web 應(yīng)用可在進(jìn)入非活動(dòng)狀態(tài) 20 分鐘后超時(shí)。A web app can time out after 20 minutes of inactivity. 只有向?qū)嶋H Web 應(yīng)用發(fā)出的請(qǐng)求才會(huì)重置計(jì)時(shí)器。Only requests to the actual web app reset the timer. 在 Azure 門戶中查看應(yīng)用的配置或向高級(jí)工具站點(diǎn) (https://.scm.chinacloudsites.cn) 發(fā)出請(qǐng)求不會(huì)重置計(jì)時(shí)器。Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://.scm.chinacloudsites.cn) don't reset the timer. 如果應(yīng)用運(yùn)行連續(xù)性或計(jì)劃的(計(jì)時(shí)器觸發(fā)器)WebJobs,可啟用 Always On 來確保 WebJobs 可靠運(yùn)行。If your app runs continuous or scheduled (Timer trigger) WebJobs, enable Always On to ensure that the WebJobs run reliably. 此功能僅在基本、標(biāo)準(zhǔn)和高級(jí)定價(jià)層中提供。This feature is available only in the Basic, Standard, and Premium pricing tiers.
支持的腳本或程序文件類型Supported file types for scripts or programs
支持以下文件類型:The following file types are supported:
.cmd、.bat、.exe(使用 Windows cmd).cmd, .bat, .exe (using Windows cmd)
.ps1(使用 PowerShell).ps1 (using PowerShell)
.sh(使用 Bash).sh (using Bash)
.php(使用 PHP).php (using PHP)
.py(使用 Python).py (using Python)
.js(使用 Node.js).js (using Node.js)
.jar(使用 Java).jar (using Java)
創(chuàng)建連續(xù) Web 作業(yè)Create a continuous WebJob
在 Azure 門戶中,轉(zhuǎn)到應(yīng)用服務(wù) Web 應(yīng)用、API 應(yīng)用或移動(dòng)應(yīng)用的“應(yīng)用服務(wù)”頁。In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.
選擇“Web 作業(yè)”。Select WebJobs .
在“Web 作業(yè)”頁中,選擇“添加”。In the WebJobs page, select Add .
使用表中指定的“添加 Web 作業(yè)”?設(shè)置。Use the Add WebJob?settings as specified in the table.
設(shè)置Setting
示例值Sample value
說明Description
名稱Name
myContinuousWebJobmyContinuousWebJob
在應(yīng)用服務(wù)應(yīng)用中唯一的名稱。A name that is unique within an App Service app. 必須以字母或數(shù)字開頭,且不能包含除“-”和“”以外的特殊字符。Must start with a letter or a number and cannot contain special characters other than "-" and "".
文件上傳File Upload
ConsoleApp.zipConsoleApp.zip
一個(gè) .zip 文件,其中包含可執(zhí)行文件或腳本文件,以及運(yùn)行程序或腳本所需的所有支持文件。A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. 支持的文件類型部分中列出了支持的可執(zhí)行文件或腳本文件類型。The supported executable or script file types are listed in the Supported file types section.
類型Type
連續(xù)Continuous
本文前面介紹了 Web 作業(yè)類型。The WebJob types are described earlier in this article.
縮放Scale
多實(shí)例Multi instance
僅適用于連續(xù) Web 作業(yè)。Available only for Continuous WebJobs. 確定程序或腳本是在所有實(shí)例還是只在一個(gè)實(shí)例上運(yùn)行。Determines whether the program or script runs on all instances or just one instance. 指定要在多個(gè)實(shí)例上運(yùn)行的選項(xiàng)不適用于免費(fèi)或共享定價(jià)層。The option to run on multiple instances doesn't apply to the Free or Shared pricing tiers.
單擊 “確定” 。Click OK .
新的 Web 作業(yè)隨即顯示在“Web 作業(yè)”頁上。The new WebJob appears on the WebJobs page.
要停止或重啟某個(gè)連續(xù) Web 作業(yè),請(qǐng)?jiān)诹斜碇杏益I單擊該 Web 作業(yè),并單擊“停止”或“啟動(dòng)”。To stop or restart a continuous WebJob, right-click the WebJob in the list and click Stop or Start .
創(chuàng)建手動(dòng)觸發(fā)的 Web 作業(yè)Create a manually triggered WebJob
在 Azure 門戶中,轉(zhuǎn)到應(yīng)用服務(wù) Web 應(yīng)用、API 應(yīng)用或移動(dòng)應(yīng)用的“應(yīng)用服務(wù)”頁。In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.
選擇“Web 作業(yè)”。Select WebJobs .
在“Web 作業(yè)”頁中,選擇“添加”。In the WebJobs page, select Add .
使用表中指定的“添加 Web 作業(yè)”設(shè)置。Use the Add WebJob settings as specified in the table.
設(shè)置Setting
示例值Sample value
說明Description
名稱Name
myTriggeredWebJobmyTriggeredWebJob
在應(yīng)用服務(wù)應(yīng)用中唯一的名稱。A name that is unique within an App Service app. 必須以字母或數(shù)字開頭,且不能包含除“-”和“”以外的特殊字符。Must start with a letter or a number and cannot contain special characters other than "-" and "".
文件上傳File Upload
ConsoleApp.zipConsoleApp.zip
一個(gè) .zip 文件,其中包含可執(zhí)行文件或腳本文件,以及運(yùn)行程序或腳本所需的所有支持文件。A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. 支持的文件類型部分中列出了支持的可執(zhí)行文件或腳本文件類型。The supported executable or script file types are listed in the Supported file types section.
類型Type
觸發(fā)Triggered
本文前面介紹了 Web 作業(yè)類型。The WebJob types are described earlier in this article.
觸發(fā)器Triggers
手動(dòng)Manual
單擊 “確定” 。Click OK .
新的 Web 作業(yè)隨即顯示在“Web 作業(yè)”頁上。The new WebJob appears on the WebJobs page.
如果要運(yùn)行 Web 作業(yè),請(qǐng)?jiān)诹斜碇杏益I單擊其名稱,并單擊“運(yùn)行” 。To run the WebJob, right-click its name in the list and click Run .
創(chuàng)建計(jì)劃的 Web 作業(yè)Create a scheduled WebJob
在 Azure 門戶中,轉(zhuǎn)到應(yīng)用服務(wù) Web 應(yīng)用、API 應(yīng)用或移動(dòng)應(yīng)用的“應(yīng)用服務(wù)”頁。In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.
選擇“Web 作業(yè)”。Select WebJobs .
在“Web 作業(yè)”頁中,選擇“添加”。In the WebJobs page, select Add .
使用表中指定的“添加 Web 作業(yè)”設(shè)置。Use the Add WebJob settings as specified in the table.
設(shè)置Setting
示例值Sample value
說明Description
名稱Name
myScheduledWebJobmyScheduledWebJob
在應(yīng)用服務(wù)應(yīng)用中唯一的名稱。A name that is unique within an App Service app. 必須以字母或數(shù)字開頭,且不能包含除“-”和“”以外的特殊字符。Must start with a letter or a number and cannot contain special characters other than "-" and "".
文件上傳File Upload
ConsoleApp.zipConsoleApp.zip
一個(gè) .zip 文件,其中包含可執(zhí)行文件或腳本文件,以及運(yùn)行程序或腳本所需的所有支持文件。A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. 支持的文件類型部分中列出了支持的可執(zhí)行文件或腳本文件類型。The supported executable or script file types are listed in the Supported file types section.
類型Type
觸發(fā)Triggered
本文前面介紹了 Web 作業(yè)類型。The WebJob types are described earlier in this article.
觸發(fā)器Triggers
計(jì)劃Scheduled
要使計(jì)劃可靠運(yùn)行,請(qǐng)啟用 Always On 功能。For the scheduling to work reliably, enable the Always On feature. Always On 僅在基本、標(biāo)準(zhǔn)和高級(jí)定價(jià)層中提供。Always On is available only in the Basic, Standard, and Premium pricing tiers.
CRON 表達(dá)式CRON Expression
0 0/20 * * * *0 0/20 * * * *
以下部分介紹了 CRON 表達(dá)式。CRON expressions are described in the following section.
單擊 “確定” 。Click OK .
新的 Web 作業(yè)隨即顯示在“Web 作業(yè)”頁上。The new WebJob appears on the WebJobs page.
NCRONTAB 表達(dá)式NCRONTAB expressions
可以在門戶中輸入 NCRONTAB 表達(dá)式,或者在 Web 作業(yè) .zip 文件的根目錄中包含一個(gè) settings.job 文件,如以下示例中所示:You can enter a NCRONTAB expression in the portal or include a settings.job file at the root of your WebJob .zip file, as in the following example:
{
"schedule": "0 */15 * * * *"
}
備注
運(yùn)行 CRON 表達(dá)式使用的默認(rèn)時(shí)區(qū)為協(xié)調(diào)世界時(shí) (UTC)。The default time zone used to run CRON expressions is Coordinated Universal Time (UTC). 若要讓 CRON 表達(dá)式基于其他時(shí)區(qū)運(yùn)行,請(qǐng)為函數(shù)應(yīng)用創(chuàng)建一個(gè)名為 WEBSITE_TIME_ZONE 的應(yīng)用設(shè)置。To have your CRON expression run based on another time zone, create an app setting for your function app named WEBSITE_TIME_ZONE. 若要了解詳細(xì)信息,請(qǐng)參閱 NCRONTAB 時(shí)區(qū)。To learn more, see NCRONTAB time zones.
查看作業(yè)歷史記錄View the job history
選擇要查看其歷史記錄 Web 作業(yè),并選擇“日志”按鈕。Select the WebJob you want to see history for, and then select the Logs button.
在“Web 作業(yè)詳細(xì)信息”頁中,選擇一個(gè)時(shí)間以查看一個(gè)運(yùn)行輪次的詳細(xì)信息。In the WebJob Details page, select a time to see details for one run.
在“Web 作業(yè)運(yùn)行詳細(xì)信息”頁中,選擇“切換輸出”查看日志內(nèi)容的文本。In the WebJob Run Details page, select Toggle Output to see the text of the log contents.
若要在單獨(dú)的瀏覽器窗口中查看輸出文本,請(qǐng)選擇“下載”。To see the output text in a separate browser window, select download . 若要下載文本本身,請(qǐng)右鍵單擊“下載”,并使用瀏覽器選項(xiàng)來保存文件內(nèi)容。To download the text itself, right-click download and use your browser options to save the file contents.
選擇頁面頂部的“Web 作業(yè)”痕跡導(dǎo)航鏈接轉(zhuǎn)到 Web 作業(yè)列表。Select the WebJobs breadcrumb link at the top of the page to go to a list of WebJobs.
后續(xù)步驟Next steps
可以結(jié)合 WebJobs 使用 Azure WebJobs SDK 來簡化許多編程任務(wù)。The Azure WebJobs SDK can be used with WebJobs to simplify many programming tasks. For more information, see What is the WebJobs SDK.
總結(jié)
以上是生活随笔為你收集整理的azure webjob java_使用 WebJobs 运行后台任务 | Azure Docs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 应用指定显示到副屏_广电演播领域与舞台L
- 下一篇: JAVA解析存储过程获取的对象_java