我正在尝试在Azure上部署WebJobs,其中只有一个WebJob在不断运行。
希望实现具有不同时间表的多个作业的正确实施。因此,如果一个作业每小时运行一次,而另外一个作业运行10分钟,那么如何在不将代码或web.config硬编码的情况下实现此目的呢?
如果这不是他们的方式,那么最佳方式是什么?
答案 0 :(得分:5)
您可以使用TimerTriggerAttribute
:
// Triggered every hours
public static void HourlyTimerJob([TimerTrigger("00:01:00")] TimerInfo timerInfo, TextWriter log)
{
log.WriteLine("Scheduled job fired!");
}
// Triggered every 10 minutes
public static void MinutelyTimerJob([TimerTrigger("00:00:10")] TimerInfo timerInfo, TextWriter log)
{
log.WriteLine("Scheduled job fired!");
}
答案 1 :(得分:0)
您可以运行一个连续的WebJob。该作业将包含各种功能,每个功能都在侦听队列上的消息,例如:
package com.example.nlsonmartins.myapplication.Highlights;
import java.util.ArrayList;
import org.json.*;
public class HighlightsObjectHandler {
// Constants
private final String
JsonKeysHighlightsHolder = "Items",
JsonKeysHighlightUrl = "Url",
JsonKeysHighlightTranslationsHolder = "Traducoes",
JsonKeysHighlightTranslationLanguage = "Idioma",
JsonKeysHighlightTranslationText = "Titulo",
JsonKeysHighlightTranslationImage = "Imagem";
// Enumerators
// Handlers
// Variables
private String
_json;
private Boolean
_updating;
private ArrayList<HighlightObject>
_highlights;
// Properties
public String HighlightsJson() {
return _json;
}
public void HighlightsJson(String highlightsJson) {
// Validate the json. This cannot be null nor equal to the present one ( to prevent firing events on the same data )
if(highlightsJson != _json && highlightsJson != null) {
_json = highlightsJson;
// Fire the Java equivalent of C# 'OnHighlightsJsonChanged( EventArgs.Empty );'
ParseJson();
}
}
public Boolean HighlightsUpdating() {
return _updating;
}
private void HighlightsUpdating(Boolean isUpdating) {
_updating = isUpdating;
}
public ArrayList<HighlightObject> Highlights() {
return _highlights;
}
// Methods
private void ParseJson() {
try {
JSONObject
jsonObject = new JSONObject(HighlightsJson());
// Fire the Java equivalent of C# 'OnHighlightsContentsChanging( EventArgs.Empty );'
// Parse the JSON object
// Fire the Java equivalent of C# 'OnHighlightsContentsChanged( EventArgs.Empty );'
} catch (JSONException exception) {
}
}
// Events
/* Create the event handler for 'OnHighlightsJsonChanged' */
/* Create the event handler for 'OnHighlightsContentsChanging' and call the 'HighlightsUpdating(true);' method */
/* Create the event handler for 'OnHighlightsContentsChanged' and call the 'HighlightsUpdating(false);' method */
// Constructors
public HighlightsObjectHandler() {
_highlights = new ArrayList<HighlightObject>();
}
}
您可以使用Azure计划程序在适当的时间将消息放置在相应的队列上。然后激活与该触发器关联的WebJob方法。
或者,您可以拥有一个队列消息内容确定要调用哪个方法的队列。
我不确定你的最佳意思是什么,但根据我使用Queues的经验非常简单并且运行良好,特别是因为失败的消息被重试(我认为是7次),这很好,因为Azure中出现了很多错误( 503,丢弃数据库连接等)