Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
28a0c4c2
Commit
28a0c4c2
authored
Jul 05, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Repair the controller cannot read to customer service
parent
b4d604ef
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
102 additions
and
53 deletions
+102
-53
ValuesController.cs
samples/Sample.Kafka/Controllers/ValuesController.cs
+1
-1
DotNetCore.CAP.Kafka.csproj
src/DotNetCore.CAP.Kafka/DotNetCore.CAP.Kafka.csproj
+1
-5
IProcessor.KafkaJobProcessor.cs
src/DotNetCore.CAP.Kafka/IProcessor.KafkaJobProcessor.cs
+20
-40
TopicAttribute.cs
src/DotNetCore.CAP/Abstractions/TopicAttribute.cs
+6
-2
CAP.ServiceCollectionExtensions.cs
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
+2
-2
IConsumerHandler.Default.cs
src/DotNetCore.CAP/IConsumerHandler.Default.cs
+1
-2
Helper.cs
src/DotNetCore.CAP/Infrastructure/Helper.cs
+31
-0
IConsumerServiceSelector.Default.cs
...tNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs
+40
-1
No files found.
samples/Sample.Kafka/Controllers/ValuesController.cs
View file @
28a0c4c2
...
...
@@ -24,7 +24,7 @@ namespace Sample.Kafka.Controllers
}
public
string
ServerPath
=>
((
IHostingEnvironment
)
HttpContext
.
RequestServices
.
GetService
(
typeof
(
IHostingEnvironment
))).
ContentRootPath
;
[
KafkaTopic
(
"zzwl.topic.finace.callBack"
,
Group
OrExchange
=
"test"
)]
[
KafkaTopic
(
"zzwl.topic.finace.callBack"
,
Group
=
"test"
)]
public
void
KafkaTest
(
Person
person
)
{
Console
.
WriteLine
(
person
.
Name
);
...
...
src/DotNetCore.CAP.Kafka/DotNetCore.CAP.Kafka.csproj
View file @
28a0c4c2
...
...
@@ -13,10 +13,6 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>
<ItemGroup>
<None Include="CAP.KafkaOptions.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="0.9.5" />
</ItemGroup>
...
...
src/DotNetCore.CAP.Kafka/IProcessor.KafkaJobProcessor.cs
View file @
28a0c4c2
...
...
@@ -84,64 +84,44 @@ namespace DotNetCore.CAP.Kafka
{
var
provider
=
scopedContext
.
Provider
;
var
messageStore
=
provider
.
GetRequiredService
<
ICapMessageStore
>();
try
{
var
message
=
await
messageStore
.
GetNextSentMessageToBeEnqueuedAsync
();
if
(
message
!=
null
)
{
try
{
var
sp
=
Stopwatch
.
StartNew
();
message
.
StatusName
=
StatusName
.
Processing
;
await
messageStore
.
UpdateSentMessageAsync
(
message
);
var
jobResult
=
ExecuteJob
(
message
.
KeyName
,
message
.
Content
);
await
ExecuteJobAsync
(
message
.
KeyName
,
message
.
Content
);
sp
.
Stop
();
if
(!
jobResult
)
{
_logger
.
JobFailed
(
new
Exception
(
"topic send failed"
));
}
else
{
//TODO : the state will be deleted when release.
message
.
StatusName
=
StatusName
.
Succeeded
;
await
messageStore
.
UpdateSentMessageAsync
(
message
);
_logger
.
JobExecuted
(
sp
.
Elapsed
.
TotalSeconds
);
}
}
}
catch
(
Exception
)
catch
(
Exception
ex
)
{
_logger
.
ExceptionOccuredWhileExecutingJob
(
message
.
KeyName
,
ex
);
return
false
;
}
}
}
return
true
;
}
private
bool
ExecuteJob
(
string
topic
,
string
content
)
{
try
private
Task
ExecuteJobAsync
(
string
topic
,
string
content
)
{
var
config
=
_kafkaOptions
.
AsRdkafkaConfig
();
using
(
var
producer
=
new
Producer
<
Null
,
string
>(
config
,
null
,
new
StringSerializer
(
Encoding
.
UTF8
)))
{
var
message
=
producer
.
ProduceAsync
(
topic
,
null
,
content
).
Result
;
if
(
message
.
Error
.
Code
==
ErrorCode
.
NoError
)
{
return
true
;
}
else
{
return
false
;
}
}
}
catch
(
Exception
ex
)
{
_logger
.
ExceptionOccuredWhileExecutingJob
(
topic
,
ex
);
return
false
;
producer
.
ProduceAsync
(
topic
,
null
,
content
);
producer
.
Flush
();
}
return
Task
.
CompletedTask
;
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Abstractions/TopicAttribute.cs
View file @
28a0c4c2
...
...
@@ -24,10 +24,14 @@ namespace DotNetCore.CAP.Abstractions
}
/// <summary>
/// the consumer group.
/// kafak --> groups.id
/// rabbitmq --> queue.name
/// </summary>
public
string
Group
OrExchange
{
get
;
set
;
}
public
string
Group
{
get
;
set
;
}
=
"cap.default.group"
;
/// <summary>
/// unused now
/// </summary>
public
bool
IsOneWay
{
get
;
set
;
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
View file @
28a0c4c2
...
...
@@ -80,9 +80,9 @@ namespace Microsoft.Extensions.DependencyInjection
var
types
=
Assembly
.
GetEntryAssembly
().
ExportedTypes
;
foreach
(
var
type
in
types
)
{
if
(
typeof
(
IConsumerService
).
IsAssignableFrom
(
type
))
if
(
Helper
.
IsController
(
type
.
GetTypeInfo
()
))
{
services
.
AddSingleton
(
typeof
(
IConsumerService
),
type
);
services
.
AddSingleton
(
typeof
(
object
),
type
);
}
}
}
...
...
src/DotNetCore.CAP/IConsumerHandler.Default.cs
View file @
28a0c4c2
...
...
@@ -54,8 +54,7 @@ namespace DotNetCore.CAP
public
void
Start
()
{
var
matchs
=
_selector
.
GetCandidatesMethods
(
_serviceProvider
);
var
groupingMatchs
=
matchs
.
GroupBy
(
x
=>
x
.
Value
.
Attribute
.
GroupOrExchange
);
var
groupingMatchs
=
matchs
.
GroupBy
(
x
=>
x
.
Value
.
Attribute
.
Group
);
foreach
(
var
matchGroup
in
groupingMatchs
)
{
...
...
src/DotNetCore.CAP/Infrastructure/Helper.cs
View file @
28a0c4c2
using
System
;
using
System.Reflection
;
using
Newtonsoft.Json
;
namespace
DotNetCore.CAP.Infrastructure
...
...
@@ -46,5 +47,35 @@ namespace DotNetCore.CAP.Infrastructure
{
return
Epoch
.
AddSeconds
(
value
);
}
public
static
bool
IsController
(
TypeInfo
typeInfo
)
{
if
(!
typeInfo
.
IsClass
)
{
return
false
;
}
if
(
typeInfo
.
IsAbstract
)
{
return
false
;
}
if
(!
typeInfo
.
IsPublic
)
{
return
false
;
}
if
(
typeInfo
.
ContainsGenericParameters
)
{
return
false
;
}
if
(!
typeInfo
.
Name
.
EndsWith
(
"Controller"
,
StringComparison
.
OrdinalIgnoreCase
))
{
return
false
;
}
return
true
;
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs
View file @
28a0c4c2
...
...
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using
System.Linq
;
using
System.Reflection
;
using
DotNetCore.CAP.Abstractions
;
using
DotNetCore.CAP.Infrastructure
;
using
Microsoft.Extensions.DependencyInjection
;
namespace
DotNetCore.CAP.Internal
...
...
@@ -38,9 +39,22 @@ namespace DotNetCore.CAP.Internal
public
IReadOnlyList
<
ConsumerExecutorDescriptor
>
SelectCandidates
(
IServiceProvider
provider
)
{
var
consumerServices
=
provider
.
GetServices
<
IConsumerService
>();
var
executorDescriptorList
=
new
List
<
ConsumerExecutorDescriptor
>();
executorDescriptorList
.
AddRange
(
FindConsumersFromInterfaceTypes
(
provider
));
executorDescriptorList
.
AddRange
(
FindConsumersFromControllerTypes
(
provider
));
return
executorDescriptorList
;
}
private
IReadOnlyList
<
ConsumerExecutorDescriptor
>
FindConsumersFromInterfaceTypes
(
IServiceProvider
provider
)
{
var
executorDescriptorList
=
new
List
<
ConsumerExecutorDescriptor
>();
var
consumerServices
=
provider
.
GetServices
<
IConsumerService
>();
foreach
(
var
service
in
consumerServices
)
{
var
typeInfo
=
service
.
GetType
().
GetTypeInfo
();
...
...
@@ -57,6 +71,31 @@ namespace DotNetCore.CAP.Internal
executorDescriptorList
.
Add
(
InitDescriptor
(
topicAttr
,
method
,
typeInfo
));
}
}
return
executorDescriptorList
;
}
private
IReadOnlyList
<
ConsumerExecutorDescriptor
>
FindConsumersFromControllerTypes
(
IServiceProvider
provider
)
{
var
executorDescriptorList
=
new
List
<
ConsumerExecutorDescriptor
>();
// at cap startup time, find all Controller into the DI container,the type is object.
var
controllers
=
provider
.
GetServices
<
object
>();
foreach
(
var
controller
in
controllers
)
{
var
typeInfo
=
controller
.
GetType
().
GetTypeInfo
();
//double check
if
(
Helper
.
IsController
(
typeInfo
))
{
foreach
(
var
method
in
typeInfo
.
DeclaredMethods
)
{
var
topicAttr
=
method
.
GetCustomAttribute
<
TopicAttribute
>(
true
);
if
(
topicAttr
==
null
)
continue
;
executorDescriptorList
.
Add
(
InitDescriptor
(
topicAttr
,
method
,
typeInfo
));
}
}
continue
;
}
return
executorDescriptorList
;
}
...
...
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