Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
丁松杰
/
Pole
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
aa873b89
authored
Jan 17, 2020
by
dingsongjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
基本流程走通
parent
0ad97bd8
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
32 additions
and
25 deletions
samples/apis/Backet.Api/Application/IntegrationEvent/Handler/JustTestWhenProductAddedIntegrationEventHandler.cs
samples/apis/Backet.Api/Application/IntegrationEvent/ProductAddedIntegrationEvent.cs
samples/apis/Backet.Api/Startup.cs
samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs
samples/apis/Product.Api/Startup.cs
src/Pole.Application/EventBus/ReliableMessageTransactionWorker.cs
src/Pole.Domain/UnitOfWork/DefaultUnitOfWork.cs
src/Pole.ReliableMessage.Masstransit/DefaultReliableEventHandlerRegistrarFactory.cs
src/Pole.ReliableMessage.Masstransit/MasstransitBasedMessageBus.cs
src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs
samples/apis/Backet.Api/Application/IntegrationEvent/Handler/JustTestWhenProductAddedIntegrationEventHandler.cs
View file @
aa873b89
using
Pole.Application.EventBus
;
using
Pole.ReliableMessage.Abstraction
;
using
Product.Api.Application.IntergrationEvent
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
...
...
samples/apis/Backet.Api/Application/IntegrationEvent/ProductAddedIntegrationEvent.cs
View file @
aa873b89
...
...
@@ -3,7 +3,7 @@ using System.Collections.Generic;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
Backet.Api.Application.Inte
grationEvent
namespace
Product.Api.Application.Inter
grationEvent
{
public
class
ProductAddedIntegrationEvent
{
...
...
samples/apis/Backet.Api/Startup.cs
View file @
aa873b89
...
...
@@ -56,6 +56,7 @@ namespace Backet.Api
rabbitoption
.
RabbitMqHostUserName
=
Configuration
[
"RabbitmqConfig:HostUserName"
];
rabbitoption
.
RabbitMqHostPassword
=
Configuration
[
"RabbitmqConfig:HostPassword"
];
rabbitoption
.
QueueNamePrefix
=
Configuration
[
"ServiceName"
];
rabbitoption
.
EventHandlerNameSuffix
=
"IntegrationEventHandler"
;
});
messageOption
.
AddMongodb
(
mongodbOption
=>
{
...
...
samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs
View file @
aa873b89
...
...
@@ -39,7 +39,6 @@ namespace Product.Api.Application.CommandHandler
await
_unitOfWork
.
Compelete
();
return
CommonCommandResponse
.
SuccessResponse
;
}
}
}
samples/apis/Product.Api/Startup.cs
View file @
aa873b89
...
...
@@ -57,6 +57,7 @@ namespace Product.Api
rabbitoption
.
RabbitMqHostUserName
=
Configuration
[
"RabbitmqConfig:HostUserName"
];
rabbitoption
.
RabbitMqHostPassword
=
Configuration
[
"RabbitmqConfig:HostPassword"
];
rabbitoption
.
QueueNamePrefix
=
Configuration
[
"ServiceName"
];
rabbitoption
.
EventHandlerNameSuffix
=
"IntegrationEventHandler"
;
});
messageOption
.
AddMongodb
(
mongodbOption
=>
{
...
...
src/Pole.Application/EventBus/ReliableMessageTransactionWorker.cs
View file @
aa873b89
...
...
@@ -24,16 +24,17 @@ namespace Pole.Application.EventBus
public
WorkerStatus
WorkerStatus
{
get
;
set
;
}
public
Task
Commit
(
CancellationToken
cancellationToken
=
default
)
public
async
Task
Commit
(
CancellationToken
cancellationToken
=
default
)
{
var
events
=
_reliableMessageScopedBuffer
.
GetAll
();
try
{
events
.
ToList
().
ForEach
(
async
@event
=>
{
await
_reliableBus
.
Publish
(
@event
.
Event
,
@event
.
PrePublishEventId
,
cancellationToken
);
@event
.
IsPublished
=
true
;
});
var
tasks
=
events
.
Select
(
async
@event
=>
{
await
_reliableBus
.
Publish
(
@event
.
Event
,
@event
.
PrePublishEventId
,
cancellationToken
);
@event
.
IsPublished
=
true
;
});
await
Task
.
WhenAll
(
tasks
);
}
catch
(
Exception
ex
)
{
...
...
@@ -41,7 +42,7 @@ namespace Pole.Application.EventBus
if
(
events
.
Count
(
@event
=>
@event
.
IsPublished
)
>
1
)
{
//这里发布失败 通过预发送后的重试机制去处理, 因为一旦有一个消息发出去后 无法挽回
return
Task
.
FromResult
(
1
)
;
return
;
}
else
{
...
...
@@ -50,7 +51,7 @@ namespace Pole.Application.EventBus
}
}
WorkerStatus
=
WorkerStatus
.
Commited
;
return
Task
.
FromResult
(
1
)
;
return
;
}
public
void
Dispose
()
...
...
@@ -63,7 +64,7 @@ namespace Pole.Application.EventBus
var
events
=
_reliableMessageScopedBuffer
.
GetAll
();
foreach
(
var
@event
in
events
)
{
@event
.
PrePublishEventId
=
await
_reliableBus
.
PrePublish
(
@event
.
Event
,
@event
.
EventType
,
@event
.
PrePublishEventId
,
cancellationToken
);
@event
.
PrePublishEventId
=
await
_reliableBus
.
PrePublish
(
@event
.
Event
,
@event
.
EventType
,
@event
.
CallbackParemeter
,
cancellationToken
);
}
WorkerStatus
=
WorkerStatus
.
PreCommited
;
}
...
...
src/Pole.Domain/UnitOfWork/DefaultUnitOfWork.cs
View file @
aa873b89
...
...
@@ -15,28 +15,30 @@ namespace Pole.Domain.UnitOfWork
{
_workers
=
serviceProvider
.
GetServices
<
IWorker
>().
ToList
();
}
public
Task
Compelete
(
CancellationToken
cancellationToken
=
default
)
public
async
Task
Compelete
(
CancellationToken
cancellationToken
=
default
)
{
_workers
.
OrderBy
(
worker
=>
worker
.
Order
).
ToList
().
ForEach
(
async
worker
=>
var
preCommitTasks
=
_workers
.
OrderBy
(
worker
=>
worker
.
Order
).
Select
(
async
worker
=>
{
await
worker
.
PreCommit
();
});
await
Task
.
WhenAll
(
preCommitTasks
);
try
{
_workers
.
OrderBy
(
worker
=>
worker
.
Order
).
ToList
().
ForEach
(
async
worker
=>
var
commitTasks
=
_workers
.
OrderBy
(
worker
=>
worker
.
Order
).
Select
(
async
worker
=>
{
await
worker
.
Commit
();
});
await
Task
.
WhenAll
(
commitTasks
);
}
catch
(
Exception
ex
)
{
_workers
.
OrderBy
(
worker
=>
worker
.
Order
).
Where
(
worker
=>
worker
.
WorkerStatus
==
WorkerStatus
.
Commited
).
ToList
().
ForEach
(
async
worker
=>
{
await
worker
.
Rollback
();
});
var
rollbackTasks
=
_workers
.
OrderBy
(
worker
=>
worker
.
Order
).
Where
(
worker
=>
worker
.
WorkerStatus
==
WorkerStatus
.
Commited
).
Select
(
async
worker
=>
{
await
worker
.
Rollback
();
});
await
Task
.
WhenAll
(
rollbackTasks
);
throw
ex
;
}
return
Task
.
FromResult
(
1
);
}
public
void
Dispose
()
...
...
src/Pole.ReliableMessage.Masstransit/DefaultReliableEventHandlerRegistrarFactory.cs
View file @
aa873b89
...
...
@@ -19,13 +19,13 @@ namespace Pole.ReliableMessage.Masstransit
public
MasstransitEventHandlerRegistrar
Create
(
Type
eventHnadler
)
{
if
(!
eventHnadler
.
Name
.
EndsWith
(
"EventHandler"
))
if
(!
eventHnadler
.
Name
.
EndsWith
(
_masstransitOptions
.
EventHandlerNameSuffix
))
{
throw
new
Exception
(
"EventHandler Name Must EndWith EventHandler
"
);
throw
new
Exception
(
$"EventHandler Name Must EndWith
{
_masstransitOptions
.
EventHandlerNameSuffix
}
"
);
}
var
reliableEventHandlerParemeterAttribute
=
eventHnadler
.
GetCustomAttributes
(
typeof
(
ReliableEventHandlerParemeterAttribute
),
true
).
FirstOrDefault
();
var
eventHandlerName
=
GetQueueName
(
reliableEventHandlerParemeterAttribute
,
eventHnadler
,
_masstransitOptions
.
QueueNamePrefix
);
var
eventHandlerName
=
GetQueueName
(
reliableEventHandlerParemeterAttribute
,
eventHnadler
,
_masstransitOptions
.
QueueNamePrefix
,
_masstransitOptions
.
EventHandlerNameSuffix
);
var
parentEventHandler
=
eventHnadler
.
BaseType
;
var
eventType
=
parentEventHandler
.
GetGenericArguments
().
ToList
().
FirstOrDefault
();
...
...
@@ -36,9 +36,9 @@ namespace Pole.ReliableMessage.Masstransit
return
eventHandlerRegisterInvoker
;
}
private
string
GetQueueName
(
object
reliableEventHandlerParemeterAttribute
,
Type
eventHnadler
,
string
queueNamePrefix
)
private
string
GetQueueName
(
object
reliableEventHandlerParemeterAttribute
,
Type
eventHnadler
,
string
queueNamePrefix
,
string
eventHandlerNameSuffix
)
{
var
eventHandlerDefaultName
=
$"eventHandler-
{
eventHnadler
.
Name
.
Replace
(
"EventHandler"
,
""
).
ToLowerInvariant
()}
"
;
var
eventHandlerDefaultName
=
$"eventHandler-
{
eventHnadler
.
Name
.
Replace
(
eventHandlerNameSuffix
,
""
).
ToLowerInvariant
()}
"
;
var
eventHandlerName
=
string
.
IsNullOrEmpty
(
queueNamePrefix
)
?
eventHandlerDefaultName
:
$"
{
queueNamePrefix
}
-
{
eventHandlerDefaultName
}
"
;
if
(
reliableEventHandlerParemeterAttribute
!=
null
)
...
...
src/Pole.ReliableMessage.Masstransit/MasstransitBasedMessageBus.cs
View file @
aa873b89
...
...
@@ -15,7 +15,7 @@ namespace Pole.ReliableMessage.Masstransit
_bus
=
bus
;
}
private
readonly
MassTransit
.
IBus
_bus
;
public
Task
Publish
(
object
@event
,
string
reliableMessageId
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
)
)
public
Task
Publish
(
object
@event
,
string
reliableMessageId
,
CancellationToken
cancellationToken
=
default
)
{
var
pipe
=
new
AddReliableMessageIdPipe
(
reliableMessageId
);
return
_bus
.
Publish
(
@event
,
pipe
,
cancellationToken
);
...
...
src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs
View file @
aa873b89
...
...
@@ -13,6 +13,8 @@ namespace Pole.ReliableMessage.Masstransit
public
string
RabbitMqHostUserName
{
get
;
set
;
}
public
string
RabbitMqHostPassword
{
get
;
set
;
}
public
string
QueueNamePrefix
{
get
;
set
;
}
=
string
.
Empty
;
public
string
EventHandlerNameSuffix
=
"EventHandler"
;
/// <summary>
/// 2 个并发
/// </summary>
...
...
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