From eb762784e7a08d57fd7d070243daf9a9e4aed91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=81=87=E6=AD=A3=E7=BB=8F=E5=93=A5=E5=93=A5?= Date: Sat, 19 Jan 2019 16:26:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=AE=8C=E6=88=90=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=B3=A8=E5=86=8C=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/ASPNetCoreExecutor/DemoJobHandler.cs | 20 +++++++++ samples/ASPNetCoreExecutor/Program.cs | 5 +++ samples/ASPNetCoreExecutor/Startup.cs | 8 ++-- .../appsettings.Development.json | 9 ---- samples/ASPNetCoreExecutor/appsettings.json | 4 +- .../DefaultHandlers/HttpJobHandler.cs | 21 --------- .../DefaultHandlers/SimpleHttpJobHandler.cs | 44 +++++++++++++++++++ src/DotXxlJob.Core/Internal/IPUtility.cs | 7 +++ src/DotXxlJob.Core/Internal/Preconditions.cs | 7 +++ 9 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 samples/ASPNetCoreExecutor/DemoJobHandler.cs delete mode 100644 samples/ASPNetCoreExecutor/appsettings.Development.json delete mode 100644 src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs create mode 100644 src/DotXxlJob.Core/DefaultHandlers/SimpleHttpJobHandler.cs create mode 100644 src/DotXxlJob.Core/Internal/IPUtility.cs create mode 100644 src/DotXxlJob.Core/Internal/Preconditions.cs diff --git a/samples/ASPNetCoreExecutor/DemoJobHandler.cs b/samples/ASPNetCoreExecutor/DemoJobHandler.cs new file mode 100644 index 0000000..70d65a7 --- /dev/null +++ b/samples/ASPNetCoreExecutor/DemoJobHandler.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using DotXxlJob.Core; +using DotXxlJob.Core.Model; + +namespace ASPNetCoreExecutor +{ + /// + /// 示例Job,只是写个日志 + /// + [JobHandler("demoJobHandler")] + public class DemoJobHandler:AbstractJobHandler + { + public override Task Execute(JobExecuteContext context) + { + context.JobLogger.Log("receive demo job handler,parameter:{0}",context.JobParameter); + + return Task.FromResult(ReturnT.SUCCESS); + } + } +} \ No newline at end of file diff --git a/samples/ASPNetCoreExecutor/Program.cs b/samples/ASPNetCoreExecutor/Program.cs index f446a2f..8095f4f 100644 --- a/samples/ASPNetCoreExecutor/Program.cs +++ b/samples/ASPNetCoreExecutor/Program.cs @@ -19,6 +19,11 @@ namespace ASPNetCoreExecutor public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) + .ConfigureLogging((ctx, builder) => + { + builder.AddConfiguration(ctx.Configuration); + builder.AddConsole(); + }) .UseStartup(); } } \ No newline at end of file diff --git a/samples/ASPNetCoreExecutor/Startup.cs b/samples/ASPNetCoreExecutor/Startup.cs index 34bd494..1ae88f9 100644 --- a/samples/ASPNetCoreExecutor/Startup.cs +++ b/samples/ASPNetCoreExecutor/Startup.cs @@ -23,13 +23,15 @@ namespace ASPNetCoreExecutor services.AddXxlJobExecutor(Configuration); services.AddDefaultXxlJobHandlers();// add httpHandler; + + services.AddSingleton(); // 添加自定义的jobHandler + + services.AddAutoRegistry(); // 自动注册 } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app,ILoggingBuilder loggerBuilder, IHostingEnvironment env) + public void Configure(IApplicationBuilder app,IHostingEnvironment env) { - loggerBuilder.AddConsole(); - if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); diff --git a/samples/ASPNetCoreExecutor/appsettings.Development.json b/samples/ASPNetCoreExecutor/appsettings.Development.json deleted file mode 100644 index e203e94..0000000 --- a/samples/ASPNetCoreExecutor/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} diff --git a/samples/ASPNetCoreExecutor/appsettings.json b/samples/ASPNetCoreExecutor/appsettings.json index 4772f08..7d418fb 100644 --- a/samples/ASPNetCoreExecutor/appsettings.json +++ b/samples/ASPNetCoreExecutor/appsettings.json @@ -6,8 +6,10 @@ }, "xxlJob": { "adminAddresses":"http://127.0.0.1:8080", - "appName": "ASPNetCoreExecutor", + "appName": "xxl-job-executor-dotnet", + "specialBindAddress": "127.0.0.1", "port": 5000, + "autoRegistry":true, "accessToken": "", "logRetentionDays": 30 } diff --git a/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs b/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs deleted file mode 100644 index 0a14399..0000000 --- a/src/DotXxlJob.Core/DefaultHandlers/HttpJobHandler.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using DotXxlJob.Core.Model; - -namespace DotXxlJob.Core.DefaultHandlers -{ - [JobHandler("httpJobHandler")] - public class HttpJobHandler:AbsJobHandler - { - public async override Task Execute(JobExecuteContext context) - { - if (string.IsNullOrEmpty(context.JobParameter)) - { - return ReturnT.Failed("url is empty"); - } - //判断是否为单URL - context.JobLogger.Log("Get Request Data:{0}",context.JobParameter); - return ReturnT.SUCCESS; - } - } - -} \ No newline at end of file diff --git a/src/DotXxlJob.Core/DefaultHandlers/SimpleHttpJobHandler.cs b/src/DotXxlJob.Core/DefaultHandlers/SimpleHttpJobHandler.cs new file mode 100644 index 0000000..01eaa9f --- /dev/null +++ b/src/DotXxlJob.Core/DefaultHandlers/SimpleHttpJobHandler.cs @@ -0,0 +1,44 @@ +using System.Net.Http; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using DotXxlJob.Core.Model; + +namespace DotXxlJob.Core.DefaultHandlers +{ + [JobHandler("simpleHttpJobHandler")] + public class SimpleHttpJobHandler:AbsJobHandler + { + private readonly IHttpClientFactory _httpClientFactory; + + private static readonly Regex UrlPattern = + new Regex(@"(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]"); + public SimpleHttpJobHandler(IHttpClientFactory httpClientFactory) + { + this._httpClientFactory = httpClientFactory; + } + public async override Task Execute(JobExecuteContext context) + { + if (string.IsNullOrEmpty(context.JobParameter)) + { + return ReturnT.Failed("url is empty"); + } + + string url = context.JobParameter; + + if (!UrlPattern.IsMatch(url)) + { + return ReturnT.Failed("url format is not valid"); + } + + using (var client = _httpClientFactory.CreateClient(Constants.DefaultHttpClientName)) + { + var responseMessage = await client.GetAsync(url); + } + + //判断是否为单URL + context.JobLogger.Log("Get Request Data:{0}",context.JobParameter); + return ReturnT.SUCCESS; + } + } + +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Internal/IPUtility.cs b/src/DotXxlJob.Core/Internal/IPUtility.cs new file mode 100644 index 0000000..7a4a7e4 --- /dev/null +++ b/src/DotXxlJob.Core/Internal/IPUtility.cs @@ -0,0 +1,7 @@ +namespace DotXxlJob.Core +{ + public class IPUtility + { + + } +} \ No newline at end of file diff --git a/src/DotXxlJob.Core/Internal/Preconditions.cs b/src/DotXxlJob.Core/Internal/Preconditions.cs new file mode 100644 index 0000000..52c32f2 --- /dev/null +++ b/src/DotXxlJob.Core/Internal/Preconditions.cs @@ -0,0 +1,7 @@ +namespace DotXxlJob.Core +{ + public class Preconditions + { + + } +} \ No newline at end of file