1313
1414namespace EventMesh . Runtime . AMQP
1515{
16- public class AMQPConsumer : IMessageConsumer
16+ public class AMQPConsumer : BaseMessageConsumer < AMQPOptions >
1717 {
1818 private readonly List < AMQPSubscriptionRecord > _subscriptions = new List < AMQPSubscriptionRecord > ( ) ;
1919 private readonly IBrokerConfigurationStore _brokerConfigurationStore ;
2020 private readonly AMQPOptions _opts ;
2121 private readonly IClientStore _clientStore ;
22- private readonly RuntimeOptions _runtimeOpts ;
2322 private IConnection _connection ;
2423
25- public event EventHandler < CloudEventArgs > CloudEventReceived ;
26-
2724 public AMQPConsumer (
2825 IBrokerConfigurationStore brokerConfigurationStore ,
2926 IClientStore clientStore ,
3027 IOptions < AMQPOptions > opts ,
31- IOptions < RuntimeOptions > runtimeOpts )
28+ IOptions < RuntimeOptions > runtimeOpts ) : base ( runtimeOpts )
3229 {
3330 _opts = opts . Value ;
3431 _brokerConfigurationStore = brokerConfigurationStore ;
3532 _clientStore = clientStore ;
36- _runtimeOpts = runtimeOpts . Value ;
3733 }
3834
35+ public override event EventHandler < CloudEventArgs > CloudEventReceived ;
36+
3937 #region Actions
4038
41- public string BrokerName
39+ public override string BrokerName
4240 {
4341 get
4442 {
4543 return _opts . BrokerName ;
4644 }
4745 }
4846
49- public Task Start ( CancellationToken cancellationToken )
47+ public override Task Start ( CancellationToken cancellationToken )
5048 {
5149 var options = GetOptions ( ) ;
5250 var connectionFactory = new ConnectionFactory ( ) ;
@@ -55,7 +53,7 @@ public Task Start(CancellationToken cancellationToken)
5553 return Task . CompletedTask ;
5654 }
5755
58- public Task Stop ( CancellationToken cancellationToken )
56+ public override Task Stop ( CancellationToken cancellationToken )
5957 {
6058 if ( _connection != null )
6159 {
@@ -65,53 +63,16 @@ public Task Stop(CancellationToken cancellationToken)
6563 return Task . CompletedTask ;
6664 }
6765
68- public Task Subscribe ( string topicName , Client client , string sessionId , CancellationToken cancellationToken )
66+ public override void Dispose ( )
6967 {
70- var options = GetOptions ( ) ;
71- var activeSession = client . GetActiveSession ( sessionId ) ;
72- if ( activeSession . HasTopic ( topicName , options . BrokerName ) )
73- {
74- return Task . CompletedTask ;
75- }
76-
77- var topic = client . GetTopic ( topicName , options . BrokerName ) ;
78- if ( topic == null )
79- {
80- topic = client . AddTopic ( topicName , options . BrokerName ) ;
81- }
82-
83- Task . Run ( ( ) =>
84- {
85- Thread . Sleep ( _runtimeOpts . WaitLocalSubscriptionIntervalMS ) ;
86- ListenTopic ( options , topicName , topic , client . ClientId , activeSession . Id ) ;
87- } ) ;
88- activeSession . SubscribeTopic ( topicName , options . BrokerName ) ;
89- return Task . CompletedTask ;
90- }
91-
92- public Task Unsubscribe ( string topicName , Client client , string sessionId , CancellationToken cancellationToken )
93- {
94- var options = GetOptions ( ) ;
95- var activeSession = client . GetActiveSession ( sessionId ) ;
96- if ( ! activeSession . HasTopic ( topicName , options . BrokerName ) )
97- {
98- return Task . CompletedTask ;
99- }
100-
101- var subscription = _subscriptions . First ( s => s . ClientSessionId == sessionId && s . ClientId == client . ClientId && s . TopicName == topicName ) ;
102- subscription . Channel . BasicCancel ( subscription . ConsumerTag ) ;
103- _subscriptions . Remove ( subscription ) ;
104- return Task . CompletedTask ;
68+ Stop ( CancellationToken . None ) . Wait ( ) ;
10569 }
10670
107- public void Dispose ( )
71+ public override void Commit ( string topicName , Client client , string sessionId , int nbEvts )
10872 {
109- Stop ( CancellationToken . None ) . Wait ( ) ;
11073 }
11174
112- #endregion
113-
114- private void ListenTopic ( AMQPOptions options , string topicName , ClientTopic topic , string clientId , string clientSessionId )
75+ protected override void ListenTopic ( AMQPOptions options , string topicName , ClientTopic topic , string clientId , string clientSessionId )
11576 {
11677 if ( _subscriptions . Any ( s => s . BrokerName == options . BrokerName && s . TopicName == topicName && s . ClientSessionId == clientSessionId && s . ClientId == clientId ) )
11778 {
@@ -137,6 +98,21 @@ private void ListenTopic(AMQPOptions options, string topicName, ClientTopic topi
13798 _subscriptions . Add ( new AMQPSubscriptionRecord ( topicName , options . BrokerName , clientId , clientSessionId , channel , tag ) ) ;
13899 }
139100
101+ protected override void UnsubscribeTopic ( string topicName , Client client , string sessionId )
102+ {
103+ var subscription = _subscriptions . First ( s => s . ClientSessionId == sessionId && s . ClientId == client . ClientId && s . TopicName == topicName ) ;
104+ subscription . Channel . BasicCancel ( subscription . ConsumerTag ) ;
105+ _subscriptions . Remove ( subscription ) ;
106+ }
107+
108+ protected override AMQPOptions GetOptions ( )
109+ {
110+ return _brokerConfigurationStore . Get ( _opts . BrokerName ) . ToAMQPOptions ( ) ;
111+ }
112+
113+ #endregion
114+
115+
140116 private void ReceiveMessage ( object sender , string clientId , string clientSessionId , string topicName , string source , string brokerName , BasicDeliverEventArgs e )
141117 {
142118 var jsonEventFormatter = new JsonEventFormatter ( ) ;
@@ -151,10 +127,5 @@ private void ReceiveMessage(object sender, string clientId, string clientSession
151127 var clientSession = client . GetActiveSessionByTopic ( brokerName , topicName ) ;
152128 CloudEventReceived ( this , new CloudEventArgs ( topicName , brokerName , cloudEvent , client . ClientId , clientSession ) ) ;
153129 }
154-
155- private AMQPOptions GetOptions ( )
156- {
157- return _brokerConfigurationStore . Get ( _opts . BrokerName ) . ToAMQPOptions ( ) ;
158- }
159130 }
160131}
0 commit comments