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
592237d8
authored
Feb 14, 2020
by
丁松杰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复bugs
parent
a391ba15
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
112 additions
and
48 deletions
src/Pole.Core/EventBus/Bus.cs
src/Pole.Core/EventBus/IBus.cs
src/Pole.Core/EventBus/Transaction/IDbTransactionAdapter.cs
src/Pole.Core/UnitOfWork/IUnitOfWork.cs
src/Pole.Core/UnitOfWork/UnitOfWork.cs
src/Pole.EventStorage.PostgreSql/EnlistBusExtensions.cs
src/Pole.EventStorage.PostgreSql/PostgreSqlDbTransactionAdapter.cs
src/Pole.Core/EventBus/Bus.cs
View file @
592237d8
...
...
@@ -5,6 +5,7 @@ using Pole.Core.EventBus.Transaction;
using
Pole.Core.Serialization
;
using
Pole.Core.Utils.Abstraction
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
...
...
@@ -14,7 +15,6 @@ namespace Pole.Core.EventBus
{
class
Bus
:
IBus
{
private
readonly
IProducer
producer
;
private
readonly
IEventTypeFinder
eventTypeFinder
;
private
readonly
ISerializer
serializer
;
private
readonly
ISnowflakeIdGenerator
snowflakeIdGenerator
;
...
...
@@ -22,11 +22,12 @@ namespace Pole.Core.EventBus
public
IDbTransactionAdapter
Transaction
{
get
;
set
;
}
public
IServiceProvider
ServiceProvider
{
get
;
}
public
BlockingCollection
<
EventEntity
>
PrePublishEventBuffer
{
get
;
}
=
new
BlockingCollection
<
EventEntity
>(
new
ConcurrentQueue
<
EventEntity
>());
public
Bus
(
IServiceProvider
serviceProvider
,
I
Producer
producer
,
I
EventTypeFinder
eventTypeFinder
,
ISerializer
serializer
,
ISnowflakeIdGenerator
snowflakeIdGenerator
,
IEventStorage
eventStorage
)
public
Bus
(
IServiceProvider
serviceProvider
,
IEventTypeFinder
eventTypeFinder
,
ISerializer
serializer
,
ISnowflakeIdGenerator
snowflakeIdGenerator
,
IEventStorage
eventStorage
)
{
ServiceProvider
=
serviceProvider
;
this
.
producer
=
producer
;
this
.
eventTypeFinder
=
eventTypeFinder
;
this
.
serializer
=
serializer
;
this
.
snowflakeIdGenerator
=
snowflakeIdGenerator
;
...
...
@@ -37,10 +38,10 @@ namespace Pole.Core.EventBus
var
eventType
=
@event
.
GetType
();
var
eventTypeCode
=
eventTypeFinder
.
GetCode
(
eventType
);
var
eventId
=
snowflakeIdGenerator
.
NextId
();
var
eventContentBytes
=
serializer
.
SerializeToUtf8Bytes
(
@event
,
eventType
);
//
var eventContentBytes = serializer.SerializeToUtf8Bytes(@event, eventType);
var
eventContent
=
serializer
.
Serialize
(
@event
,
eventType
);
var
bytesTransport
=
new
EventBytesTransport
(
eventTypeCode
,
eventId
,
eventContentBytes
);
var
bytes
=
bytesTransport
.
GetBytes
();
//
var bytesTransport = new EventBytesTransport(eventTypeCode, eventId, eventContentBytes);
//
var bytes = bytesTransport.GetBytes();
var
eventEntity
=
new
EventEntity
{
Added
=
DateTime
.
UtcNow
,
...
...
@@ -59,15 +60,10 @@ namespace Pole.Core.EventBus
{
var
mediumMessage
=
await
eventStorage
.
StoreMessage
(
eventEntity
,
Transaction
.
DbTransaction
);
if
(
Transaction
.
AutoCommit
)
{
await
Transaction
.
CommitAsync
();
}
}
await
producer
.
Publish
(
bytes
);
await
eventStorage
.
ChangePublishStateAsync
(
eventEntity
,
EventStatus
.
Published
);
PrePublishEventBuffer
.
Add
(
eventEntity
);
//await producer.Publish(bytes);
//await eventStorage.ChangePublishStateAsync(eventEntity,EventStatus.Published);
return
true
;
}
...
...
src/Pole.Core/EventBus/IBus.cs
View file @
592237d8
using
Pole.Core.EventBus.Transaction
;
using
Pole.Core.EventBus.EventStorage
;
using
Pole.Core.EventBus.Transaction
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
...
...
@@ -11,6 +13,7 @@ namespace Pole.Core.EventBus
{
IServiceProvider
ServiceProvider
{
get
;
}
IDbTransactionAdapter
Transaction
{
get
;
set
;
}
BlockingCollection
<
EventEntity
>
PrePublishEventBuffer
{
get
;
}
Task
<
bool
>
Publish
(
object
@event
,
CancellationToken
cancellationToken
=
default
);
}
}
src/Pole.Core/EventBus/Transaction/IDbTransactionAdapter.cs
View file @
592237d8
using
System
;
using
Pole.Core.EventBus.EventStorage
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Text
;
using
System.Threading
;
...
...
@@ -10,7 +12,6 @@ namespace Pole.Core.EventBus.Transaction
{
Task
CommitAsync
(
CancellationToken
cancellationToken
=
default
);
Task
RollbackAsync
(
CancellationToken
cancellationToken
=
default
);
bool
AutoCommit
{
get
;
set
;
}
object
DbTransaction
{
get
;
set
;
}
}
}
src/Pole.Core/UnitOfWork/IUnitOfWork.cs
0 → 100644
View file @
592237d8
using
Pole.Core.EventBus
;
using
Pole.Core.EventBus.Transaction
;
using
System
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.Extensions.DependencyInjection
;
namespace
Pole.Core.UnitOfWork
{
public
interface
IUnitOfWork
:
IDisposable
{
Task
CompeleteAsync
(
CancellationToken
cancellationToken
=
default
);
Task
Rollback
(
CancellationToken
cancellationToken
=
default
);
IUnitOfWork
Enlist
(
IDbTransaction
dbTransaction
,
IBus
bus
);
}
}
src/Pole.Core/UnitOfWork/UnitOfWork.cs
0 → 100644
View file @
592237d8
using
Pole.Core.EventBus
;
using
Pole.Core.EventBus.Transaction
;
using
System
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Linq
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.Extensions.DependencyInjection
;
using
Pole.Core.Abstraction
;
using
Pole.Core.Serialization
;
using
Pole.Core.EventBus.Event
;
using
Pole.Core.EventBus.EventStorage
;
namespace
Pole.Core.UnitOfWork
{
class
UnitOfWork
:
IUnitOfWork
{
private
readonly
IProducer
producer
;
private
readonly
IEventTypeFinder
eventTypeFinder
;
private
readonly
ISerializer
serializer
;
private
readonly
IEventStorage
eventStorage
;
private
IBus
bus
;
public
UnitOfWork
(
IProducer
producer
,
IEventTypeFinder
eventTypeFinder
,
ISerializer
serializer
,
IEventStorage
eventStorage
)
{
this
.
producer
=
producer
;
this
.
eventTypeFinder
=
eventTypeFinder
;
this
.
serializer
=
serializer
;
this
.
eventStorage
=
eventStorage
;
}
public
async
Task
CompeleteAsync
(
CancellationToken
cancellationToken
=
default
)
{
await
bus
.
Transaction
.
CommitAsync
();
var
bufferedEvents
=
bus
.
PrePublishEventBuffer
.
ToList
();
bufferedEvents
.
ForEach
(
async
@event
=>
{
var
eventType
=
eventTypeFinder
.
FindType
(
@event
.
Name
);
var
eventContentBytes
=
serializer
.
SerializeToUtf8Bytes
(
@event
,
eventType
);
var
bytesTransport
=
new
EventBytesTransport
(
@event
.
Name
,
@event
.
Id
,
eventContentBytes
);
var
bytes
=
bytesTransport
.
GetBytes
();
await
producer
.
Publish
(
bytes
);
await
eventStorage
.
ChangePublishStateAsync
(
@event
,
EventStatus
.
Published
);
});
}
public
void
Dispose
()
{
bus
=
null
;
}
public
IUnitOfWork
Enlist
(
IDbTransaction
dbTransaction
,
IBus
bus
)
{
bus
.
Transaction
=
bus
.
ServiceProvider
.
GetService
<
IDbTransactionAdapter
>();
bus
.
Transaction
.
DbTransaction
=
dbTransaction
;
this
.
bus
=
bus
;
return
this
;
}
public
Task
Rollback
(
CancellationToken
cancellationToken
=
default
)
{
return
bus
.
Transaction
.
RollbackAsync
();
}
}
}
src/Pole.EventStorage.PostgreSql/EnlistBusExtensions.cs
deleted
100644 → 0
View file @
a391ba15
using
Pole.Core.EventBus
;
using
Pole.Core.EventBus.Transaction
;
using
System
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Text
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.EntityFrameworkCore.Storage
;
namespace
Pole.EventStorage.PostgreSql
{
public
static
class
EnlistBusExtensions
{
public
static
IDbTransaction
EnlistBus
(
this
IDbTransaction
dbTransaction
,
IBus
bus
,
bool
autoCommit
=
false
)
{
bus
.
Transaction
=
bus
.
ServiceProvider
.
GetService
<
IDbTransactionAdapter
>();
bus
.
Transaction
.
DbTransaction
=
dbTransaction
;
bus
.
Transaction
.
AutoCommit
=
autoCommit
;
return
dbTransaction
;
}
public
static
IDbContextTransaction
EnlistBus
(
this
IDbContextTransaction
dbContextTransaction
,
IBus
bus
,
bool
autoCommit
=
false
)
{
bus
.
Transaction
=
bus
.
ServiceProvider
.
GetService
<
IDbTransactionAdapter
>();
bus
.
Transaction
.
DbTransaction
=
dbContextTransaction
;
bus
.
Transaction
.
AutoCommit
=
autoCommit
;
return
dbContextTransaction
;
}
}
}
src/Pole.EventStorage.PostgreSql/PostgreSqlDbTransactionAdapter.cs
View file @
592237d8
using
Microsoft.EntityFrameworkCore.Storage
;
using
Pole.Core.Abstraction
;
using
Pole.Core.EventBus
;
using
Pole.Core.EventBus.Event
;
using
Pole.Core.EventBus.EventStorage
;
using
Pole.Core.EventBus.Transaction
;
using
Pole.Core.Serialization
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Linq
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
...
...
@@ -11,8 +18,7 @@ namespace Pole.EventStorage.PostgreSql
{
class
PostgreSqlDbTransactionAdapter
:
IDbTransactionAdapter
{
public
bool
AutoCommit
{
get
=>
throw
new
NotImplementedException
();
set
=>
throw
new
NotImplementedException
();
}
public
object
DbTransaction
{
get
=>
throw
new
NotImplementedException
();
set
=>
throw
new
NotImplementedException
();
}
public
object
DbTransaction
{
get
;
set
;
}
public
async
Task
CommitAsync
(
CancellationToken
cancellationToken
=
default
)
{
...
...
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