Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Marat Pavlov
/
AlexAdapter
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6fb87588
authored
Mar 01, 2022
by
Alex Nasyr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
activemq
parent
7faabd3d
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
75 additions
and
20 deletions
DDO_Application/Controllers/apiController.cs
DDO_Application/DDO_Application.csproj
DDO_Application/Model/IUploadService.cs → DDO_Application/Model/IApiService.cs
DDO_Application/Model/MessageProducer.cs
DDO_Application/Properties/launchSettings.json
DDO_Application/Services/UploadHostedService.cs → DDO_Application/Services/ApiHostedService.cs
DDO_Application/Services/UploadProcessingService.cs → DDO_Application/Services/ApiProcessingService.cs
DDO_Application/Startup.cs
DDO_Application/Controllers/apiController.cs
View file @
6fb87588
...
@@ -4,32 +4,44 @@ using Microsoft.Extensions.DependencyInjection;
...
@@ -4,32 +4,44 @@ using Microsoft.Extensions.DependencyInjection;
using
DDO_Application.Model
;
using
DDO_Application.Model
;
using
System
;
using
System
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
System.Net
;
namespace
DDO_Application.Controllers
{
namespace
DDO_Application.Controllers
{
[
ApiController
]
[
ApiController
]
public
class
apiController
:
ControllerBase
{
public
class
apiController
:
ControllerBase
{
private
readonly
MessageProducer
_messageProducer
;
private
IApiService
_apiService
;
public
IServiceProvider
Services
{
get
;
}
public
IServiceProvider
Services
{
get
;
}
private
IUploadService
_uploadService
;
public
apiController
(
IServiceProvider
services
)
{
Services
=
services
;
public
apiController
(
IServiceProvider
services
,
MessageProducer
messageProducer
)
{
Services
=
services
;
using
(
var
scope
=
Services
.
CreateScope
())
{
using
(
var
scope
=
Services
.
CreateScope
())
{
var
uploadProcessingService
=
var
uploadProcessingService
=
scope
.
ServiceProvider
scope
.
ServiceProvider
.
GetRequiredService
<
I
Upload
Service
>();
.
GetRequiredService
<
I
Api
Service
>();
_
upload
Service
=
uploadProcessingService
;
_
api
Service
=
uploadProcessingService
;
}
}
_messageProducer
=
messageProducer
;
}
}
[
HttpGet
]
[
HttpGet
]
[
Route
(
"[controller]"
)]
[
Route
(
"[controller]"
)]
public
async
Task
<
ActionResult
<
apiStatus
>>
Get
()
{
public
async
Task
<
ActionResult
<
apiStatus
>>
Get
Status
()
{
return
new
apiStatus
();
return
new
apiStatus
();
}
}
[
HttpGet
]
[
HttpGet
]
[
Route
(
"[controller]/lifetime"
)]
[
Route
(
"[controller]/lifetime"
)]
public
int
GetLifetime
()
=>
_uploadService
.
Counter
;
public
int
GetLifetime
()
=>
_apiService
.
Counter
;
[
HttpPost
]
[
Route
(
"[controller]/sendmsg"
)]
public
async
Task
<
IActionResult
>
SendMessage
(
string
msg
)
{
await
_messageProducer
.
PublishAsync
(
msg
);
return
StatusCode
((
int
)
HttpStatusCode
.
Created
,
null
);
}
}
}
public
class
apiStatus
{
public
class
apiStatus
{
...
...
DDO_Application/DDO_Application.csproj
View file @
6fb87588
...
@@ -5,6 +5,8 @@
...
@@ -5,6 +5,8 @@
</PropertyGroup>
</PropertyGroup>
<ItemGroup>
<ItemGroup>
<PackageReference Include="ArtemisNetClient.Extensions.DependencyInjection" Version="2.6.0" />
<PackageReference Include="ArtemisNetClient.Extensions.Hosting" Version="2.6.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
</ItemGroup>
...
...
DDO_Application/Model/I
Upload
Service.cs
→
DDO_Application/Model/I
Api
Service.cs
View file @
6fb87588
...
@@ -4,7 +4,7 @@ using System.Threading;
...
@@ -4,7 +4,7 @@ using System.Threading;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
namespace
DDO_Application.Model
{
namespace
DDO_Application.Model
{
public
interface
I
Upload
Service
{
public
interface
I
Api
Service
{
public
int
Counter
{
get
;
}
public
int
Counter
{
get
;
}
Task
DoWork
(
CancellationToken
stoppingToken
);
Task
DoWork
(
CancellationToken
stoppingToken
);
}
}
...
...
DDO_Application/Model/MessageProducer.cs
0 → 100644
View file @
6fb87588
using
ActiveMQ.Artemis.Client
;
using
System.Text.Json
;
using
System.Threading.Tasks
;
namespace
DDO_Application.Model
{
public
class
MessageProducer
{
private
readonly
IAnonymousProducer
_producer
;
public
MessageProducer
(
IAnonymousProducer
producer
)
{
_producer
=
producer
;
}
public
async
Task
PublishAsync
<
T
>(
T
message
)
{
var
serialized
=
JsonSerializer
.
Serialize
(
message
);
var
address
=
typeof
(
T
).
Name
;
var
msg
=
new
Message
(
serialized
);
await
_producer
.
SendAsync
(
address
,
msg
);
}
}
}
DDO_Application/Properties/launchSettings.json
View file @
6fb87588
{
{
"$schema"
:
"http://json.schemastore.org/launchsettings.json"
,
"$schema"
:
"http://json.schemastore.org/launchsettings.json"
,
"iisSettings"
:
{
"iisSettings"
:
{
"windowsAuthentication"
:
false
,
"windowsAuthentication"
:
false
,
...
@@ -19,13 +19,23 @@
...
@@ -19,13 +19,23 @@
},
},
"DDO_Application"
:
{
"DDO_Application"
:
{
"commandName"
:
"Project"
,
"commandName"
:
"Project"
,
"dotnetRunMessages"
:
"true"
,
"launchBrowser"
:
true
,
"launchBrowser"
:
true
,
"launchUrl"
:
"swagger"
,
"launchUrl"
:
"swagger"
,
"applicationUrl"
:
"https://localhost:5001;http://localhost:5000"
,
"environmentVariables"
:
{
"environmentVariables"
:
{
"ASPNETCORE_ENVIRONMENT"
:
"Development"
"ASPNETCORE_ENVIRONMENT"
:
"Development"
}
},
"applicationUrl"
:
"https://localhost:5001;http://localhost:5000"
,
"dotnetRunMessages"
:
"true"
},
"WSL"
:
{
"commandName"
:
"WSL2"
,
"launchBrowser"
:
true
,
"launchUrl"
:
"https://localhost:5001/swagger"
,
"environmentVariables"
:
{
"ASPNETCORE_ENVIRONMENT"
:
"Development"
,
"ASPNETCORE_URLS"
:
"https://localhost:5001;http://localhost:5000"
},
"distributionName"
:
""
}
}
}
}
}
}
\ No newline at end of file
DDO_Application/Services/
Upload
HostedService.cs
→
DDO_Application/Services/
Api
HostedService.cs
View file @
6fb87588
...
@@ -6,9 +6,9 @@ using System.Threading;
...
@@ -6,9 +6,9 @@ using System.Threading;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
namespace
DDO_Application.Services
{
namespace
DDO_Application.Services
{
public
class
Upload
HostedService
:
BackgroundService
{
public
class
Api
HostedService
:
BackgroundService
{
public
IServiceProvider
Services
{
get
;
}
public
IServiceProvider
Services
{
get
;
}
public
Upload
HostedService
(
IServiceProvider
services
)
{
public
Api
HostedService
(
IServiceProvider
services
)
{
Services
=
services
;
Services
=
services
;
}
}
...
@@ -16,7 +16,7 @@ namespace DDO_Application.Services {
...
@@ -16,7 +16,7 @@ namespace DDO_Application.Services {
using
(
var
scope
=
Services
.
CreateScope
())
{
using
(
var
scope
=
Services
.
CreateScope
())
{
var
uploadProcessingService
=
var
uploadProcessingService
=
scope
.
ServiceProvider
scope
.
ServiceProvider
.
GetRequiredService
<
I
Upload
Service
>();
.
GetRequiredService
<
I
Api
Service
>();
await
uploadProcessingService
.
DoWork
(
stoppingToken
);
await
uploadProcessingService
.
DoWork
(
stoppingToken
);
}
}
...
...
DDO_Application/Services/
Upload
ProcessingService.cs
→
DDO_Application/Services/
Api
ProcessingService.cs
View file @
6fb87588
...
@@ -5,11 +5,11 @@ using System.Threading;
...
@@ -5,11 +5,11 @@ using System.Threading;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
namespace
DDO_Application.Services
{
namespace
DDO_Application.Services
{
public
class
UploadProcessingService
:
IUpload
Service
{
public
class
ApiProcessingService
:
IApi
Service
{
private
int
_counter
=
0
;
private
int
_counter
=
0
;
public
int
Counter
{
get
=>
_counter
;
}
public
int
Counter
{
get
=>
_counter
;
}
public
Upload
ProcessingService
()
{
public
Api
ProcessingService
()
{
}
}
public
async
Task
DoWork
(
CancellationToken
stoppingToken
)
{
public
async
Task
DoWork
(
CancellationToken
stoppingToken
)
{
...
...
DDO_Application/Startup.cs
View file @
6fb87588
...
@@ -13,6 +13,9 @@ using System;
...
@@ -13,6 +13,9 @@ using System;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
ActiveMQ.Artemis.Client.Extensions.DependencyInjection
;
using
ActiveMQ.Artemis.Client
;
using
ActiveMQ.Artemis.Client.Extensions.Hosting
;
namespace
DDO_Application
{
namespace
DDO_Application
{
public
class
Startup
{
public
class
Startup
{
...
@@ -27,8 +30,16 @@ namespace DDO_Application {
...
@@ -27,8 +30,16 @@ namespace DDO_Application {
services
.
AddControllers
();
services
.
AddControllers
();
services
.
AddSwaggerGen
(
c
=>
{
c
.
SwaggerDoc
(
"v1"
,
new
OpenApiInfo
{
Title
=
"DDO_Application"
,
Version
=
"v1"
});
});
services
.
AddSwaggerGen
(
c
=>
{
c
.
SwaggerDoc
(
"v1"
,
new
OpenApiInfo
{
Title
=
"DDO_Application"
,
Version
=
"v1"
});
});
services
.
AddHostedService
<
UploadHostedService
>();
services
.
AddHostedService
<
ApiHostedService
>();
services
.
AddSingleton
<
IUploadService
,
UploadProcessingService
>();
services
.
AddSingleton
<
IApiService
,
ApiProcessingService
>();
// turn enable ActiveMQ support in project
services
.
AddActiveMq
(
"ddoApp-cluster"
,
new
[]
{
Endpoint
.
Create
(
host
:
"localhost"
,
port
:
5672
,
"guest"
,
"guest"
)
})
.
AddAnonymousProducer
<
MessageProducer
>()
.
AddConsumer
(
"BookUpdated"
,
RoutingType
.
Multicast
,
async
(
message
,
consumer
,
serviceProvider
,
cancellationToken
)
=>
{
// your consuming logic
await
consumer
.
AcceptAsync
(
message
);
});
services
.
AddActiveMqHostedService
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment